Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

load a System command into a Variable 1

Status
Not open for further replies.

polka4ever

Technical User
Jan 25, 2006
42
US
hello,

i would like to load a system command into a variable. The system command is: system "ps -ef | grep java3 | egrep -v grep | awk '{print $2}'";

I'm guessing the way I would do this is:

my $proc = system "ps -ef | grep java3 | egrep -v grep | awk '{print $2}'";

But i have a question though! I'm very new to Perl, and not really an experienced programmer. Do I "HAVE" to use "my" - what does "my" mean? also - am I doing this correctly above?

~p4e
 
2 issues.
Firstly, system returns the error code from the process, not the output. If you want the output (stdout) as I suspect you do then you should use backticks like this:
Code:
my $proc = `ps -ef | grep java3 | egrep -v grep | awk '{print $2}'`;
Also, you might like to consider opening a pipeline for anything that might return more than one line of text so that you can iterate through each record in turn. You might do that like this:
Code:
open PIPE, "ps -ef | grep java3 | egrep -v grep | awk '{print $2}'" or die "Failed to open pipe";
while(<PIPE>) {
  chomp;   # This will remove the linefeed from the end of the record
  # Do something with $_ here since that is where the record is.
}
On the subject of "my", it is used to declare a "scoped" variable which is a kind of temporary variable.

Trojan.


Trojan.
 
Why ponce about with a pipe, when you can just read the output directly into an array to start with
Code:
my @processes = qx{ps -ef | grep java3 | egrep -v grep | awk '{print $2}'};
chomp @processes;
BTW Trojan, how's the thumb healing?
 
One would PONCE about with a pipe because you only deal with one record in memory at a time. ;)
Fundamentaly it's a far better and safer habit to get into.

The thumb is still rubbish I'm afraid. Gonna be years before it properly heals, if ever. :(



Trojan.
 
True, but we are talking about the reduced output from a 'ps' here. If you have that many processes on the box, then the memory used by this task is probably going to be the least of your worries...[smile]
 
hehehe
Very true, but it habits that I'm talking about.
If you get into the habit of banging everything into arrays then one day it will turn round and bite you.
If you teach yourself "defensive" programming habits then it can't bite you.


Trojan.
 
cool. thx for the help - there *should* be only 1 process id that is returned. I tried to really narrow it down by grepping 'java3' - which is VERY specific to this one process and no other running process should have this in its command. Also, the egrep -v should get rid of the grep pid. So, there should not be an array of variables that are returned.

Only 1 process id, like the highlander, there can only be one. ;-)

~polka
 
Just my 2c worth
If your writing perl why are you calling grep (twice) and awk in the system call. perl will do everything grep and awk will and more.
Building on Trojan's pipe
Code:
my $proc;
open PIPE, "ps -ef|";
while (<PIPE>)
  {
  /^\s*\w+\s+(\d+).+java3/ and $proc = $1, last;
  }
close PIPE;
print $proc, "\n";'
It's also a good start to learning perl's regular expressions. See also this site

Columb Healy
 
Hi columb, well, to be honest - the main reason is because I'm very much new to this - and really only know one way of doing it. I looked in my perl book for PIPE - and it's not in there. (It's the O'Reilly Learning Perl book). Is that more of an advanced function?

Also, a question - this isn't working for me and I can't figure out why:

my $proc = `ps -ef | grep java3 | egrep -v grep | awk '{print $2}'`;
print $proc;

what it prints here is the entire process line - when ALL i want is JUST the process id. When I run this at the command line: ps -ef | grep java3 | egrep -v grep | awk '{print $2}'

I get JUST the process id - so why is that NOT what is returning in Perl?

~p4e
 
PIPE is nothing special, it's just the name used for the filehandle associated with the open() function, it could as well be FOO or FILE or XXX or any other valid name for a filehandle.
 
Thank you kevin! That makes perfect sense. I see it now.

do you know why this:

my $proc = `ps -ef | grep java3 | egrep -v grep | awk '{print $2}'`;
print $proc;

gives me the entire process information instead of JUST the process id?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top