简体   繁体   中英

If condition in shell script with multiple conditions including check for null strings

What is wrong with the below syntax, I keep getting [: too many arguments

All the variables are strings, also I have introduced var3 and str3 , two make sure we are not comparing two null values, is there any other better option to deal with it

if [ "$var1" = "$var2" = "$var3" && "$str1" = "$str2" = "$str3" ]
then
echo " something"
elif [ "$var1" = "$var2" = "$var3"]
then
echo something else
elif [ "$str1" = "str2" = "str3" ]
then
echo something else
else
exit
fi
  1. You cannot do a = b = c check in a test ( [...] )
  2. You cannot use &&, || in [...] test, instead, use -a (and) -o (or)

so fix your codes as something like:

if [ "$a" = "$b" -a "$a" = "$c" -a "$d" = "$x" ]; then
...

If you want to use &&, || you could use [[...]] condition expression evaluation, which is bash built-in

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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