AnDeriensのブログ

個人的なブログです

bashで正規表現を使ってどれか一文字にマッチさせる

シェルスクリプト難しい…。

結論

    pattern=[ABC]
    if [[ "$i" == $pattern ]]; then
        echo "$i mathces pattern $pattern."
    else
        echo "$i does not mathces pattern $pattern."
    fi

その他

#! /bin/bash

for i in A B C ABC "[A]"
do
    if [ "$i" = 'A' ]; then
        echo "$i is A."
    else
        echo "$i is not A."
    fi


    if [[ "$i" = 'A' ]]; then
        echo "$i is A."
    else
        echo "$i is not A."
    fi

    pattern=[ABC]
    if [ "$i" == $pattern ]; then
        echo "$i mathces extended $pattern."
    else
        echo "$i does not mathces extended $pattern."
    fi

    if [[ "$i" == $pattern ]]; then
        echo "$i mathces pattern $pattern."
    else
        echo "$i does not mathces pattern $pattern."
    fi

    echo -e ""
done

参考

新しいシェルプログラミングの教科書

新しいシェルプログラミングの教科書