if else 语句

if else 语句

我在一些 if 语句内有一个 case 语句,但是在 if 语句内执行了 case 语句后,循环结束。在 case 语句之后,应该转到数组上的下一个值。发生的情况是数组中的第一个值,在通过 if 语句 then 情况后,脚本完成而不是循环到数组中的其他值。

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

breakyes语句中添加了该语句,因此它正在退出for-loop,因此无法完成对数组其余部分的测试。只需break从“是”的 case 语句中删除即可。

我对您的代码进行了一些修改进行了测试,并得到了我认为是您预期的结果:

#!/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

我不知道是否清楚,但当您使用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

相关内容