I’m having an issue with bash read input when using a case statement.
The script halts and doesn’t read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here.
defaultans=8hrs
read -e -i $defaultans -p "${bldwht}How long would you like to grant access to $USERNAME (8hrs,24hrs,48hrs,72hrs,96hrs,1wk or 2wk)? ${txtrst}" input
defaultans="${input:-$defaultans}"
#echo -e "${bldwht}How long would you like to grant access to $USERNAME (8hrs,24hrs,48hrs,72hrs,96hrs,1wk or 2wk)? ${txtrst}"
echo " "
echo " "
while true; do
read defaultans
case $defaultans in
8hrs ) break;;
24hrs ) break;;
48hrs ) break;;
72hrs ) break;;
96hrs ) break;;
1wk ) break;;
2wk ) break;;
* ) echo "You entered an invalid choice. You must choose 8hrs,24hrs,48hrs,72hrs,96hrs,1wk or 2wk access.";;
esac
done
Script output in debug.
+ defaultans=8hrs
+ read -e -i 8hrs -p 'How long would you like to grant access to tnew (8hrs,24hrs,48hrs,72hrs,96hrs,1wk or 2wk)? ' input
How long would you like to grant access to tnew (8hrs,24hrs,48hrs,72hrs,96hrs,1wk or 2wk)? 8hrs
+ defaultans=8hrs
+ echo ' '
+ echo ' '
+ true
[B]+ read defaultans < script sits here and doesn't read the given defaultans above.
[/B]
[B][U]##Once I hit enter one time the script proceeds as expected.
[/U][/B]+ case $defaultans in
+ echo 'You entered an invalid choice. You must choose 8hrs,24hrs,48hrs,72hrs,96hrs,1wk or 2wk access.'
You entered an invalid choice. You must choose 8hrs,24hrs,48hrs,72hrs,96hrs,1wk or 2wk access.
+ true
+ read defaultans