入力パラメータのパターン一致

入力パラメータのパターン一致

私たちはスクリプトの1つを改善しようとしています。

ユーザーはいくつかのパラメータを渡し、いくつかのパラメータは5.0.3Jboss5.0.3GAJboss5.0.3GAには「5.0.3」があるので、インストールバイナリ「Jboss5.0.3GA.tar」を探してみましょう。

現在のスクリプトはkshスクリプトです。ifスクリプトで条件を使用しようとしています。

ユースケースと結果の例:

./test.sh Jboss5.0.3GA
Match found... we'll try to locate the installation binary
./test.sh Jboss5.0.3
Match found... we'll try to locate the installation binary
./test.sh 5.0.3
Match found... we'll try to locate the installation binary
./test.sh Jboss5.1.3
No Match found ... we'll be exiting the script.

ベストアンサー1

POSIXシェルのパターンマッチングはcaseこの設定で行われます。kshまた、演算子([[ x = pattern ]]and にもコピーされます)と最新バージョンです。bashzsh[[ x =~ regexp ]]

だから:

case $1 in
  (*5.0.3*)
    install=$1.tar
    echo Found;;
  (*)
    echo >&2 Not found
    exit 1;;
esac

おすすめ記事