Inline if shell script

后端 未结 5 1948
一向
一向 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:32

    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
    

    Also, do take note the issue of ps -ef | grep ... matching itself as mentioned in @DigitalRoss' answer.

    update

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

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

提交回复
热议问题