Why does my menu with select fail the first time?

Walter A

I tried to answer another SO question with a simple menu using the builtin select statement. The code displays names from /etc/passwd, and let you select a name by giving a number:

PS3="Enter a number: "
select name in $(cut -d: -f1 /etc/passwd) ; do
   if [ -n "${name}" ]; then
      break
   fi
   echo "Sorry, please enter a number as shown."
done
echo "Entry from passwd is: ${name}"

The works fine except for the very first time. When you give a correct answer the first time it will ask you to try again.
I tried to get a more detailed explanation of the first time, but I couldn't get a reproducable cook-book. When you copy-paste this code on your server, and give a correct answer you will probably have the same problem. When you repeat the command (from history or a new paste), the code shows now problem. I tried to get the problem again by logging out and logging in (sometimes it works) or rebooting.
I tried different ways to reproduce the problem in other situations (using different variable names, unsetting variables, using a slow list of values with select name in $(echo One; sleep 1; echo Two; sleep 2; echo Three; sleep 1); and opening a new shell.

I searched for other examples with select, but I can't find clues in other posts like https://stackoverflow.com/a/16750755/3220113 and https://askubuntu.com/a/1716.

I tried to fix my code with a sync and that seems to be a work-around:

PS3="Enter a number: "
select name in $(cut -d: -f1 /etc/passwd) ; do
   # is sync needed here?
   sync
   if [ -n "${name}" ]; then
      break
   fi
   echo "Sorry, please enter a number as shown."
done
echo "Entry from passwd is: ${name}"

I couldn't reproduce the error when I include the sync command. Is sync really a working patch and why do I need this here?

I do not need other ways to write a menu. I already found the graphical dialog Dialog from bash script and was looking for a simple replacement of my own over-complicated https://unix.stackexchange.com/a/115371/57293.

Barmar

This problem only occurs when you type the commands interactively, not in a script. The reason is that the line you type after the select line is being used as the response to the prompt. Since if isn't in the menu, it reports an error. Then it doesn't execute the if command, because it was read as the response to the prompt.

It's not a problem in a script because the commands in the script are not used as standard input.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does my UpdateAsync(user) method fail the first time, claiming user doesn't exist?

From Dev

Why does my flask test client fail about 60% of the time?

From Dev

Why does my NSMutableURLRequest fail?

From Dev

Why does my prompt repeat twice the first time?

From Dev

why does my program take long to access files the first time

From Dev

Why does select() consume so much CPU time in my program?

From Dev

why does my spring boot app first fail and then suddenly start in cloudfoundry?

From Java

Why does assembly binding fail in my project?

From Dev

Why does my cross-compiling fail?

From Dev

Why does my redirected CORS request fail?

From Dev

Why does my rspec test fail?

From Dev

Why does my kernel fail to build?

From Dev

Why does my linq to sql query fail?

From Dev

Why does my pointer arithmetic fail in this array

From Dev

Why does my wifi fail to stay connected?

From Dev

Why does my jasmine tests fail on this directive?

From Dev

Why does my Vapor query always fail?

From Dev

Why Does My FAMILY Query Fail?

From Dev

Why does my menu jump the second time I try and drag it to a new item?

From Dev

Why does my removeDuplicates method only remove the duplicate integer the first time it encounters it?

From Dev

Why does onCreate called twice in my Fragment when I first time open Activity

From Dev

Why does the first access to samba shared folder always fail?

From Dev

Why does this Joda-Time related test fail sometimes?

From Dev

Why does it take more time for a login to fail than to succeed?

From Dev

Why does this Joda-Time related test fail sometimes?

From Dev

Why does my HUnit test suite fail but pass successfully in Cabal?

From Dev

Why does cron silently fail to run sudo stuff in my script?

From Dev

Why does my WebSockets handshake fail with ERR_CONNECTION_RESET?

From Dev

Why does my Python script fail with syntax errors?

Related Related

  1. 1

    Why does my UpdateAsync(user) method fail the first time, claiming user doesn't exist?

  2. 2

    Why does my flask test client fail about 60% of the time?

  3. 3

    Why does my NSMutableURLRequest fail?

  4. 4

    Why does my prompt repeat twice the first time?

  5. 5

    why does my program take long to access files the first time

  6. 6

    Why does select() consume so much CPU time in my program?

  7. 7

    why does my spring boot app first fail and then suddenly start in cloudfoundry?

  8. 8

    Why does assembly binding fail in my project?

  9. 9

    Why does my cross-compiling fail?

  10. 10

    Why does my redirected CORS request fail?

  11. 11

    Why does my rspec test fail?

  12. 12

    Why does my kernel fail to build?

  13. 13

    Why does my linq to sql query fail?

  14. 14

    Why does my pointer arithmetic fail in this array

  15. 15

    Why does my wifi fail to stay connected?

  16. 16

    Why does my jasmine tests fail on this directive?

  17. 17

    Why does my Vapor query always fail?

  18. 18

    Why Does My FAMILY Query Fail?

  19. 19

    Why does my menu jump the second time I try and drag it to a new item?

  20. 20

    Why does my removeDuplicates method only remove the duplicate integer the first time it encounters it?

  21. 21

    Why does onCreate called twice in my Fragment when I first time open Activity

  22. 22

    Why does the first access to samba shared folder always fail?

  23. 23

    Why does this Joda-Time related test fail sometimes?

  24. 24

    Why does it take more time for a login to fail than to succeed?

  25. 25

    Why does this Joda-Time related test fail sometimes?

  26. 26

    Why does my HUnit test suite fail but pass successfully in Cabal?

  27. 27

    Why does cron silently fail to run sudo stuff in my script?

  28. 28

    Why does my WebSockets handshake fail with ERR_CONNECTION_RESET?

  29. 29

    Why does my Python script fail with syntax errors?

HotTag

Archive