LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-25-2010, 05:23 PM   #1
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 550

Rep: Reputation: 46
Assigning Output of a Command to an Array in Perl


Hi,


I am trying to execute a Unix Command in perl and assigning its output to an array:

Code:
@File_List=exec("ls -1 /tmp");
but it is not working. I have tried the perl function system() also but its return code is getting assigned to the array.

In fact, the output is still being generated and displayed but not being assigned to the array.

Any ideas?
 
Old 11-25-2010, 05:32 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by devUnix View Post
...
Any ideas?
First and foremost, read http://perldoc.perl.org/perlop.html -> http://perldoc.perl.org/perlop.html#...Like-Operators .

Secondly, you do not need this in the first place - read

http://perldoc.perl.org/functions/opendir.html
http://perldoc.perl.org/functions/readdir.html
http://perldoc.perl.org/functions/closedir.html
.
 
Old 11-25-2010, 05:33 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
And http://perldoc.perl.org/functions/split.html .
 
Old 11-25-2010, 07:09 PM   #4
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 550

Original Poster
Rep: Reputation: 46
That is fine! But what I said above is just an example of what I want to do: To assign the output/result of a Unix command to a variable/array.

Now, consider these two lines of perl statements:

this:

Code:
exec("ifconfig eth0");
and this:

Code:
$ip=exec("ifconfig eth0");
produce the same result: output the result on the screen / console / web page or wherever we are redirecting it to.

In fact, I want to store the result of a Shell Script,which I can run using the exec() function, in a variable or an array.
 
Old 11-25-2010, 11:14 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Code:
@File_List=`ls -1 /tmp`;
 
1 members found this post helpful.
Old 11-26-2010, 02:30 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by devUnix View Post
...
Now, consider these two lines of perl statements:

this:

Code:
exec("ifconfig eth0");
...
Based on which official (from perldoc.perl.org )documentation have you decided to use 'exec' ? I gave you the links to the documentation you were supposed to read before even starting the thread.
 
Old 11-26-2010, 05:14 PM   #7
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 550

Original Poster
Rep: Reputation: 46
Quote:
Originally Posted by Sergei Steshenko View Post
Based on which official (from perldoc.perl.org )documentation have you decided to use 'exec' ?
Actually, I invented 'exec()' but forgot its prototype.
 
Old 11-26-2010, 05:15 PM   #8
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 550

Original Poster
Rep: Reputation: 46
Thumbs up

Quote:
Originally Posted by Tinkster View Post
Code:
@File_List=`ls -1 /tmp`;
Great!
 
Old 11-27-2010, 03:57 AM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by devUnix View Post
Actually, I invented 'exec()' but forgot its prototype.
Are you using functions without first reading documentation describing their actions, inputs and outputs ?
 
Old 11-27-2010, 03:43 PM   #10
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 550

Original Poster
Rep: Reputation: 46
Quote:
Originally Posted by Sergei Steshenko View Post
Are you using functions without first reading documentation describing their actions, inputs and outputs ?
Yes.
 
Old 11-27-2010, 05:43 PM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by devUnix View Post
Yes.
Thank you for confirming that you are willfully illiterate. I will take this knowledge into consideration when seeing other threads opened by you in the future, and the standard answer will be: RTFM.

Your code should not work exactly because of the very first sentence of 'exec' description:

http://perldoc.perl.org/functions/exec.html :

Code:
The exec function executes a system command and never returns
.

By the way, you are not only willfully illiterate, you are also ignoring an answer given to you in very first reply posted by me.

Namely (see items in Red in Code section below, scroll right for that):

http://www.linuxquestions.org/questi...4/#post4171251 ->
http://perldoc.perl.org/perlop.html#...Like-Operators ->

Code:
Quote-Like Operators 
...
# qx/STRING/
# `STRING`

A string which is (possibly) interpolated and then executed as a system command with /bin/sh or its equivalent. Shell wildcards, pipes, and redirections will be honored. The collected standard output of the command is returned; standard error is unaffected. In scalar context, it comes back as a single (potentially multi-line) string, or undef if the command failed. In list context, returns a list of lines (however you've defined lines with $/ or $INPUT_RECORD_SEPARATOR), or an empty list if the command failed.

Because backticks do not affect standard error, use shell file descriptor syntax (assuming the shell supports this) if you care to address this. To capture a command's STDERR and STDOUT together:

   1. $output = `cmd 2>&1`;

To capture a command's STDOUT but discard its STDERR:

   1. $output = `cmd 2>/dev/null`;

To capture a command's STDERR but discard its STDOUT (ordering is important here):

   1. $output = `cmd 2>&1 1>/dev/null`;
For those who didn't scroll:

Quote:
The collected standard output of the command is returned; standard error is unaffected. In scalar context, it comes back as a single (potentially multi-line) string, or undef if the command failed. In list context, returns a list of lines

Last edited by Sergei Steshenko; 11-27-2010 at 05:46 PM.
 
0 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to redirect the output of exec command output in perl? sudhap85 Linux - Newbie 3 08-28-2009 03:35 PM
printing array contents after "push" of elements gives a different output in PERL gaynut Programming 1 08-20-2008 04:04 AM
Help!! Shell script to get output of ls -l command into an array kasthana Programming 8 06-01-2008 11:37 PM
Perl - how to capture output of a command with more than one args new_2_unix Linux - Newbie 2 11-29-2007 10:55 PM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM


All times are GMT -5. The time now is 07:23 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration