bash bash脚本中的逻辑运算符问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6583126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:18:00  来源:igfitidea点击:

Problem with logical operator in bash script

bashunix

提问by John

I read in tldp.com that

我在 tldp.com 上读到

if [ $condition1 ] && [ $condition2 ]

Same as: if [ $condition1 -a $condition2 ]

Returns true if both condition1 and condition2 hold true..."

if [ $condition1 ] && [ $condition2 ]

同: if [ $condition1 -a $condition2 ]

如果条件 1 和条件 2 都为真,则返回真......”

but when I tried

但是当我尝试

if [ $a == 2 ] || [ $b == 4 ]
then
echo "a or b is correct"
else
echo "a and b are not correct"
fi

it gives error. I'm using bash.

它给出了错误。我正在使用 bash。

回答by nickjb

Your logic is ok but your comparison operators are incorrect, you should use the '-eq' for comparing integers and '==' for strings. See 'man test' for quick reference, though it's also documented in 'man bash'.

您的逻辑没问题,但您的比较运算符不正确,您应该使用 '-eq' 来比较整数,使用 '==' 来比较字符串。请参阅“man test”以获取快速参考,尽管它也记录在“man bash”中。

When using integer comparison it is always best to initialise variables to 0 as well otherwise if they remain unset you will get errors.

使用整数比较时,最好将变量也初始化为 0,否则如果它们保持未设置,您将得到错误。

As mentioned by c00k, use [[ rather than [ if using bash as it is a builtin so bash will not need to shell out to use the /usr/bin/[ command.

正如 c00k 所提到的,使用 [[ 而不是 [ 如果使用 bash 因为它是内置的所以 bash 不需要外壳来使用 /usr/bin/[ 命令。

i.e.

IE

a=0;b=0
# do something else with a or b
if [[ $a -eq 2 ]] || [[ $b -eq 4 ]]
then
    echo "a or b is correct"
else
    echo "a and b are not correct"
fi

回答by c00kiemon5ter

If you're using Bash, then drop the single [and use double ones [[.
For arithmetic operations, use ((.

如果您正在使用Bash,则删除单个[并使用双重[[
对于算术运算,请使用((.

So you'd want to write this:

所以你想写这个:

if (( a == 2 )) || (( b == 4 )); then
    echo "foo"
fi # etc

回答by bmk

Did you assign a value to aand b? If not you have to (otherwise you definitely should) quote your variables with double quotes:

你给a和赋值了b吗?如果不是,你必须(否则你绝对应该)用双引号引用你的变量:

if [ "$a" == "2" ] || [ "$b" == "4" ]; then
  echo "a or b is correct";
else
  echo "a and b are not correct";
fi

回答by Mubeen

Correct me if I'm wrong. && and || are bash comparison

如果我错了纠正我。&& 和 || 是 bash 比较

#!/usr/bin/ksh
set -x    ###### debug mode on 

while :
do
dt=`date '+%M'`

if ([ "$dt" -ge "15"] && [ "$dt" -le "17" ]) || ([ "$dt" -ge "25" ] && [ "$dt" -le "27" ]) 

then

echo "Time-->minutes between 15 to 17 OR 25 to 27"

else

echo "Time--> minutes out of range"
fi

sleep 300 ##### sleep for 5 minutes

done

------------- below lines are debug output

------------- 下面几行是调试输出

+ + date +%M

dt=17

+ [ 17 -ge 15 ]

+ [ 17 -le 17 ]

+ echo Time--> minutes between 15 to 17 OR 25 to 27

Time--> minutes between 15 to 17 OR 25 to 27

+ date

Sat Mar  3 15:17:23 AST 2012

+ sleep 300

>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

+ :

+ + date +%M

dt=22

+ [ 22 -ge 15 ]

+ [ 22 -le 17 ]

+ [ 22 -ge 25 ]

+ echo Time--> minutes out of range

Time--> minutes out of range

+ sleep 300

^C$    ######## breaking out of loop(terminating the execution).

$ 
$ env |grep -i shell

+ grep -i shell

+ env

SHELL=/bin/ksh

$ set +x   ##### ending debug mode.