for 'loop'の 'read'入力を確認またはテストする方法はありますか?
入力が正しくない場合、Case文を実行したくありません。現時点では、正しい入力を実行しながら*)分岐を数回出力しながら繰り返します。
例:
echo "Select Your Options (Seperated by spaces): 1 2 3"
read SELECTION
# Test SELECTION here? Then exit if fail? IE, if a 4 is entered.
for INPUT in $SELECTION; do
case $INPUT in
1 ) echo "you selection option 1"
;;
2 ) echo "you selection option 2"
;;
3 ) echo "you selection option 3"
;;
* ) echo "Check your selection. Anything outside the following will make this fail: 1 2 3 "
RETVAL=1
exit
;;
esac
done
ベストアンサー1
このように?
rpt=0
until [ 5 -le "$((rpt+=1))" ] && unset rpt
printf '\nSelect your Options:(1|2|3): ' >&2
read SELECTION &&
case $SELECTION in
(*[!123\ ]*|*[123][123]*|'') ! :
esac
do : "${rpt?too many loops!}"
echo >&2 \
"Check your selection. Anything outside the following will make this fail: 1 2 3 "
done
for INPUT in $SELECTION
do case $INPUT in
(1) echo "you selection option 1"
;;
(2) echo "you selection option 2"
;;
(3) echo "you selection option 3"
;;
esac
done