영어와 논리는 서로 다른 의미를 부정적인 OR로 간주합니다. 혼동을 일으키는 것은 매우 쉽습니다.
$client
에 대해 가능한 값을 생각해보십시오. 그들은 'host1', 'host2'및 'other'입니다. 값이 'host1'인 경우 ||
의 두 번째 용어는 true입니다. 'host2'인 경우 ||
의 첫 번째 용어는 참입니다. '기타'인 경우 ||
의 두 용어가 모두 참입니다. 이는 조건이 항상 참임을 의미합니다.
클라이언트가 host1과 같지 않고 host2와 다른 경우 ||
을 &&
으로 바꿉니다.
if [[ "$categorie" = "DG" || "$categorie" = "DI" ]] &&
[[ ! ("$client" = "host1" || "$client" = "host2") ]]; then
echo "requirements are met"
else
echo "requirements not met"
fi
는 테스트 스크립트로 퍼팅 :로 번역 할 수
if [[ "$categorie" = "DG" || "$categorie" = "DI" ]] &&
[[ "$client" != "host1" && "$client" != "host2" ]]; then
echo "requirements are met"
else
echo "requirements not met"
fi
또는 사용 !(client == host1 || client == host2)
for categorie in DG DI XX
do
for client in host1 host2 other
do
if [[ "$categorie" = "DG" || "$categorie" = "DI" ]] &&
[[ "$client" != "host1" && "$client" != "host2" ]]; then
echo "(categorie $categorie; client $client): requirements are met"
else
echo "(categorie $categorie; client $client): requirements not met"
fi
if [[ "$categorie" = "DG" || "$categorie" = "DI" ]] &&
[[ ! ("$client" = "host1" || "$client" = "host2") ]]; then
echo "(categorie $categorie; client $client): requirements are met"
else
echo "(categorie $categorie; client $client): requirements not met"
fi
done
done
출력 : 라인의
(categorie DG; client host1): requirements not met
(categorie DG; client host1): requirements not met
(categorie DG; client host2): requirements not met
(categorie DG; client host2): requirements not met
(categorie DG; client other): requirements are met
(categorie DG; client other): requirements are met
(categorie DI; client host1): requirements not met
(categorie DI; client host1): requirements not met
(categorie DI; client host2): requirements not met
(categorie DI; client host2): requirements not met
(categorie DI; client other): requirements are met
(categorie DI; client other): requirements are met
(categorie XX; client host1): requirements not met
(categorie XX; client host1): requirements not met
(categorie XX; client host2): requirements not met
(categorie XX; client host2): requirements not met
(categorie XX; client other): requirements not met
(categorie XX; client other): requirements not met
쌍 항상 same - 표현식은 동일합니다. 그리고 결과는 당신이 원하는 것입니다.
에 테스트를 단순화하는 패턴을 비교하는 대신 다중 비교를 수행하여 주위를 얻을 수 있습니다. 모든 변수는 따옴표로 묶어서 단어 분할과 glob 확장으로 모든 문자가 중복되는 것을 피하십시오. – Jens
@Jens 나는 이중 괄호가 이것을 막을 것이라고 생각 했습니까? –
어쩌면, 그들은 비표준 적이기도합니다 (그들이하는 bash에서, 당신 말이 맞습니다). (POSIX 표준) 단일 대괄호는 이것을 막지 않습니다. – Jens