Running system commands

V

vincente13

Hi all


Im using
system("ps -fu $USER | grep $INSTANCE | grep -v grep | awk {print $2} "
)

$USER and $INSTANCE are variables from my perl program

However $2 is suppose to be from the pipe output.
Somehow Perl is interpreting $2 to be some variables from the program
itself

So how can differentiate the program variables and system variables?

Appreciate any help
 
V

vincente13

It seems like i can do this
system("ps -fu $USER | grep $INSTANCE | grep -v grep | awk {print \$2}
")

Put in a backslash
 
J

Jürgen Exner

system("ps -fu $USER | grep $INSTANCE | grep -v grep | awk {print $2}
" )

Well, the first question would be why are you forking out an external grep
with all the process overhead involved instead of simply using Perl's
buildin grep() command. And the same for awk. There is nothing that awk can
do that Perl can't do.
$USER and $INSTANCE are variables from my perl program

However $2 is suppose to be from the pipe output.

Actually now. $2 is supposed to be passed literally as a command line
argument to awk without any interpolation whatsoever.
Somehow Perl is interpreting $2 to be some variables from the program
itself

Of course. It is the second group that was matched successfully by the most
recent regular expression.
So how can differentiate the program variables and system variables?

You don't. $2 is not a 'system' variable, whatever that is supposed to mean.
You want to pass the literal text '$2' to awk. And that's simply done by
escaping the $ sign with a backslash.

jue
 
J

John W. Krahn

Im using
system("ps -fu $USER | grep $INSTANCE | grep -v grep | awk {print $2} "
)

$USER and $INSTANCE are variables from my perl program

However $2 is suppose to be from the pipe output.
Somehow Perl is interpreting $2 to be some variables from the program
itself

So how can differentiate the program variables and system variables?

Try it like this instead:

print
map { ( split )[ 1 ], "\n" }
grep /\Q$INSTANCE/,
`ps -fu $USER`;



John
 
T

Ted Zlatanov

It seems like i can do this
system("ps -fu $USER | grep $INSTANCE | grep -v grep | awk {print \$2}
")

Just use sprintf! It's clearer, too.

system sprintf('ps -fu %s | grep %s | grep -v grep | awk {print $2}',
$USER,
$INSTANCE);

Of course, what you are doing is not portable, probably wrong, and
better done with Proc::processTable :)

Ted
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top