Simple Unix Question, ( Simple for you maybe ) 
Author Message
 Simple Unix Question, ( Simple for you maybe )

From with a perl script.
I want to run the UNIX command ls -l
and have the info/string stored in a variable.

If I do
$MY_VARIABLE = system (ls -l);

$MY_VARIABLE is set to equal 0, which
is the unix return code for ls.

How do I get $MY_VARIABLE to eqaul
-rwxr-xr-x  1  root   sys   ..........   some_file_name

Thanks



Sat, 23 Sep 2000 03:00:00 GMT  
 Simple Unix Question, ( Simple for you maybe )



Quote:

> From with a perl script.
> I want to run the UNIX command ls -l
> and have the info/string stored in a variable.

> If I do
> $MY_VARIABLE = system (ls -l);

> $MY_VARIABLE is set to equal 0, which
> is the unix return code for ls.

> How do I get $MY_VARIABLE to eqaul
> -rwxr-xr-x  1  root   sys   ..........   some_file_name

> Thanks

        $MY_VARIABLE=`ls -l`;

or as an array:


Brian Wheeler



Sat, 23 Sep 2000 03:00:00 GMT  
 Simple Unix Question, ( Simple for you maybe )


Quote:



>> From with a perl script.
>> I want to run the UNIX command ls -l
>> and have the info/string stored in a variable.

>> If I do
>> $MY_VARIABLE = system (ls -l);

>> $MY_VARIABLE is set to equal 0, which
>> is the unix return code for ls.

>> How do I get $MY_VARIABLE to eqaul
>> -rwxr-xr-x  1  root   sys   ..........   some_file_name

>> Thanks

>    $MY_VARIABLE=`ls -l`;

>or as an array:



The key here is to use backquotes around your UNIX command - this will execute
the command and place the results in the variable.  The first example places the
entire result in one scalar variable.  The second example places each line
separately into elements of an array.
Which method you choose depends on what you plan to do with the data.  If you
use the array method, you might want to do a chop on each line to get rid of the
newline.

Dave

- Show quoted text -

Quote:
>Brian Wheeler




Sat, 23 Sep 2000 03:00:00 GMT  
 Simple Unix Question, ( Simple for you maybe )

: From with a perl script.
: I want to run the UNIX command ls -l
: and have the info/string stored in a variable.

: If I do
: $MY_VARIABLE = system (ls -l);
                 ^^^^^^

The very first paragraph of the description of system() in the
'perlfunc' man page says how to capture the output of a command.

You didn't even try to read about system(), did you?

That's Not Good.

Please try and be a responsible Usenet citizen in the future.

   $my_variable = `ls -l`;

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Sat, 23 Sep 2000 03:00:00 GMT  
 Simple Unix Question, ( Simple for you maybe )


I think this is what you meant.

Quote:


>: From with a perl script.
>: I want to run the UNIX command ls -l
>: and have the info/string stored in a variable.

>: If I do
>: $MY_VARIABLE = system (ls -l);
>                 ^^^^^^

>The very first paragraph of the description of system() in the
>'perlfunc' man page says how to capture the output of a command.

>You didn't even try to read about system(), did you?

>That's Not Good.

>Please try and be a responsible Usenet citizen in the future.

>   $my_variable = `ls -l`;

>--
>    Tad McClellan                          SGML Consulting

>    Fort Worth, Texas



Sat, 23 Sep 2000 03:00:00 GMT  
 Simple Unix Question, ( Simple for you maybe )


: I think this is what you meant.

Why do you think that?

: >
: >: From with a perl script.
: >: I want to run the UNIX command ls -l
: >: and have the info/string stored in a variable.
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^

: >   $my_variable = `ls -l`;

That looks like a string stored in a variable to me.

Try:

--------------------------------------
#!/usr/bin/perl -w


$my_variable = `ls -l`;           # all mashed together into a single string
print "==>$my_variable<==\n";
--------------------------------------

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Sat, 23 Sep 2000 03:00:00 GMT  
 Simple Unix Question, ( Simple for you maybe )

Quote:



> : I think this is what you meant.

> Why do you think that?



> : >
> : >: From with a perl script.
> : >: I want to run the UNIX command ls -l
> : >: and have the info/string stored in a variable.
>                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^

> : >   $my_variable = `ls -l`;

> That looks like a string stored in a variable to me.

`ls -l` is back-ticked.  That means perl should execute the command "ls
-l", the output of which is multiple lines and so is a list.

The sequence $my_variable = `ls -l` will yield the count of returned lines
in the variable $my_variable.



As you can see, there is a major difference there.

      _ _ _ _           _    _ _ _ _ _    
     /\_\_\_\_\        /\_\ /\_\_\_\_\_\  
    /\/_/_/_/_/       /\/_/ \/_/_/_/_/_/  QUIDQUID LATINE DICTUM SIT,
   /\/_/__\/_/ __    /\/_/    /\/_/          PROFUNDUM VIDITUR
  /\/_/_/_/_/ /\_\  /\/_/    /\/_/            
 /\/_/ \/_/  /\/_/_/\/_/    /\/_/         (Whatever is said in Latin
 \/_/  \/_/  \/_/_/_/_/     \/_/              sounds profound)

 Rob Tanner
 Manager of Systems and Communications
 Linfield College, McMinnville, OR



