bash의 케이스 조건 내 if 조건을 기반으로 오류가 발생하는 방법을 찾고 있습니다. 예를 들면 다음과 같습니다.
input="foo"
VAR="1"
case $input in
foo)
    if [ $VAR = "1" ]; then
        # perform fallthrough
    else
        # do not perform fallthrough
    fi
;;
*)
    echo "fallthrough worked!"
;;
esac위의 코드에서 변수 VAR가 1인 경우 사례 조건이 폴 스루를 수행하도록하고 싶습니다.
답변
당신은 할 수 없습니다. case넘어지는 방법 은 ;;분리기를 ;&(또는 ;;&) 로 바꾸는 것 입니다. 그리고 그것을 if 안에 넣는 것은 구문 오류입니다.
전체 논리를 규칙적인 조건부로 작성할 수 있습니다.
if [ "$input" != "foo" ] || [ "$VAR" = 1 ]; then
    one branch ...
else   # $input = "foo" && $VAR != 1
    another branch...
fi답변
당신의 논리를 재구성하는 것이 좋습니다 : 대신에 “fallthrough”코드를 함수에 넣으십시오 :
fallthrough() { echo 'fallthrough worked!'; }
for input in foo bar; do
    for var in 1 2; do
        echo "$input $var"
        case $input in
            foo)
                if (( var == 1 )); then
                    echo "falling through"
                    fallthrough
                else
                    echo "not falling through"
                fi
            ;;
            *) fallthrough;;
        esac
    done
done출력
foo 1
falling through
fallthrough worked!
foo 2
not falling through
bar 1
fallthrough worked!
bar 2
fallthrough worked!답변
다음 스크립트는 $var먼저 테스트한다는 의미에서 테스트를 “내부”로 전환 한 다음 에 따라 (을 사용 ;&하여 case) 폴 스루를 수행합니다 $input.
여부의 질문 “를 위해 fallthrough을 수행”때문에 우리는이 작업을 수행에 정말에만 의존하는 $input경우 $var입니다 1. 그것이 다른 가치라면, 실패를 할 것인지에 대한 질문은 요구 될 필요조차 없습니다.
#/bin/bash
input='foo'
var='1'
case $var in
    1)
        case $input in
            foo)
                echo 'perform fallthrough'
                ;&
            *)
                echo 'fallthough worked'
        esac
        ;;
    *)
        echo 'what fallthrough?'
esac또는없이 case:
if [ "$var" -eq 1 ]; then
    if [ "$input" = 'foo' ]; then
        echo 'perform fallthrough'
    fi
    echo 'fallthough worked'
else
    echo 'what fallthrough?'
fi답변
내가 할 일이 아니지만 다음과 같이 접근 할 수 있습니다.
shopt -s extglob # for !(*)
default='*'
case $input in
  (foo)
    if [ "$VAR" = 1 ]; then
      echo going for fallthrough
    else
      echo disabling fallthrough
      default='!(*)'
    fi ;;&
  ($default)
    echo fallthrough
esac답변
두 변수를 한 번에 테스트하십시오 (bash 4.0-alpha +).
#!/bin/bash
while (($#>1)); do
    input=$1    VAR=$2
    echo "input=${input} VAR=${VAR}"; shift 2
    if [ "$VAR" = 1 ]; then new=1; else new=0; fi
    case $input$new in
    foo0)   echo "do not perform fallthrough"   ;;
    foo*)   echo "perform fallthrough"          ;&
    *)      echo "fallthrough worked!"          ;;
    esac
    echo
done테스트 중 :
$ ./script foo 0   foo 1   bar baz
input=foo VAR=0
do not perform fallthrough
input=foo VAR=1
perform fallthrough
fallthrough worked!
input=bar VAR=baz
fallthrough worked!깨끗하고 간단합니다.
테스트 된 값 ( $new)에는 VAR을 부울 값으로 변환하기 위해 두 개의 가능한 값만 있어야하므로 if 절이있는 이유를 이해하십시오. VAR은 대한 다음 시험 부울,로 할 수있는 경우 0(하지 1의) case과를 제거합니다 if.
답변
폴 스루 기본값을 설정할 수 있지만 조건이 충족되는 경우에만 코드가 실행되는 조건을 배치하십시오.
#!/bin/bash
input='foo'
var='1'
case $input in
foo)
        echo "Do fall through"
;& #always fall through
*)
        if [ $var = "1" ] #execute only if condition matches
        then
        echo "fallthrough took place"
        fi
esac그러나 ilkkachu가 제안한 것처럼 스위치 대신 조건을 사용할 수도 있습니다.
답변
누군가가 코드를 이해하지 못한다고 불평하지 않는다면 두 조건의 순서를 간단히 바꿀 수 있습니다.
input="foo"
VAR="1"
if
    case $input in
    foo)
        [ $VAR = "1" ]
    ;;
    esac
then
    echo "fallthrough worked!"
fi또는:
input="foo"
VAR="1"
case $input in
foo)
    [ $VAR = "1" ]
;;
esac &&
    echo "fallthrough worked!"간단하고 명확합니다 (적어도 나에게). case실패 자체를 지원하지 않습니다. 하지만 당신은 대체 할 수 *)와 &&후 esac는 다른 지점의 반환 값을 존중하기 위해.