choose(1f) - [NCURSES] select one item from a menu using a screen interface (LICENSE:PD)
Synopsis
Description
Options
Examples
See Also
Author
License
choose [choices] [ -i index] [ -t timeout ] [ -m text] [ -d default]|[ -help|--version]
choose(1f) allows users to select one item from a list of choices and returns the prompt of the selected choice on stderr. If the escape key is struck the return string is EXIT and the program terminates. Otherwise, the highlighted string is returned when a RETURN is entered. Cursor keys may be used to scroll thru the choices. The current selection is highlighted.Hot keys are generated by using the first character of each choice (the default) or from the string provided for the -i option.
If the mouse is supported by the terminal type being used, clicking on a menu item selects it and the program then exits.
NOTE:
choices Specifies the list of choices to be created, delimited by :. The default list is "Yes:No:Cancel". Currently limited to 16 60-character prompts.
-i index optional list of letters to act as case-insensitive shortcuts (ie. "hot-keys") for menu items. Striking the key will select the matching string from the choices and exit. The default is the first letter of each of the choices. -t timeout The number of 1/10 seconds to pause before a default choice is made. Acceptable values are from 0 to 9999. -d default If a timeout occurs the default is to return the first string in the choices. This allows you to specify a string specifically returned by a timeout. It is ignored otherwise. -m text Specifies the message to be displayed before the prompt. --help Displays this help message and exit --version Displays version info and exit
Sample commands
choose --helpThe program writes the selection to stderr. A bash(1) example showing how to read the output into a variable follows:choose Yes:No:Cancel -i ync -m Select Y for Yes, N for No or C for Cancel
# time out and return "Yes" if no activity for ten seconds choose Yes:No:Cancel -t 10
# hot-keys will be "12E" choose 1) Tomorrow:2) Next Week:Exit -m Select when to start ...
#!/bin/bash # The program writes to stderr. To read the output into a # variable but let ncurses(3c) control the screen we need # to first perform some redirection to perform a command substitution # and assign the output of a the command to the variable RESULT. # exec 3>&1 # Duplicate file descriptor 1 on descriptor 3 # as backup of descriptor 1 RESULT=$(choose 2>&1 1>&3) # run program exec 3>&- # Close file descriptor 3 case "$RESULT" in Yes) echo "Do stuff when answer is $RESULT" ;; No) echo "Do stuff when answer is $RESULT" ;; Cancel) echo "Do stuff when answer is $RESULT" ;; EXIT) echo "Do stuff when answer is escape key ;; *) echo "$0 : ERROR: unknown answer $RESULT" ;; esac # redirection of descriptor 2 (stderr) to be the duplicate # of descriptor 1 and descriptor 1 is restored to its original # value by duplicating descriptor 3 which contains the backup copy. exitYou can also use scratch files. For example
#!/bin/bash # The program writes the chosen string to stderr. # Create a temporary file and make sure it goes away when dome tmp_file=$(tempfile 2>/dev/null) || tmp_file=/tmp/test$$ trap "rm -f $tmp_file" 0 1 2 5 15 choose 2> $tmp_file RESULT=cat $tmp_file exit
dialog
John S. Urban
Public Domain
Nemo Release 3.1 | choose (1) | February 23, 2025 |