デフォルトでYes
sample.sh
#!/bin/bash
echo -n "実行しますか? [Y/n]: "
read ANS
case $ANS in
"" | [Yy]* )
# 「Yes」の場合の処理
;;
* )
# 「No」の場合の処理
;;
esac
デフォルトでNo
sample.sh
#!/bin/bash
echo -n "実行しますか? [y/N]: "
read ANS
case $ANS in
[Yy]* )
# 「Yes」の場合の処理
;;
* )
# 「No」の場合の処理
;;
esac
必ず入力させる
sample.sh
#!/bin/bash
function ask_yes_no {
while true; do
echo -n "$* [y/n]: "
read ANS
case $ANS in
[Yy]*)
return 0
;;
[Nn]*)
return 1
;;
*)
echo "yまたはnを入力してください"
;;
esac
done
}
if ask_yes_no "実行しますか?"; then
# 「Yes」の場合の処理
else
# 「No」の場合の処理
fi