LoginSignup
2
3

More than 5 years have passed since last update.

bashプログラミングで確認用(confirm)関数を作ってみる

Last updated at Posted at 2017-07-01

bashプログラミングで確認用(confirm)関数を作ってみる

# 確認
function confirm() {
  while true; do
    if [ "$1" != "" ]; then
      printf '%s ' "$1"
    fi
    printf "[y/n] >"
    read input
    if [ "$input" = "y" ]; then
      return 0;
    elif [ "$input" = "n" ]; then
      return 1;
    fi
  done
}

で、OK。

編集リクエストが来たので、採用しましたが、このコードは動作確認してません。

使い方の例

confirm "OKですか?" && echo "YES" || echo "NO"

こんな感じの例文がわかりやすいかと思います。

で、上記のconfirm関数.bashrcbash_profileとかに書いておけば、どこでも使えるようになるので、bashプログラミングする人は便利だと思います。

2
3
1

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
2
3