system PROGRAM LIST syntax

Discussion in 'Perl Misc' started by jonathan, Jan 5, 2004.

  1. jonathan

    jonathan Guest

    hey all,

    I wanted to use this syntax to rename a complicated - and long - shell
    function involving piping, teeing to another command, etc.
    Unfortunately,

    system { 'ls -lR' } 'secretls';

    doesn't work, since it uses 'ls -lR' as the command itself, not ls
    with -lR as an argument. I could rewrite this as:

    system { 'ls' } 'secretls', '-lR';

    which works for the simple case, but how about:

    system( "ls -lR > 1 2>&1" );


    I can't think of any syntax in list form that could replace this - and
    of course, I *do* need to replace it for the sake of hiding entries in
    the process table.


    Any clue on how to do this?

    Thanks much,

    jon
     
    jonathan, Jan 5, 2004
    #1
    1. Advertising

  2. jonathan

    Ben Morrow Guest

    (jonathan) wrote:
    > hey all,
    >
    > I wanted to use this syntax to rename a complicated - and long - shell
    > function involving piping, teeing to another command, etc.
    > Unfortunately,
    >
    > system { 'ls -lR' } 'secretls';
    >
    > doesn't work, since it uses 'ls -lR' as the command itself, not ls
    > with -lR as an argument.


    Yes. That's the point.

    > I could rewrite this as:
    > system { 'ls' } 'secretls', '-lR';
    >
    > which works for the simple case, but how about:
    >
    > system( "ls -lR > 1 2>&1" );


    my $pid;
    unless($pid = fork) {
    defined $pid or die "fork failed: $!";
    open STDOUT, ">", "1" or die "open failed: $!";
    open STDERR, ">&", STDOUT or die "dup2 failed: $!";
    exec { "/bin/ls" } "secretls", "-lR" or die "exec failed: $!";
    }
    {
    local $SIG{TERM} = sub { kill TERM => $pid };
    local $SIG{INT} = sub { kill INT => $pid };
    -1 != waitpid $pid, 0 or die "waitpid failed: $!";
    }

    (I think).

    > I can't think of any syntax in list form that could replace this - and
    > of course, I *do* need to replace it for the sake of hiding entries in
    > the process table.


    This is entirely pointless. It is generally trivial to find out which
    file a process is executing anyway (I believe BSDish systems keep 'ls'
    in the process table entry somewhere; linux has /proc/*/exe;
    etc.). Either do whatever you need to do in Perl, without invoking
    external commands at all, or arrange things so it doesn't matter if
    people see what programs you're running.

    Ben

    --
    "If a book is worth reading when you are six, *
    it is worth reading when you are sixty." - C.S.Lewis
     
    Ben Morrow, Jan 5, 2004
    #2
    1. Advertising

  3. jonathan

    pkent Guest

    In article <>,
    (jonathan) wrote:

    > which works for the simple case, but how about:
    >
    > system( "ls -lR > 1 2>&1" );


    The problem there is that that's not a command followed by its
    arguments; its a command, an argument and then _shell metacharacters_.
    So you need a shellto execute - and with the multi-arg forms of system()
    you're specifically not getting a shell, which means that thome
    metacharacters aren't doing much.

    Now, off the top of my head, how about:

    system { 'sh' } 'secretls', '-c', 'ls -lR > 1 2>&1';

    Because there you're executing sh(1) and giving it a string to parse...
    Hang on I've just tried that and ps -Afw says:

    pkent 12207 12206 1 23:18 pts/5 00:00:00 secretls -c ls -lR > 1
    2>&1

    which of course shows the args.

    Actually, one thing you can do is open the relevant filehandles in perl
    and do an exec() - no need to involve the shell at all! No need to use
    redirection symbols! I think that the spawned ls(1) process would show
    up but then it's a process, so it _has_ to show up.

    Alternatively you can implement ls -lR in perl and then you have no need
    of a subprocess at all! Already done, in fact, to some extent at
    http://www.perl.com/language/ppt/src/ls/index.html

    So there you go - do it all within perl :)

    P

    --
    pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
    Remove the tea to reply
     
    pkent, Jan 6, 2004
    #3
    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. Parvsandhu
    Replies:
    2
    Views:
    428
    Thomas Weidenfeller
    Jul 11, 2005
  2. gabriele renzi
    Replies:
    2
    Views:
    238
    gabriele renzi
    Dec 31, 2005
  3. Ken Bloom
    Replies:
    3
    Views:
    242
  4. Good Night Moon
    Replies:
    9
    Views:
    315
    Rick DeNatale
    Jul 25, 2007
  5. Christopher M. Lusardi
    Replies:
    3
    Views:
    217
    Alan J. Flavell
    Oct 19, 2003
Loading...

Share This Page