Bash read input in case statement not working as expected


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash read input in case statement not working as expected
Prev   Next
# 1  
Old 10-05-2018
Bash read input in case statement not working as expected

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.

Code:
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.

Code:
+ 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
+ read defaultans  < script sits here and doesn't read the given defaultans above.


##Once I hit enter one time the script proceeds as expected.
+ 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

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input and output as well as when displaying code segments.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Why this script is not working as 'expected' when doing ssh with while read ... really confused?

Hi, I have a script below that is running ssh <host> <command> on some servers. Below is more or less the script. I have to modify it somehow to get rid of the 'confidential' hostnames check_log.bash #!/bin/bash # myPID=$$ parse_log () { sub="parse_log" host=${1} ... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

BASH - case statement

Hi Gurus, I have the below BASH code which does not works for upper case alphabets except Z (upper case Z). What may be the reason. Also escape sequences like \n, \t, \b, \033(1m \033(0m (For bold letter) are not working. case $var in ) echo "Lower case alphabet" ;; ... (7 Replies)
Discussion started by: GaneshAnanth
7 Replies

3. Shell Programming and Scripting

Case statement not working as expected

case "$freq" in " Hz") low=250; high=550;; "8 Hz") low=250; high=1000;; " Hz") low=400; high=1000;; "63 Hz") low=550; high=1000;; " Hz") low=400; high=550;; ... (2 Replies)
Discussion started by: Michael Stora
2 Replies

4. Shell Programming and Scripting

If statement with [[ ]] and regex not working as expected

Using BASH: $ if -- ::00" ]]; then echo "true"; else echo "false"; fi false Mike (5 Replies)
Discussion started by: Michael Stora
5 Replies

5. Shell Programming and Scripting

Problem using bash case statement

I have the following bash script and it is not accepting the lines "--"|"--""-") "--""-"") while do echo "Current Argument is ${1}" case "$1" in "--"|"--""-") echo "Argument is ${1}" shift # Skip ahead one to the next argument. ... (1 Reply)
Discussion started by: kristinu
1 Replies

6. Shell Programming and Scripting

Read command not working as expected

I was trying to write a simple script which will read a text file and count the number of vowels in the file. My code is given below - #!/bin/bash file=$1 v=0 if then echo "$0 filename" exit 1 fi if then echo "$file not a file" exit 2 fi while read -n... (14 Replies)
Discussion started by: linux_learner
14 Replies

7. Shell Programming and Scripting

Trouble in getting user input while using CASE statement in UNIX

i want to get user input like this please tell which option to chose 1. mango 2. tango 3. rango if user chooses mango then it should execute a set of statements and again ask like this what do you want to do 1.add 2.subtract 3.exit when i choose exit it should goto my previous menu... (4 Replies)
Discussion started by: s.deepak
4 Replies

8. Shell Programming and Scripting

!!VERY URGENT!! Trouble in getting user input, while using under CASE statement in UNIX

i want to get user input like this please tell which option to chose 1. mango 2. tango 3. rango if user chooses mango then it should execute a set of statements and again ask like this what do you want to do 1.add 2.subtract 3.exit when i choose exit it should goto my previous... (1 Reply)
Discussion started by: s.deepak
1 Replies

9. Shell Programming and Scripting

redirect input within case statement?

I'm trying to run the logic below but get a `<' is not matched error message when I return a Y or y; printf "Run this ? : " read RESP case $RESP in Y|y) cat <<EOF > file today is Monday EOF ;; N|n) exit 1 ;; esac Any ideas? (2 Replies)
Discussion started by: h8mmer
2 Replies

10. Shell Programming and Scripting

bash case statement output help

greetings, I have a script that is taking input like this: a b c d aa bb aaa bbb ccc ddd and formating it to be like this: a b c d aa bb aaa bbb ccc ddd (4 Replies)
Discussion started by: adambot
4 Replies
Login or Register to Ask a Question