|
Prev: hexadecimal addition
Next: What's ^$ mean?
From: vincente13 on 11 Jun 2006 23:11 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
From: vincente13 on 11 Jun 2006 23:34 It seems like i can do this system("ps -fu $USER | grep $INSTANCE | grep -v grep | awk {print \$2} ") Put in a backslash vincente13(a)gmail.com wrote: > 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
From: J?rgen Exner on 11 Jun 2006 23:50 vincente13(a)gmail.com wrote: > 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
From: John W. Krahn on 12 Jun 2006 02:01 vincente13(a)gmail.com wrote: > > 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: map { ( split )[ 1 ], "\n" } grep /\Q$INSTANCE/, `ps -fu $USER`; John -- use Perl; program fulfillment
From: Ted Zlatanov on 12 Jun 2006 15:07 On 11 Jun 2006, vincente13(a)gmail.com wrote: > 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
|
Pages: 1 Prev: hexadecimal addition Next: What's ^$ mean? |