bashのif文では、変数の中身について真偽を判定する際にワイルドカードや正規表現が利用できる。 ワイルドカードを利用する場合、以下のようにするとよい。
TEST="test1234"
echo $TEST
if [[ $TEST == *"12"* ]]
then
echo $TEST" is ok"
fi
TEST="test4567"
echo $TEST
if [[ $TEST == *"12"* ]]
then
echo $TEST" is ok"
fi
[root@BS-PUB-CENT7-01 ~]# cat /tmp/test.sh
TEST="test1234"
echo $TEST
if [[ $TEST == *"12"* ]]
then
echo $TEST" is ok"
fi
TEST="test4567"
echo $TEST
if [[ $TEST == *"12"* ]]
then
echo $TEST" is ok"
fi
[root@BS-PUB-CENT7-01 ~]# bash /tmp/test.sh
test1234
test1234 is ok
test4567
正規表現で対応する場合は、「==」ではなく「=\~」で真偽判定ができる。
TEST="test1234"
echo $TEST
if [[ $TEST =~ .*12.* ]]
then
echo $TEST" is ok"
fi
TEST="test4567"
echo $TEST
if [[ $TEST =~ .*12.* ]]
then
echo $TEST" is ok"
fi
[root@BS-PUB-CENT7-01 ~]# cat /tmp/test.sh
TEST="test1234"
echo $TEST
if [[ $TEST =~ .*12.* ]]
then
echo $TEST" is ok"
fi
TEST="test4567"
echo $TEST
if [[ $TEST =~ .*12.* ]]
then
echo $TEST" is ok"
fi
[root@BS-PUB-CENT7-01 ~]# bash /tmp/test.sh
test1234
test1234 is ok
test4567