How to do a logical OR operation in shell scripting

前端 未结 8 2117
小蘑菇
小蘑菇 2020-11-27 08:55

I am trying to do a simple condition check, but it doesn\'t seem to work.

If $# is equal to 0 or is greater than 1 then say he

相关标签:
8条回答
  • 2020-11-27 09:56

    And in Bash

     line1=`tail -3 /opt/Scripts/wowzaDataSync.log | grep "AmazonHttpClient" | head -1`
     vpid=`ps -ef|  grep wowzaDataSync | grep -v grep  | awk '{print $2}'`
     echo "-------->"${line1}
        if [ -z $line1 ] && [ ! -z $vpid ]
        then
                echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` :: 
                "Process Is Working Fine"
        else
                echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` :: 
                "Prcess Hanging Due To Exception With PID :"${pid}
       fi
    

    OR in Bash

    line1=`tail -3 /opt/Scripts/wowzaDataSync.log | grep "AmazonHttpClient" | head -1`
    vpid=`ps -ef|  grep wowzaDataSync | grep -v grep  | awk '{print $2}'`
    echo "-------->"${line1}
       if [ -z $line1 ] || [ ! -z $vpid ]
        then
                echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` :: 
                "Process Is Working Fine"
        else
                echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` :: 
                "Prcess Hanging Due To Exception With PID :"${pid}
      fi
    
    0 讨论(0)
  • 2020-11-27 09:59

    From Bash Reference Manual → 3.4.2 Special Parameters

    #
    ($#) Expands to the number of positional parameters in decimal.

    Therefore, $# will always be either 0 or a bigger integer.

    So if you want to do something whenever $# is either 0 or bigger than 1, you just have to check if $# is or is not 1:

    [ $# -eq 1 ] && echo "1 positional param" || echo "0 or more than 1"
    

    This uses the syntax:

    [ condition ] && {things if true} || {things if false}
    
    0 讨论(0)
提交回复
热议问题