Shell
if else 語句
我在一些 if 語句中有一個 case 語句,但是在 if 語句中執行 case 語句後,循環結束。在 case 語句之後,它應該轉到數組上的下一個值。發生的是數組中的第一個值,在通過 if 語句 then case 之後,腳本完成而不是循環到數組中的其他值。
set -A arrs a b c d num=`expr ${#arrs[*]} - 1` for x in `seq 0 $num` do var1=`some command here` var2=`some command here` var3=`some command here` if [[ "$var1" == "$var2" && "$var3" == "0" ]]; then #do something here elif [[ "$var1" == "$var2" && "$var3" != "0" ]]; then echo "\nContinue?" echo "[y/n]:\c" read yes_no case $yes_no in [yY]) echo "You answer yes." echo "Some text that will go to the text file" >> some_text.txt break ;; [nN]) echo "you answered no" exit ;; *) echo "\nTry again" ;; esac elif [[ "$var1" != "$var2" && "$var3" == "0" ]]; then echo "\nContinue?" echo "[y/n]:\c" read no_yes case $no_yes in [yY]) echo "You answer yes" echo "Some text that will go to the text file" >> some_text.txt break ;; [nN]) echo "you answered no" exit ;; *) echo "\nTry again" ;; esac else echo "Do back flip" exit fi done
我不知道是否清楚,但是當您使用 break 和 exit 時,您可能會中斷循環
set -A arrs a b c d num=`expr ${#arrs[*]} - 1` for x in `seq 0 $num` do var1=`some command here` var2=`some command here` var3=`some command here` if [[ "$var1" == "$var2" && "$var3" == "0" ]] then #do something here elif [[ "$var1" == "$var2" && "$var3" != "0" ]] then echo "\nContinue?" echo "[y/n]:\c" read yes_no case $yes_no in [yY]) echo "You answer yes." echo "Some text that will go to the text file" >> some_text.txt break ;; [nN]) echo "you answered no" exit ;; *) echo "\nTry again" ;; esac elif [[ "$var1" != "$var2" && "$var3" == "0" ]] then echo "\nContinue?" echo "[y/n]:\c" read no_yes case $no_yes in [yY]) echo "You answer yes" echo "Some text that will go to the text file" >> some_text.txt break ;; [nN]) echo "you answered no" exit ;; *) echo "\nTry again" ;; esac else echo "Do back flip" exit fi done
您
break
在yes
語句中添加,因此它正在退出for-loop
,因此未完成對數組其餘部分的測試。只需break
從“是”案例聲明中刪除即可。我對您的程式碼進行了一些修改進行了測試,並得到了我認為是您預期的結果:
#!/bin/sh arrs=(a b c d) num=`expr ${#arrs[*]} - 1` for x in `seq 0 $num` do var1=`echo "hi"` var2=`echo "hi"` var3=`echo "hi"` echo "var1 equals: $var1" echo "var2 equals: $var2" echo "var3 equals: $var3" if [[ "$var1" = "$var2" && "$var3" = 0 ]] then #do something here echo "First check" elif [[ "$var1" == "$var2" && "$var3" != 0 ]] then echo "\nContinue?" echo "[y/n]:\c" read yes_no case $yes_no in [yY]) echo "You answer yes." echo "Some text that will go to the text file" >> some_text.txt ;; [nN]) echo "you answered no" exit ;; *) echo "\nTry again" ;; esac elif [[ "$var1" != "$var2" && "$var3" == 0 ]] then echo "\nContinue?" echo "[y/n]:\c" read no_yes case $no_yes in [yY]) echo "You answer yes" echo "Some text that will go to the text file" >> some_text.txt ;; [nN]) echo "you answered no" exit ;; *) echo "\nTry again" ;; esac else echo "Do back flip" exit fi done
輸出(是測試):
[root]# sh test.sh var1 equals: hi var2 equals: hi var3 equals: hi \nContinue? [y/n]:\c y You answer yes. var1 equals: hi var2 equals: hi var3 equals: hi \nContinue? [y/n]:\c y You answer yes. var1 equals: hi var2 equals: hi var3 equals: hi \nContinue? [y/n]:\c y You answer yes. var1 equals: hi var2 equals: hi var3 equals: hi \nContinue? [y/n]:\c y You answer yes.
輸出(無測試):
[root]# sh test.sh var1 equals: hi var2 equals: hi var3 equals: hi \nContinue? [y/n]:\c n you answered no