Inline if shell script

后端 未结 5 1951
一向
一向 2021-02-02 06:12

Is it possible to execute shell script in command line like this :

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


        
5条回答
  •  情深已故
    2021-02-02 06:13

    Yes, with syntax issues fixed

    That almost worked. The correct syntax is:

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

    But note that in an expression of this sort involving ps and grep, the grep will usually match itself because the characters "grep -c Myapplication" show up in the ps listing. There are several ways around that, one of them is to grep for something like [M]yapplication.

提交回复
热议问题