Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

system call how-to

by elsiddik (Scribe)
on May 01, 2007 at 10:21 UTC ( #612950=perlquestion: print w/replies, xml ) Need Help??

elsiddik has asked for the wisdom of the Perl Monks concerning the following question:

well i wanted to just mess a little with perl and try if it can give me the same result of a small shell script i wrote a little while ago but it doesnt seems to work out with me . here is my shell script
#!/bin/bash DIRECTORY="/usr/bin" for file in $DIRECTORY/* do whatis `basename $file` done exit 0
i tried to run a similiar perl script by typing
perl -e "system qw (whatis /usr/bin/*)"
but its not giving me the same whatislog - what am i doing wrong ? cheers.

Replies are listed 'Best First'.
Re: system call how-to
by shmem (Chancellor) on May 01, 2007 at 10:42 UTC
    In your shell script, you build a list of files and strip off the directory part of each file with basename, whereas in your perl snippet you simply pass the string /urs/bin/* directly to whatis.

    Not quite the same, is it? For basename in perl, see File::Basename. whatis doesn't work with the full path to a binary.

    Since you invoke system with a list (as opposed to: with a string containing whitespace), no shell is involved, so /usr/bin/* is not even expanded.


    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: system call how-to
by akho (Hermit) on May 01, 2007 at 10:42 UTC
    Doesn't whatis /usr/bin/* do the same thing your script does? Why do you need it then? Is it a part of a larger script?

    AFAIK, system does not interpolate wildcards; that means * does not work. You can write an explicit glob:

    $files = join ' ', glob '/usr/bin/*'; system "whatis $files";
Re: system call how-to
by elsiddik (Scribe) on May 01, 2007 at 11:56 UTC
    now im i managed to use File::Basename. with this code
    #!/usr/bin/perl -w use strict; use warnings; use File::Basename; my $path = "/usr/bin/*"; my $files = basename($path); my $dir = dirname($path); print "dir is $dir , files is $files\n"; system "whatis $path";
    the output of $files is listening all files in the directory - so far so good , but im still stuck with the same problem - the script is not giving me the real whatis command Output. any idea?
      Check that whatis exists in your Bourne shell. It could be an old version that does not have it (system uses the Bourne shell on UNIX by default). Check the return value, or $?.
      If you want to capture the output in Perl, maybe you should be using qx (or back-ticks) instead?
Re: system call how-to
by elsiddik (Scribe) on May 01, 2007 at 11:04 UTC
    $files = join ' ', glob '/usr/bin/*'; system "whatis $files";
    gave me the same results so far - and  perl -e 'chdir shift || "/usr/bin"; system whatis => <*> ended up with  whatis : unknown option. ill give basename a shot - maybe it helps
      Yikes! Where did my parentheses go? So sorry elsiddik, it's really supposed to be:
      perl -e 'chdir (shift || "/usr/bin"); system whatis => <*>'
      as long as one-liner is concerned. The chdir functions takes higher precedence than the logical OR (||) operator. The truth is, I added the shift || part later on the textbox without retesting. My fault, my fault....

      shmem privately messaged me correcting my answer, and suggested me to have some coffee. But I think I need more than that, perhaps taking a fresh bath :D Thanks, shmem :-)

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: system call how-to
by Moron (Curate) on May 01, 2007 at 13:21 UTC
    The first example seems a bit long-winded already in bash and could be improved to:
    ls /usr/bin | xargs whatis
    It didn't look like a worthwhile goal anyway to just use perl -e to system() back to the shell. I'd try something that can be done without the system() call, e.g. counting the number of lines in each file.

    ^M Free your mind!

Re: system call how-to
by naikonta (Curate) on May 01, 2007 at 10:44 UTC
    How about,

    perl -e 'system whatis => </usr/bin/*>'

    Gleh, that's what could happen if you just woke up and tried to do something before your mind was completely clear :-) I mean this,

    $ perl -e 'chdir shift || "/usr/bin"; system whatis => <*>'
    No wonder the first code gave me strange output :D
    Update: fixed the snip, and added this update note.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: system call how-to
by Anonymous Monk on May 01, 2007 at 21:29 UTC
    thx naikonta this  perl -e 'chdir (shift || "/usr/bin"); system whatis => <*>' works the way i want it :) cheers.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://612950]
Approved by andye
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2023-12-05 17:50 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (27 votes). Check out past polls.