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!

how to pass the return value of a cmd to a var? 1

Status
Not open for further replies.

michael3

Programmer
Aug 8, 2001
26
US
Hi Everyone,

I got a problem when I try to pass the return value of a command to a varible, and I've never got the correct value. Please help me out. The ksh shell script looks as:

# check if the application ($1 + $2) is running?
NUMRUN=$(ps -ef | grep $1 | grep $2 | grep -v grep | wc -l)
### $NUMRUN is always 0 no mater how many copies of app are running???
echo $NUMRUN

if [ $NUMRUN -ge 2 ]; then
# running
echo "App has already run\n"
exit 1
else
# not running
exit 0
fi


The sample result of the above script:
8294:/usr/local/p2000/bin> ps -ef |grep PSPG_CipStats |grep -v grep |wc -l
2
8294:/usr/local/p2000/bin>

I guess that the white spaces before '2' may
cause the problem, how can I get rid of them?


Thanks.

Michael



 
Shouldn't that be
NUMRUN=`ps -ef | grep $1 | grep $2 | grep -v grep | wc -l`

- i.e. using `` rather than ()

???
One by one, the penguins steal my sanity.
 
Hi ayjaycee,

Thanks a lot for your tip.

I did try
NUMRUN=`ps -ef | grep PSPG_CipStatsApp |grep -v grep | wc -l`, which gave me the same result, $NUMRUN IS 0.

Michael
 
ayjaycee ... $(command) is the same as doing `command`

(something very useful which I found out here not long ago)

Michael ... try this

NUMRUN=$(ps -ef | grep $1 | grep $2 | grep -v grep | wc -l | awk '{print $1}')

This should remove the leading spaces from the wc -l. I'm not convinced that's the problem though!

Greg.

 
Thanks all for your help.

Ayjaycee --:
the cmd itself gave " 2", ie, some leading spaces plus the number of processes running.

Greg --:
I've tried
NUMRUN=$(ps -ef | grep $1 | grep $2 | grep -v grep | wc -l | awk '{print $1}')
I still got value 0 for $NUMRUN.

Many, many thanks to all.

Michael





 
OK, using grega's idea, try

N=$(ps -ef | grep $1 | grep $2 | grep -v grep | wc -l)
NUMRUN=$(echo "."$N | awk '{print $2}')


which should strip out the leading blanks.

One by one, the penguins steal my sanity.
 
if on the command line you are running this...

# ps -ef | grep PSPG_CipStatsApp |grep -v grep | wc -l

and you get "2" as the result, but in the script you are running this ...

# NUMRUN=$(ps -ef | grep $1 | grep $2 | grep -v grep | wc -l | awk '{print $1}')

I would have to say that your $1 and $2 vars are not being passed correctly. If you were grepping for "nothing" you would get a 0 from wc -l.
Try running the script with
# sh -x scriptname
This will show that values of the variables as the script "sees" them.

crowe
 
The pipe ps -ef | grep $1 | grep $2
give the line of ps containing the two strings $1 AND $2.

If $2 and $2 are two differents process, the result will be null. In that case you must select lines wich contains $1 OR $2.

# check if the application (process $1 and $2) is running?
NUMRUN=$(ps -ef | egrep "$1|$2" | grep -v grep | wc -l)
### $NUMRUN is always 0 no mater how many copies of app are running???
echo $NUMRUN

if [ $NUMRUN -ge 2 ]; then
# running
echo "App has already run\n"
exit 1
else
# not running
exit 0
fi

Jean Pierre.
 
Thanks for all the tips and hints from everybody. The problem is caused by $2, which was reset by my other script, hence wc -l returned 0.

NUMRUN=$(ps -ef | grep $1 | grep $2 | grep -v grep | wc -l | awk '{print $1}') is right.

Again, thanks.

Michael

 
You can avoid the pipe to 'wc -l' and awk '{print $1}' by using the '-c' option for grep. Here's how:

NUMRUN=$(ps -ef | grep $1 | grep $2 | grep -vc grep)
 
How about simply adding the cut command?

cut -c8

your result, if a single figure is in column 8, thereforewill extract only col. 8 via the above command and you should now be in col.1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top