3
2

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.

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

Posted at

ファイル作成

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を入力すると、その時点で処理が終了する。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?