LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Assigning Output of a Command to an Array in Perl (https://www.linuxquestions.org/questions/programming-9/assigning-output-of-a-command-to-an-array-in-perl-846594/)

devUnix 11-25-2010 05:23 PM

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?

Sergei Steshenko 11-25-2010 05:32 PM

Quote:

Originally Posted by devUnix (Post 4171245)
...
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
.

Sergei Steshenko 11-25-2010 05:33 PM

And http://perldoc.perl.org/functions/split.html .

devUnix 11-25-2010 07:09 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4171251)

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.

Tinkster 11-25-2010 11:14 PM

Code:

@File_List=`ls -1 /tmp`;

Sergei Steshenko 11-26-2010 02:30 AM

Quote:

Originally Posted by devUnix (Post 4171308)
...
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.

devUnix 11-26-2010 05:14 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4171508)
Based on which official (from perldoc.perl.org )documentation have you decided to use 'exec' ?

Actually, I invented 'exec()' but forgot its prototype.

devUnix 11-26-2010 05:15 PM

Quote:

Originally Posted by Tinkster (Post 4171411)
Code:

@File_List=`ls -1 /tmp`;

Great!

Sergei Steshenko 11-27-2010 03:57 AM

Quote:

Originally Posted by devUnix (Post 4172168)
Actually, I invented 'exec()' but forgot its prototype.

Are you using functions without first reading documentation describing their actions, inputs and outputs ?

devUnix 11-27-2010 03:43 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4172507)
Are you using functions without first reading documentation describing their actions, inputs and outputs ?

Yes.

Sergei Steshenko 11-27-2010 05:43 PM

Quote:

Originally Posted by devUnix (Post 4172966)
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


All times are GMT -5. The time now is 12:39 PM.