5
5

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.

Shell : yes/no/skipを選択しながら順番に実行するシェルスクリプトのサンプル

5
Posted at
  • 3つのコマンドを1つのシェルで実行する
  • 実行後 yes, no, skip を入力して、実行、中断、スキップをすることができる
  • yesの場合はy, Y, yes, Yes, YES など、様々なパターンの入力を許可する
  • root/tool/hoge.sh
hoge.sh
# !/bin/sh

set -e

# 1つ上のディレクトリに移動する
cd $(dirname $0)/../

read -p "[1/3] Are You Ready? (y/n/skip)" ans1
case $ans1 in
    [Yy] | [Yy][Ee][Ss] )
        # TODO ここに実行コマンドを記述
        ;;
    [Ss] | skip )
        echo "Skip!";;
    * )
        echo "Quit"; exit 1;;
esac

read -p "[2/3] Are You Ready? (y/n/skip)" ans2
case $ans2 in
    [Yy] | [Yy][Ee][Ss] )
        # TODO ここに実行コマンドを記述
        ;;
    [Ss] | skip )
        echo "Skip!";;
    * )
        echo "Quit"; exit 1;;
esac

read -p "[3/3] Are You Ready? (y/n)" ans3
case $ans3 in
    [Yy] | [Yy][Ee][Ss] )
        # TODO ここに実行コマンドを記述
        ;;
    * )
        echo "Quit"; exit 1;;
esac

echo "Complete!"

※1つの選択で複数のコマンドを実行したい場合は改行して記述する

    [Yy] | [Yy][Ee][Ss] )
        cp ./aa ./bb
        chmod 755 ./bb
        rm ./aa;;

実行

$ ./tool/hoge.sh
$ [1/3] Are You Ready? (y/n/skip) y
$ [2/3] Are You Ready? (y/n/skip) s
$ Skip!
$ [3/3] Are You Ready? (y/n/skip) n
$ Quit
5
5
2

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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?