Sun, 24 Sep 2000 03:00:00 GMT  
 Simple Unix Question, ( Simple for you maybe )

  >> : > $my_variable = `ls -l`;
  >>
  >> That looks like a string stored in a variable to me.
  >>

  RT> `ls -l` is back-ticked.  That means perl should execute the
  RT> command "ls -l", the output of which is multiple lines and so is a
  RT> list.

no it is the output of ls -l. the list part is determined by the
context. backticks does nothing to the output of its command.

  RT> The sequence $my_variable = `ls -l` will yield the count of
  RT> returned lines in the variable $my_variable.

where did you get this false information? camel2 says the correct thing
on page 52. you always get the full output of backticks. in a list
context a split is done (preserving the newlines) and one list of lines
is returned.



  RT> As you can see, there is a major difference there.

wrongo my little buddy. the only difference is the line split to the list.

uri, who has been lurking out there!

--
Uri Guttman  -----------------  SYStems ARCHitecture and Software Engineering
Perl Hacker for Hire  ----  8 Years of Perl Experience, Available Immediately

Use the Best Search Engine on the Net  --------  http://www.northernlight.com



Sun, 24 Sep 2000 03:00:00 GMT  
 Simple Unix Question, ( Simple for you maybe )




: >
: > : I think this is what you meant.
: >
: >
: > Why do you think that?
: >
: >


: > : >
: > : >: From with a perl script.
: > : >: I want to run the UNIX command ls -l
: > : >: and have the info/string stored in a variable.
: >                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
: >
: >
: > : >   $my_variable = `ls -l`;
: >
: > That looks like a string stored in a variable to me.
: >

: `ls -l` is back-ticked.  That means perl should execute the command "ls
: -l", the output of which is multiple lines and so is a list.

: The sequence $my_variable = `ls -l` will yield the count of returned lines
                                           ^^^^^^^^^^^^^^^
: in the variable $my_variable.

No it won't.

from the 'perlop' man page (my underlining):

-----------------------------------------------
=head2 I/O Operators

There are several I/O operators you should know about.
A string is enclosed by backticks (grave accents) first undergoes
variable substitution just like a double quoted string.  It is then
interpreted as a command, and the output of that command is the value
of the pseudo-literal, like in a shell.  In a scalar context, a single
                                              ^^^^^^^^^^^^^^    ^^^^^^
