Sponsored Content
Top Forums Shell Programming and Scripting Bash read input in case statement not working as expected Post 303024291 by woodson2 on Friday 5th of October 2018 10:04:34 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
CAT(1)							      General Commands Manual							    CAT(1)

NAME
cat, read, nobs - catenate files SYNOPSIS
cat [ file ... ] read [ -m ] [ -n nline ] [ file ... ] nobs [ file ... ] DESCRIPTION
Cat reads each file in sequence and writes it on the standard output. Thus cat file prints a file and cat file1 file2 >file3 concatenates the first two files and places the result on the third. If no file is given, cat reads from the standard input. Output is buffered in blocks matching the input. Read copies to standard output exactly one line from the named file, default standard input. It is useful in interactive rc(1) scripts. The -m flag causes it to continue reading and writing multiple lines until end of file; -n causes it to read no more than nline lines. Read always executes a single write for each line of input, which can be helpful when preparing input to programs that expect line-at-a- time data. It never reads any more data from the input than it prints to the output. Nobs copies the named files to standard output except that it removes all backspace characters and the characters that precede them. It is useful to use as $PAGER with the Unix version of man(1) when run inside a win (see acme(1)) window. SOURCE
/src/cmd/cat.c /src/cmd/read.c /bin/nobs SEE ALSO
cp(1) DIAGNOSTICS
Read exits with status eof on end of file or, in the -n case, if it doesn't read nlines lines. BUGS
Beware of and which destroy input files before reading them. CAT(1)
All times are GMT -4. The time now is 04:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy