且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

内联if shell脚本

更新时间:2023-12-05 17:50:28

它不起作用,因为您错过了 fi 来结束 if 语句.

It doesn't work because you missed out fi to end your if statement.

counter=`ps -ef | grep -c "myApplication"`; if [ $counter -eq 1 ]; then echo "true"; fi

您可以使用以下方法进一步缩短它:

You can shorten it further using:

if [ $(ps -ef | grep -c "myApplication") -eq 1 ]; then echo "true"; fi

还请注意 ps -ef |的问题.grep ... 匹配 @DigitalRoss的答案>.

实际上,您可以使用 pgrep 做一个更好的事情:

In fact, you can do one better by using pgrep:

if [ $(pgrep -c "myApplication") -eq 1 ]; then echo "true"; fi