
シェルスクリプトのIF条件で正規表現を確認するとき〜と!間で使用する必要がある演算子は何ですか?
以下に共有されている2つのケースの間に違いはありますか?
ケース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.