1
0

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.

シェルスクリプト bash select 文

Posted at

PS3 はシェル変数で select 文において入力を促すメッセージとなる。

select
PS3='Please select.'
select num in 1 2 3 4 5 end
do
  case $num in
    1)
      echo 'Selected number 1.';;
    2)
      echo 'Selected number 2.';;
    3)
      echo 'Selected number 3.';;
    4)
      echo 'Selected number 4.';;
    5)
      echo 'Selected number 5.';;
    end)
      echo 'Good Bye.'
      break;;
    *)
      echo "{REPLY}"
      echo 'Not valid, input again.';;
  esac
  echo
done

実行すると、以下のように番号で選択するように要求される。
以下は3番を入力した例。

1) 1
2) 2
3) 3
4) 4
5) 5
6) end
Please select.3
Selected number 3.

番号ではなく、関係のない文字列を入力した例。
入力した文字はシェル変数 REPLY に格納されるため、それを元に条件分岐させることも可能。

Please select.ABC 
ABC
Not valid, input again.

bash に依存(shでは動かない)しているため、積極的には使わないほうがよさそう。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?