我正在編寫一個腳本,其中使用邏輯“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