3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シェルスクリプト case文 書き方(3つの例)

Last updated at Posted at 2019-11-22

1. いろんなパターンの指定のしかた

case-example1.sh
arg=$1
case $arg in
  [a-z])
    echo "英小文字1文字"
    ;;
  ?)
    echo "1文字"
    ;;
  japan*)
    echo "japanではじまる"
    ;;
  *)
    echo "その他"
esac
$ ./case-example.sh a
英小文字1文字
$ ./case-example.sh 1
1文字
$ ./case-example.sh japan-color
japanではじまる
$ ./case-example.sh other
その他

2. つぎの条件内の処理も実行 ;&

var=my
case $var in
  my)
    echo "my"
    ;&
  you)
    echo "you"
esac

一度条件にマッチしたら、つぎは条件にマッチするかに関わらず実行される。

my
you

3. つぎの条件も判定 ;;&

str="your"
case $str in
  you*)
    echo "youではじまる"
    ;;&
  my)
    echo "my"
    ;;&
  your)
    echo "your"
esac

条件にマッチしていたら、つぎの分も実行される。

youではじまる
your
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?