string consisting of all the output is returned.  In a list context,
^^^^^^                              ^^^^^^^^^^^
a list of values is returned, one for each line of output.  (You can
-----------------------------------------------

Uh. I thought I included some code in the post you are following up to.

You didn't run it I guess.  Code is meant to be run.

That's why I seldom post code _fragments_.

I usually try to post complete programs that can be run after a simple cut
and paste. I guess I'm wasting my time with that...

Please run it and see for yourself.



That part is right.

: As you can see, there is a major difference there.

I can see the difference...

...but it isn't that difference  ;-)

(which I expect was Namsuk Kim's misperception too)

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Sun, 24 Sep 2000 03:00:00 GMT  
 Simple Unix Question, ( Simple for you maybe )



Quote:
> `ls -l` is back-ticked.  That means perl should execute the command "ls
> -l", the output of which is multiple lines and so is a list.

Read the following (again, hopefully):

# perldoc perlop
[SNIP]
    qx/STRING/

    `STRING`
            A string which is interpolated and then executed as
            a system command.  The collected standard output of
            the command is returned.  In scalar context, it
            comes back as a single (potentially multi-line)
            string.  In list context, returns a list of lines
            (however you've defined lines with $/ or
            $INPUT_RECORD_SEPARATOR).

So ONLY in a list context, is the output returned as a list. In scalar
context, the output comes back as a scalar.

Quote:
> The sequence $my_variable = `ls -l` will yield the count of returned lines
> in the variable $my_variable.

It is not that bad that you misinterpret part of the documentation
(where you presumed that multiple lines were always returned as a
list), but making up stuff like this is really bad. If you don't know,
please look it up in the documentation. Try to avoid making things up.

Martien
--
Martien Verbruggen                  |
Webmaster www.tradingpost.com.au    | Little girls, like butterflies, need no
Commercial Dynamics Pty. Ltd.       | excuse - Lazarus Long
NSW, Australia                      |



Sun, 24 Sep 2000 03:00:00 GMT  
 Simple Unix Question, ( Simple for you maybe )

[posted and mailed]

Quote:


> > : >   $my_variable = `ls -l`;

> > That looks like a string stored in a variable to me.

> `ls -l` is back-ticked.  That means perl should execute the command "ls
> -l", the output of which is multiple lines and so is a list.

> The sequence $my_variable = `ls -l` will yield the count of returned lines
> in the variable $my_variable.



> As you can see, there is a major difference there.

Yes, and the difference is, one of you knows what he is talking about, and one
of you doesn't.

Here's a hint; Tad McClellan knows what he is talking about.

Back-ticks, like *many* Perl operators, gives different results depending on
whether it is executed in list or scalar context.  Specifically, in a list
context, back-ticks return a list of newline-terminated strings.  In a scalar
contex, back-ticks return a single string containing all the lines of output.

Instead of incorrectly correcting Tad, you could have tried reading the
documentation for back-ticks, or even, god forbid, *executed the code* to
verify your assertion.

--


    /                                   http://www.ziplink.net/~rjk/
        "It's funny 'cause it's true ... and vice versa."



Mon, 25 Sep 2000 03:00:00 GMT  
 Simple Unix Question, ( Simple for you maybe )


Quote:

> `ls -l` is back-ticked.  That means perl should execute the command "ls
> -l", the output of which is multiple lines and so is a list.

> The sequence $my_variable = `ls -l` will yield the count of returned lines
> in the variable $my_variable.



> As you can see, there is a major difference there.

Oops, I goofed!  :-(


use "$my_variable = `ls -l`", I forgot we are talking about i/o type
activity (pipes actually) and not array vs scalar contexts.

In the above cases, the back-ticked "ls -l" returns its full result to the
variable.  If an list or array variable, line by line and element by
element.  If a scalar, the whole output goes into the scalar as a scalar,
and if you want to view it line by line, you will need to split the line
up (using split or substr or some other equally grotesque mechanism --
which is why I never back-tick into a scalar).

Since I first misinformed and now must repent -- as the bible teaches,
it's payback plus 20%.  So let me describe another way of executing UNIX
commands from within Perl and what the results look like.

open(LS, "ls -l |");     # the "|" is a UNIX pipe symbol
$my_variable = <LS>;     # first line of output of ls command
$my_variable = <LS>;     # second line of output of ls command

etc.

Not terribly useful as shown above, but see what I can do now:

open(LS, "ls -l |");     # the "|" is a UNIX pipe symbol
while (<LS>) {           # a single line of output goes into $_
    ....                 # the stuff I want to do

Quote:
}                        # the "while" loop loops until EOT

Also, another way of collecting the command output:


And this method also works:

open(LS, "ls -l |");     # the "|" is a UNIX pipe symbol
print <LS>;              # prints the output to STDOUT

That's my plus 20%.  Sorry if I mislead anybody with my earlier erroneous
posting.

-- Rob

      _ _ _ _           _    _ _ _ _ _    
     /\_\_\_\_\        /\_\ /\_\_\_\_\_\  
    /\/_/_/_/_/       /\/_/ \/_/_/_/_/_/  QUIDQUID LATINE DICTUM SIT,
   /\/_/__\/_/ __    /\/_/    /\/_/          PROFUNDUM VIDITUR
  /\/_/_/_/_/ /\_\  /\/_/    /\/_/            
 /\/_/ \/_/  /\/_/_/\/_/    /\/_/         (Whatever is said in Latin
 \/_/  \/_/  \/_/_/_/_/     \/_/              sounds profound)

 Rob Tanner
 Manager of Systems and Communications
 Linfield College, McMinnville, OR



Mon, 25 Sep 2000 03:00:00 GMT  
 Simple Unix Question, ( Simple for you maybe )



: Oops, I goofed!  :-(

: Since I first misinformed and now must repent -- as the bible teaches,
: it's payback plus 20%.  

That's a good attitude ;-)

: open(LS, "ls -l |");     # the "|" is a UNIX pipe symbol

Ack!

An open() call without checking the return value.

Please don't promote such fragile practices.

   open(LS, "ls -l |") || die "could not fork  $!";

And then, have a look at this FAQ (part 8):

   "Why doesn't open() return an error when a pipe open fails?"

It would be nice if your program aborted if the shell cannot
find or execute the 'ls' command rather than continuing to
execute and generating strange results...

: That's my plus 20%.  Sorry if I mislead anybody with my earlier erroneous
: posting.

It happens. Don't worry about it.

I guess you owe another 10% or so...    ;-)

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Mon, 25 Sep 2000 03:00:00 GMT  
 
 [ 19 post ]  Go to page: [1] [2]

 Relevant Pages 

1. New to programming/perl, maybe simple question

2. XS - Simple answer to simple question?

3. Simple Question from a simple chap!

4. Simple Question - LWP::Simple not accessing the internet

5. simple, simple question

6. Simple simple question

7. Simple UNIX question

8. Simple TCP client using io::socket::inet not so simple

9. A simple problem befuddling my simple head

10. simple, simple regex Q

11. Help with simple simple perl script...

12. Graphics::Simple 0.03 - C-64-like simple drawing

 

 
Powered by phpBB® Forum Software