쉘 스크립트의 IF 조건에서 정규식을 확인할 때 ~와 ! 사이에 사용해야 하는 연산자는 무엇입니까?
아래에 공유된 두 사례 사이에 차이점이 있나요?
사례 1:
if [[ $file_date != ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]];
then
echo -e "The file_date is not in the required format";
else
echo "doing good";
fi
사례 2:
if [[ $file_date =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]];
then
echo -e "The file_date is not in the required format";
else
echo "doing good";
fi
답변1
합계를 비교하는 것은 사과와 오렌지를 비교하는 것과 !=
같습니다 =~
.
!=
대략적으로 "같지 않음"을 의미하고 =~
"일치"를 의미합니다. [[ expression ]]
이 연산자에 대한 자세한 내용은 에서 확인할 수 있습니다 man bash
. 다음은 가장 관련성이 높은 부분을 발췌한 것입니다.
When the == and != operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described below under Pattern Matching, as if the extglob shell option were enabled. The = operator is equivalent to ==. If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters. The return value is 0 if the string matches (==) or does not match (!=) the pattern, and 1 otherwise. Any part of the pattern may be quoted to force the quoted portion to be matched as a string. An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex(3)). The return value is 0 if the string matches the pattern, and 1 otherwise. If the regular expression is syntactically incorrect, the conditional expression's return value is 2.