system PROGRAM LIST syntax

J

jonathan

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
 
B

Ben Morrow

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
 
P

pkent

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top