一部のifステートメント内にCaseステートメントがあり、ifステートメント内でCaseステートメントを実行した後、ループは終了します。 Case ステートメントの後、配列の次の値に移動する必要があります。配列の最初の値は、ifステートメントを渡した後にケースを渡した後、配列の他の値で繰り返すのではなく、スクリプトが完了します。
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
答え1
break
そのステートメントを追加して終了するyes
ためfor-loop
、残りの配列テストを完了できません。break
「is」ケースステートメントからそれを削除してください。
いくつかの修正を適用してコードをテストしたところ、期待した結果が得られました。
#!/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
答え2
明確であるかどうかはわかりませんが、ブレークとシャットダウンを使用するとループが中断される可能性があります。
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