what is the purpose of below specially "-z" string checking, how?
pid=`ps ax |grep java`
if [ ! -z $pid ]
what is the purpose of below specially "-z" string checking, how?
pid=`ps ax |grep java`
if [ ! -z $pid ]
It checks if the length of the string is zero or not.
In your case they want to perform some actions if PID is not forund for a java process
if [ ... ]
is the same as
if test ...
So you can read about it in
man bash
or
man test
There are some glitches in your sample. The grep for processes can report the grep process itsself. A fix:
pid=`ps ax |grep '[j]ava'`
Certain values of pid can cause a syntax error. Fix:
if [ ! -z "$pid" ]
On many systems you'll have the much safer pgrep to check for the existence of a program, too.
if pgrep java >/dev/null
then
echo "java is there"
else
echo "java is not there"
fi