もし[ xxx ]
文字列またはファイル インクルード '.' の表現方法
私はシェルを勉強するのが初めてです、助けてくれてありがとう
一致演算子を使用できます。
$ if [[ "abc.def" =~ \. ]]; then echo "yes"; else echo "no"; fi
yes
$ if [[ "abcdef" =~ \. ]]; then echo "yes"; else echo "no"; fi
no
これは、ドットが文字列の最初または最後 (または唯一) の文字である場合に一致します。ドットの両側に文字があると予想される場合は、次のことができます。
$ if [[ "ab.cdef" =~ .\.. ]]; then echo "yes"; else echo "no"; fi
yes
$ if [[ ".abcdef" =~ .\.. ]]; then echo "yes"; else echo "no"; fi
no
$ if [[ "abcdef." =~ .\.. ]]; then echo "yes"; else echo "no"; fi
no
パターン マッチングを使用することもできます。
$ if [[ "ab.cdef" == *?.?* ]]; then echo "yes"; else echo "no"; fi
yes
$ if [[ ".abcdef" == *?.?* ]]; then echo "yes"; else echo "no"; fi
no
$ if [[ "abcdef." == *?.?* ]]; then echo "yes"; else echo "no"; fi
no
パターンと正規表現の両方の良いリファレンスは、Greg の Wikiにあります。
bash
glob スタイルのパターン マッチングをサポートします。
if [[ "$file" = *?.?* ]]; then
...
fi
これはプレフィックスも想定していることに注意してください。これにより、.
および..
ディレクトリと一致しないことも保証されます。
特定の拡張子を確認する場合:
if [[ "$file" = *?.foo ]]; then
...
fi
echo "xxx.yyy" | grep -q '\.'
if [ $? = 0 ] ; then
# do stuff
fi
または
echo "xxx.yyy" | grep -q '\.' && <one statement here>
#e.g.
echo "xxx.yyy" | grep -q '\.' && echo "got a dot"