Skip to content

Instantly share code, notes, and snippets.

@na0AaooQ
Last active October 15, 2016 19:21
Show Gist options
  • Save na0AaooQ/043fab2f787ba102c64f3702bfd94cd5 to your computer and use it in GitHub Desktop.
Save na0AaooQ/043fab2f787ba102c64f3702bfd94cd5 to your computer and use it in GitHub Desktop.
bashでユーザのキーボード入力を対話的に受け取るbashスクリプト ref: http://qiita.com/na0AaooQ/items/f2759c9b2c49d2210265
#!/bin/bash
## ----------------------------
# ユーザからのキーボードの入力を受け取り、
# yes と入力されたらスクリプトを実行する、no と入力されたらスクリプトを終了します.
## ----------------------------
function ConfirmExecution() {
echo "----------------------------"
echo "スクリプトを実行しますか?"
echo " 実行する場合は yes、実行をキャンセルする場合は no と入力して下さい."
read input
if [ -z $input ] ; then
echo " yes または no を入力して下さい."
ConfirmExecution
elif [ $input = 'yes' ] || [ $input = 'YES' ] || [ $input = 'y' ] ; then
echo " スクリプトを実行します."
elif [ $input = 'no' ] || [ $input = 'NO' ] || [ $input = 'n' ] ; then
echo " スクリプトを終了します."
exit 1
else
echo " yes または no を入力して下さい."
ConfirmExecution
fi
}
# シェルスクリプトの実行を継続するか確認します。
ConfirmExecution
echo "----------------------------"
echo "hello world!"
[ec2-user@example-ruby-sinatra-server ~]$ bash --version
GNU bash, バージョン 4.2.46(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
ライセンス GPLv3+: GNU GPL バージョン 3 またはそれ以降 <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[ec2-user@example-ruby-sinatra-server ~]$
[ec2-user@example-ruby-sinatra-server ~]$ vi example.sh
[ec2-user@example-ruby-sinatra-server ~]$ chmod 755 example.sh
[ec2-user@example-ruby-sinatra-server ~]$
[ec2-user@example-ruby-sinatra-server ~]$ ./example.sh
----------------------------
スクリプトを実行しますか?
実行する場合は yes、実行をキャンセルする場合は no と入力して下さい.
yes
スクリプトを実行します.
----------------------------
hello world!
[ec2-user@example-ruby-sinatra-server ~]$
[ec2-user@example-ruby-sinatra-server ~]$ ./example.sh
----------------------------
スクリプトを実行しますか?
実行する場合は yes、実行をキャンセルする場合は no と入力して下さい.
no
スクリプトを終了します.
[ec2-user@example-ruby-sinatra-server ~]$
[ec2-user@example-ruby-sinatra-server ~]$ ./example.sh
----------------------------
スクリプトを実行しますか?
実行する場合は yes、実行をキャンセルする場合は no と入力して下さい.
test
yes または no を入力して下さい.
----------------------------
スクリプトを実行しますか?
実行する場合は yes、実行をキャンセルする場合は no と入力して下さい.
yes または no を入力して下さい.
----------------------------
スクリプトを実行しますか?
実行する場合は yes、実行をキャンセルする場合は no と入力して下さい.
no
スクリプトを終了します.
[ec2-user@example-ruby-sinatra-server ~]$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment