![条件表达式中的语法错误](https://linux22.com/image/59700/%E6%9D%A1%E4%BB%B6%E8%A1%A8%E8%BE%BE%E5%BC%8F%E4%B8%AD%E7%9A%84%E8%AF%AD%E6%B3%95%E9%94%99%E8%AF%AF.png)
我正在编写一个脚本,其中使用逻辑“OR”和逻辑“AND”语句的组合。这是脚本:
#!/bin/bash
echo "Enter the value of a"
read $a
echo "Enter the value of b"
read $b
if
[[ $a != STARTED && $b == STARTED ] || [ $b != STARTED && $a == STARTED ]]; then
echo "Either of the JVMs is not yet up, so lets wait for some more time"
i=$(($i+1))
sleep 1s
fi
并在执行时出现以下错误:
line 13: syntax error in conditional expression
line 13: syntax error near `]'
line 13: `[[ $a != STARTED && $b == STARTED ] || [ $b != STARTED && $a == STARTED ]]; then'
我正在使用 bash shell。非常感谢对此的任何帮助。
答案1
您[[
与不匹配]
。应始终以和with[[
关闭。使用:]]
[
]
if [[ $a != STARTED && $b == STARTED ]] || [[ $b != STARTED && $a == STARTED ]]; then
更好的是,因为你[[
无论如何都在使用:
if [[ ($a != STARTED && $b == STARTED) || ($b != STARTED && $a == STARTED) ]]; then
另一个错误是您在执行以下操作,直到应用格式后我才注意到:
read $a
read $b
你应该这样做:
read a
read b
对于第一种形式,$a
和$b
被 shell 替换为它们的内容,所以如果您没有在此行之前设置它们,最终的命令将是:
read
(在这种情况下,读取的值将存储在变量中REPLY
。)如果您设置了a
某些值(例如a="blah blah"
),则它看起来像:
read blah blah
答案2
代替:
[[ $a != STARTED && $b == STARTED ] || [ $b != STARTED && $a == STARTED ]]; then
我会用:
if [ $a != STARTED && $b == STARTED ] || [ $b != STARTED && $a == STARTED ]; then