I am facing a strange problem while grepping for a process. Here is the small script that i have written.
It will look for any process running with the parameter passed to the script.
If no process is running it should print appropriate message.
[oracle@server]$ cat t.ksh
#!/bin/ksh
set -x
sid=$1
sid_cnt=$(ps -ef|grep -w "$sid"|grep -v "grep"|wc -l)
echo $sid_cnt
if [ $sid_cnt -eq 0 ];then
echo "sid not runing"
exit 1
else
echo "sid running"
fi
I executed the script with a parameter "abc". There is no process running as "abc". But the output of the script shows otherwise
NB the shell matches ab[c] against the files in the current directory.
If a file name abc exists it will change ab[c] to abc , and the problem will be back.
Therefore, it needs to be quoted:
$$ can be a small number.
Better grep -vw $$ .
Also some shells can spawn sub processes with a different PID, without updating $$,
then the $$ would not match grep's PPID.