if else 문

if else 문

일부 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

명확한지는 모르겠지만 break 및 종료를 사용하면 루프가 중단될 수 있습니다.

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

관련 정보