シェルスクリプトでシンプルな対話形式する


ファイル作成

test.sh
#!/bin/bash

function ConfirmExecution() {
  echo "スクリプトを実行しますか? y/n"
  read input

  if [ -z $input ] ; then
    ConfirmExecution
  elif [ $input = 'y' ] ; then
    echo "スクリプトを実行します。"
  else
    echo "スクリプトを終了します。"
    exit 1
  fi
}

ConfirmExecution

echo "Hello world!"

実行

./test.sh

まとめ

yを入力すると、Hello worldが出力され、
nを入力すると、その時点で処理が終了する。