
このスクリプトは正常に実行されていますが、タイプミスがあってスクリプトを再実行したい場合はどうすればよいですか?
#! /bin/bash
#! userInput - a script that reads in text and outputs it immediately
echo "Would you like to input some text? Y/N"
read request
if [[ $request = Y ]]; then
echo "Please input some text"
read input
echo $input
elif [[ $request = N ]]; then
echo "Thank You"
else
echo "Invalid Input - Please Input Y for yes or N for no"
fi
答え1
それがselect
目的です。
PS3="Would you like to input some text? <Y/N> ]"
select choice in "Y" "N"; do
case $choice in
"Y")
echo -n "Please input some text >"
read input
echo "$input"
break
;;
"N")
echo "Very well."
break
;;
*)
echo "Invalid response."
;;
esac
done
答え2
解決したい問題をモデル化するために制御フローを設定することをお勧めします。ユーザーが永遠に終了したくない時点を見たいです。
#!/bin/bash
echo -n "Would you like to input some text (Y/N): "
read request
while [[ "${request}" != "N" ]]; do
if [[ "${request}" == "Y" ]]; then
echo -n "Please input some text: "
read input
echo "You entered '${input}'"
else
echo "Invalid input: '${request}'"
fi
echo -n "Would you like to input some text (Y/N): "
read request
done
echo "Thank you"
答え3
どうですか?
#!/bin/bash
# userInput - a script that reads in text and outputs it immediately
while true; do
echo "Would you like to input some text? Y/N"
read request
if [[ $request = Y ]]; then
echo "Please input some text"
read input
echo $input
break
elif [[ $request = N ]]; then
echo "Thank You"
break
else
echo "Invalid Input - Please Input Y for yes or N for no"
fi
done
答え4
実際にスクリプトを再実行するには、次の手順を実行します。
else
exec $0