system call - how-to.

Discussion in 'Perl Misc' started by elsiddik, May 1, 2007.

  1. elsiddik

    elsiddik Guest

    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 code by typing on command line.

    perl -e "system qw (whatis /usr/bin/*)"

    but its not giving me the same whatislog - what am i doing wrong ?

    cheers.


    zaher el siddik
     
    elsiddik, May 1, 2007
    #1
    1. Advertising

  2. elsiddik

    Paul Lalli Guest

    On May 1, 7:22 am, elsiddik <> wrote:
    > 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 code by typing on command line.
    >
    > perl -e "system qw (whatis /usr/bin/*)"
    >
    > but its not giving me the same whatislog - what am i doing wrong ?


    You are using the qw operator. This operator takes a space-separated
    list of barewords and returns a quoted list of words. That is, your
    system() call is equivalent to:
    system('whatis', '/usr/bin/*');

    When you call system() with two or more arguments, Perl executes the
    first argument directly, passing in the remaining arguments. It does
    not spawn a shell at all. So the * in the second argument there is
    not interpreted by any shell to mean "all files", because there is no
    shell to do the interpreting. So you are litterally calling "whatis"
    on the single argument "/usr/bin/*". Since no such file with that
    name exists, there is no output.

    Change your qw() to qq() or q(), and read up on `perldoc -f system`
    for more information.

    Paul Lalli
     
    Paul Lalli, May 1, 2007
    #2
    1. Advertising

  3. elsiddik

    elsiddik Guest

    On May 1, 10:45 pm, Paul Lalli <> wrote:
    > On May 1, 7:22 am, elsiddik <> wrote:
    >
    >
    >
    > > 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 code by typing on command line.

    >
    > > perl -e "system qw (whatis /usr/bin/*)"

    >
    > > but its not giving me the same whatislog - what am i doing wrong ?

    >
    > You are using the qw operator. This operator takes a space-separated
    > list of barewords and returns a quoted list of words. That is, your
    > system() call is equivalent to:
    > system('whatis', '/usr/bin/*');
    >
    > When you call system() with two or more arguments, Perl executes the
    > first argument directly, passing in the remaining arguments. It does
    > not spawn a shell at all. So the * in the second argument there is
    > not interpreted by any shell to mean "all files", because there is no
    > shell to do the interpreting. So you are litterally calling "whatis"
    > on the single argument "/usr/bin/*". Since no such file with that
    > name exists, there is no output.
    >
    > Change your qw() to qq() or q(), and read up on `perldoc -f system`
    > for more information.
    >
    > Paul Lalli



    On May 1, 10:45 pm, Paul Lalli <> wrote:
    > On May 1, 7:22 am, elsiddik <> wrote:
    >
    >
    >
    > > 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 code by typing on command line.

    >
    > > perl -e "system qw (whatis /usr/bin/*)"

    >
    > > but its not giving me the same whatislog - what am i doing wrong ?

    >
    > You are using the qw operator. This operator takes a space-separated
    > list of barewords and returns a quoted list of words. That is, your
    > system() call is equivalent to:
    > system('whatis', '/usr/bin/*');
    >
    > When you call system() with two or more arguments, Perl executes the
    > first argument directly, passing in the remaining arguments. It does
    > not spawn a shell at all. So the * in the second argument there is
    > not interpreted by any shell to mean "all files", because there is no
    > shell to do the interpreting. So you are litterally calling "whatis"
    > on the single argument "/usr/bin/*". Since no such file with that
    > name exists, there is no output.
    >
    > Change your qw() to qq() or q(), and read up on `perldoc -f system`
    > for more information.
    >
    > Paul Lalli


    well i tried running the code with q and qq but im still getting the
    same output --
    the part that i cant understand is when you run the shell script - i
    could see all the /usr/bin/files output with <whatis> details ,
    with perls everything looks quiet good also but instead of the
    <whatis> details - keeps giving me /usr/bin/filename: nothing
    appropriate.

    here is an outlook:

    animate (1) - animates an image or image sequence on any
    X server << shell script
    animate: nothing appropriate.

    i even tried to write a perl script - used File::Basename

    <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";

    </code>

    im keep having the same log :x

    its confusing me big time.

    cheers,

    zaher el siddik
    http://elsiddik.blogspot.com/
     
    elsiddik, May 1, 2007
    #3
  4. elsiddik

    Klaus Guest

    > > On May 1, 7:22 am, elsiddik <> wrote:
    > > > perl -e "system qw (whatis /usr/bin/*)"
    > > > but its not giving me the same whatislog - what am i doing wrong ?


    > On May 1, 10:45 pm, Paul Lalli <> wrote:
    > > You are using the qw operator.
    > > Change your qw() to qq() or q()


    On May 1, 3:11 pm, elsiddik <> wrote:
    > well i tried running the code with q and qq
    > my $path = "/usr/bin/";
    > system "whatis $path";
    > its confusing me big time.


    It's confusing you, probably because you forgot to put an asterisk at
    the end of /usr/bin/

    --
    Klaus
     
    Klaus, May 1, 2007
    #4
  5. elsiddik

    Paul Lalli Guest

    > > On May 1, 7:22 am, elsiddik <> wrote:
    >
    > > > perl -e "system qw (whatis /usr/bin/*)"

    >
    > > > but its not giving me the same whatislog - what am i doing wrong ?

    >
    > > You are using the qw operator. This operator takes a space-separated
    > > list of barewords and returns a quoted list of words. That is, your
    > > system() call is equivalent to:
    > > system('whatis', '/usr/bin/*');

    >
    > > Change your qw() to qq() or q(), and read up on `perldoc -f system`
    > > for more information.


    > well i tried running the code with q and qq but im still getting the
    > same output --


    I don't believe you. Please post a short-but-complete script that
    demonstrates this, like so:

    $ perl -e'system qq(whatis /usr/bin/*)'
    acctcom acctcom (1) - search and print process accounting
    files
    activation-client activation-client () - bonobo-
    activationdebugging tool
    adb adb (1) - general-purpose debugger
    <remainder snipped>

    > the part that i cant understand is when you run the shell script - i
    > could see all the /usr/bin/files output with <whatis> details ,
    > with perls everything looks quiet good also but instead of the
    > <whatis> details - keeps giving me /usr/bin/filename: nothing
    > appropriate.


    Again, post a short-but-complete script that generates this output.

    > i even tried to write a perl script - used File::Basename


    I have no idea what you think File::Basename has to do 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);


    I still have no idea what you think File::Basename has to do with
    this.

    > print "dir is $dir , files is $files\n";


    Didn't that output tell you you're doing something very wrong? It
    should have claimed that $dir is /usr and $files is the empty string.

    > system "whatis $path";


    Now you're just calling whatis on "/usr/bin/", rather than on all the
    files in /usr/bin. Why did you omit the * this time?

    Paul Lalli
     
    Paul Lalli, May 1, 2007
    #5
  6. elsiddik

    elsiddik Guest

    On May 2, 3:12 am, Paul Lalli <> wrote:
    > > > On May 1, 7:22 am, elsiddik <> wrote:

    >
    > > > > perl -e "system qw (whatis /usr/bin/*)"

    >
    > > > > but its not giving me the same whatislog - what am i doing wrong ?

    >
    > > > You are using the qw operator. This operator takes a space-separated
    > > > list of barewords and returns a quoted list of words. That is, your
    > > > system() call is equivalent to:
    > > > system('whatis', '/usr/bin/*');

    >
    > > > Change your qw() to qq() or q(), and read up on `perldoc -f system`
    > > > for more information.

    > > well i tried running the code with q and qq but im still getting the
    > > same output --

    >
    > I don't believe you. Please post a short-but-complete script that
    > demonstrates this, like so:
    >
    > $ perl -e'system qq(whatis /usr/bin/*)'
    > acctcom acctcom (1) - search and print process accounting
    > files
    > activation-client activation-client () - bonobo-
    > activationdebugging tool
    > adb adb (1) - general-purpose debugger
    > <remainder snipped>
    >
    > > the part that i cant understand is when you run the shell script - i
    > > could see all the /usr/bin/files output with <whatis> details ,
    > > with perls everything looks quiet good also but instead of the
    > > <whatis> details - keeps giving me /usr/bin/filename: nothing
    > > appropriate.

    >
    > Again, post a short-but-complete script that generates this output.
    >
    > > i even tried to write a perl script - used File::Basename

    >
    > I have no idea what you think File::Basename has to do 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);

    >
    > I still have no idea what you think File::Basename has to do with
    > this.
    >
    > > print "dir is $dir , files is $files\n";

    >
    > Didn't that output tell you you're doing something very wrong? It
    > should have claimed that $dir is /usr and $files is the empty string.
    >
    > > system "whatis $path";

    >
    > Now you're just calling whatis on "/usr/bin/", rather than on all the
    > files in /usr/bin. Why did you omit the * this time?
    >
    > Paul Lalli



    i could solve it with :
    perl -e 'chdir (shift || "/usr/bin"); system whatis => <*>'

    thx anyway guys

    zaher elsiddik
    http;//elsiddik.blogspot.com/
     
    elsiddik, May 1, 2007
    #6
  7. On 2007-05-01 11:22, elsiddik <> wrote:
    > 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 code by typing on command line.
    >
    > perl -e "system qw (whatis /usr/bin/*)"
    >
    > but its not giving me the same whatislog - what am i doing wrong ?


    Your perl script doesn't do the same thing as your shell script.

    Your shell script loops over all the filenames matching /usr/bin/*,
    extracting the basename of each and calling whatis with that. So if your
    /usr/bin/* starts like this:

    /usr/bin/822-date
    /usr/bin/Mail
    /usr/bin/X
    /usr/bin/X11
    /usr/bin/Xorg
    /usr/bin/[
    /usr/bin/a2p
    /usr/bin/aconnect
    /usr/bin/acpi
    /usr/bin/acpi_fakekey

    it will invoke

    whatis 822-date
    whatis Mail
    whatis X
    ...

    Your perl script just invokes the shell with the command
    "whatis /usr/bin/*" (assuming qq instead of qw), which will will in turn
    invoke

    whatis /usr/bin/822-date /usr/bin/Mail /usr/bin/X /usr/bin/X11 ...


    hp


    --
    _ | Peter J. Holzer | I know I'd be respectful of a pirate
    |_|_) | Sysadmin WSR | with an emu on his shoulder.
    | | | |
    __/ | http://www.hjp.at/ | -- Sam in "Freefall"
     
    Peter J. Holzer, May 6, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bill sandner
    Replies:
    1
    Views:
    881
    iksrazal
    Jul 1, 2004
  2. system call and library call

    , Aug 22, 2005, in forum: C Programming
    Replies:
    7
    Views:
    3,023
    Jonathan Bartlett
    Aug 23, 2005
  3. leoman730

    System call and library call

    leoman730, May 3, 2007, in forum: C++
    Replies:
    6
    Views:
    920
    Jim Langston
    May 6, 2007
  4. David Pratt
    Replies:
    0
    Views:
    265
    David Pratt
    Apr 7, 2008
  5. David Pratt
    Replies:
    0
    Views:
    407
    David Pratt
    Apr 7, 2008
Loading...

Share This Page