Inline if shell script

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

    Other responses have addressed your syntax error, but I would strongly suggest you change the line to:

    test $(ps -ef | grep -c myApplication) -eq 1 && echo true
    

    If you are not trying to limit the number of occurrences to exactly 1 (eg, if you are merely trying to check for the output line myApplication and you expect it never to appear more than once) then just do:

    ps -ef | grep myApplication > /dev/null && echo true
    

    (If you need the variable counter set for later processing, neither of these solutions will be appropriate.)

    Using short circuited && and || operators is often much clearer than embedding if/then constructs, especially in one-liners.

提交回复
热议问题