简体   繁体   English

Bash - 复杂的 if 条件语句,结合 && 和 || 一起测试算术和字符串

[英]Bash - complex if condition statement, combining && and || to test arithmetic and string together

I am trying to test few things together.我正在尝试一起测试一些东西。 but my if statement is throwing an error saying:但我的 if 语句抛出一条错误消息:

./script1.sh: line 30: expected `)'
./script1.sh: line 30: syntax error near `0)'
./script1.sh: line 30: `  if [[ (($1 < 0) || ("$1" == "-0")) && ($3 >= 0) ]]'

my actual code is:我的实际代码是:

#!/bin/bash
sub(){      
if [[ (($1 < 0) || ("$1" == "-0")) && ($3 >= 0) ]]
then
 echo "Condition is true"
else
  echo "Condition is false"
fi
}
A=-0
B=50
C=-0
D=55
sub "$A" "$B" "$C" "$D"

can anyone suggest how to fix such issue?谁能建议如何解决此类问题?

In bash, the following should work:在 bash 中,以下应该起作用:

if [[ (($1 -lt 0) || ("$1" == "-0")) && ($3 -ge 0) ]]; then
    # Do something
else
    # Do something
fi

Are you sure you're running this with bash?您确定要使用 bash 运行此程序吗? Not all older shells support [[ .并非所有旧 shell 都支持[[

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM