How to do a logical OR operation in shell scripting

前端 未结 8 2208
小蘑菇
小蘑菇 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:45

    If you are using the bash exit code status $? as variable, it's better to do this:

    if [ $? -eq 4 -o $? -eq 8 ] ; then  
       echo "..."
    fi
    

    Because if you do:

    if [ $? -eq 4 ] || [ $? -eq 8 ] ; then  
    

    The left part of the OR alters the $? variable, so the right part of the OR doesn't have the original $? value.

提交回复
热议问题