Bash read input in case statement not working as expected


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash read input in case statement not working as expected
# 1  
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.
# 2  
You have a read command at the start of your script and you have a read command inside your loop. Every read command will wait for you to type in a response.

Try replacing the first read with an echo or printf that gives the directions for the data to be entered and let the read command inside the loop be the only command that blocks waiting for data to be entered.
# 3  
Quote:
Originally Posted by woodson2
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.
There is just one loop, and it looks like the first read (outside the loop) has completed accepting the preset value "8hrs" as <return> was hit.
Quote:
if I hit enter then the scripts starts to respond as expected.
. . .
Which is the correct behaviour of read - read a line terminated by a return character.
Quote:
script sits here and doesn't read the given defaultans above.
There are no modifiers / options for this (second) read - no default value, no timeout value, no char count - so, yes, it sits there and waits for your input, terminated by a <Return>.

Last edited by RudiC; 10-05-2018 at 07:42 PM..
# 4  
Here you can avoid repeated instructions by putting them between while and do
Code:
while
    defaultans=8hrs
    read -e -i $defaultans -p "${bldwht}How long would you like to grant access to $USERNAME (8hrs,24hrs,48hrs,72hrs,96hrs,1wk,2wk or q)? ${txtrst}" input
    defaultans="${input:-$defaultans}"
    echo "       "
    echo "       "
    echo "$defaultans"
    true
do
    case $defaultans in
    8hrs ) break;;
    24hrs ) break;;
    48hrs ) break;;
    72hrs ) break;;
    96hrs ) break;;
      1wk ) break;;
      2wk ) break;;
    q ) exit;;
    * ) echo "You entered an invalid choice.";;
    esac
done

The last command "true" sets a good exit status (0) so the while loop runs forever, only terminating by the break statements.
# 5  
Scratching my head - is it wise to modify the default answer instead of working with the input read?
# 6  
Bash has a built-in menu command called select. Consider this code snippet:

Code:
words=(8hrs 24hrs 48hrs 72hrs 96hrs 1wk 2wk)
PS3="Enter a number or q to quit "
printf "How long would you like to grant access to %s?\n" "$USERNAME"
select time in "${words[@]}"
do
   [[ "$REPLY" == [qQ]* ]] && break
   case "${time:-null}" in
      8hrs) break ;;
      24hrs) break ;;
      48hrs) break ;;
      72hrs) break ;;
      96hrs) break ;;
      1wk) break ;;
      2wk) break ;;
      null)
         printf "incorrect answer. Please enter a number 1-%d\n" ${#words[@]}
         ;;
   esac
done

The major difference between this and your version is no default value. You could add one by putting this line before the case statement:
Code:
   [[ -z "$REPLY" ]] && time=8hrs

Andrew
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #976
Difficulty: Medium
In April 2014, Linus Torvalds banned Kay Sievers from submitting patches to the Linux kernel for failing to deal with bugs that caused systemd to negatively interact with the kernel.
True or False?

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

Featured Tech Videos