If statement 带有逻辑OR的Bash if语句
我希望命令仅在字符串变量不等于foo或以bar开头的任何单词时执行If statement 带有逻辑OR的Bash if语句,if-statement,logical-operators,If Statement,Logical Operators,我希望命令仅在字符串变量不等于foo或以bar开头的任何单词时执行 if [[ $string != "foo" || $string != bar* ]]; then commands fi 当$string=foo和我使用以下代码时,它工作正常,不执行任何命令: if [[ $string != "foo" ]]; then commands fi 但是,当我将第二部分添加为| |时,命令在不应该执行的时候执行。您需要and,而不是or: 代码的意思是“字符串不等于foo,或者字符串不以
if [[ $string != "foo" || $string != bar* ]]; then
commands
fi
当$string=foo和我使用以下代码时,它工作正常,不执行任何命令:
if [[ $string != "foo" ]]; then
commands
fi
但是,当我将第二部分添加为| |时,命令在不应该执行的时候执行。您需要and,而不是or:
代码的意思是“字符串不等于foo,或者字符串不以bar开头,这必须是真的”——这对所有字符串都是真的
您需要说的是“字符串不等于foo,和字符串不以bar开头,这必须是真的”
(如果有可能,
$string
可能是空的,您可能还需要在其周围加上双引号。)您需要&&而不是| |。想想看。
if [[ $string != "foo" && $string != bar* ]] ...