bash - Why does my menu with select fail the first time? -


i tried answer another question simple menu using builtin select statement. code displays names /etc/passwd, , let select name giving number:

ps3="enter number: " select name in $(cut -d: -f1 /etc/passwd) ;    if [ -n "${name}" ];       break    fi    echo "sorry, please enter number shown." done echo "entry passwd is: ${name}" 

the works fine except first time. when give correct answer first time ask try again.
tried more detailed explanation of first time, couldn't reproducable cook-book. when copy-paste code on server, , give correct answer have same problem. when repeat command (from history or new paste), code shows problem. tried problem again logging out , logging in (sometimes works) or rebooting.
tried different ways reproduce problem in other situations (using different variable names, unsetting variables, using slow list of values select name in $(echo one; sleep 1; echo two; sleep 2; echo three; sleep 1); , opening new shell.

i searched other examples select, can't find clues in other posts https://stackoverflow.com/a/16750755/3220113 , https://askubuntu.com/a/1716.

i tried fix code sync , seems work-around:

ps3="enter number: " select name in $(cut -d: -f1 /etc/passwd) ;    # sync needed here?    sync    if [ -n "${name}" ];       break    fi    echo "sorry, please enter number shown." done echo "entry passwd is: ${name}" 

i couldn't reproduce error when include sync command. sync working patch , why need here?

i not need other ways write menu. found graphical dialog dialog bash script , looking simple replacement of own over-complicated https://unix.stackexchange.com/a/115371/57293.

this problem occurs when type commands interactively, not in script. reason line type after select line being used response prompt. since if isn't in menu, reports error. doesn't execute if command, because read response prompt.

it's not problem in script because commands in script not used standard input.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -