1

I cannot seem to figure out how to make this loop pause for user selection. It automatically chooses the * selection every time. What am I doing wrong?

#!/bin/bash    

while read -r lineIn; do 
if [[ "$(cmd_output)" != "" ]]; then
   echo "choose one letter (a)(b)(c)(d)(e)" 
   read abcde  
      case $abcde in
         [aA] ) do stuff and continue the loop;;
         [bB] ) do stuff and do not continue the loop;;
         [cC] ) do other stuff and continue the loop
         [dD] ) do nothing and continue the loop;;
         [eE] ) exit;;  
            * ) echo "Danger Will Robinson!";;
      esac
else
   echo "No output from command!"
fi   
done < filename
5
  • 2
    1. Do you really want/need read -p ? I think just read abcde should work. 2. constructs like [a | A] "boil" down to [aA |], so I think you really just need [Aa] (etc). Otherwise, looks good. Good Luck! Commented Apr 28, 2016 at 3:11
  • The reason I added the -p option is because I originally thought it would pause to prompt for user input. But you are right, it is unnecessary. Commented Apr 28, 2016 at 3:27
  • for ksh, the read man page shows that -p is to "Read from the current co-process instead of standard input. An end of file causes read to disconnect the co-process so that another can be created.", but it may be different for bash (too tired to look right now). Good luck. Commented Apr 28, 2016 at 3:59
  • It chooses the * every time because there are no break commands at the end of the choices within the loop. Commented Apr 28, 2016 at 14:07
  • @DavidC.Rankin Having break commands at the end of the choices didn't stop the loop from running unless break was on the * choice. But doing that breaks out of the entire script. And it would automatically run any command found for choice * which could be really bad. Martin's answer has fixed the issue I was having. With read abcde < /dev/tty it waits for one the choices to be entered. But also break is needed now that it works. Commented Apr 28, 2016 at 15:26

1 Answer 1

2

You need to redirect the "read abcde" to read from the tty.

read abcde < /dev/tty

Because of the while loop the stdin is actually coming from the redirect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.