天天看點

SHELL下獲得指定程序的程序号,并截取為整數

柳鲲鵬

有時候檢視程序,需要根據程序名得到程序号。

PROCESS_NAME=gh_main
text=`ps -A | grep $PROCESS_NAME`
# 去掉開頭的空格
text=`echo $text | sed -e 's/^[ \t]*//g'`
 
# 得到程序号之後的空格
pos=`expr index "$text" " "`
pos=`expr $pos - 1`
 
#截取程序号
pid=`echo $text | cut -c 1-$pos`
echo pid=---$pid+++      

也可以參考:

https://blog.csdn.net/quantum7/article/details/88660859