choice
Package: WA2L/edrc 1.5.57
Section: Library Commands (3)
Updated: 16 July 2007
Index
Return to Main Contents
NAME
choice - prompt for a key to be pressed
SYNOPSIS
edrc/lib/choice
"prompt"
keylist
[
default_key
[
options
]]
AVAILABILITY
WA2L/edrc
DESCRIPTION
This command is used in scripts to prompt a user to press a key.
As long as a key is pressed that is not specified as valid in the
keylist
the
choice
prompt is repeated.
It is also possible to specify a default key that takes in effect
when <ENTER> or <RETURN> without entering a key is pressed.
All input is logged to a logfile if the environment variable
$EDRC_LOGFILE
is set and the
NO_LOG
option isn't set.
OPTIONS
- prompt
-
prompt to be displayed.
- keylist
-
a string of keys which are accepted by
choice.
If a key is entered the
choice
prompt is re-displayed and the user is requested to enter a valid
key. The
keylist
is case-sensitive.
- default_key
-
default key. This key will be returned if the <ENTER> key is pressed.
- options
-
-
- NOT_NULL
-
null input is not accepted.
- NO_LOG
-
do not record the input to the logfile.
- LOG_STARS
-
log a star (*) to the logfile instead of the real input.
ENVIRONMENT
- $EDRC_LOGFILE
-
the accepted input read by
choice
is logged to the file specified in this environment variable (if set).
EXIT STATUS
- 0
-
no error.
- 4
-
usage listed.
EXAMPLES
The following examples are script cut-outs of Bourne-, Korn- or
Bash shell scripts:
- 1) Yes/No user prompt
-
answer=`choice "remove file? <yn>" yn n`
if [ $answer = y ]; then
\rm -f $file
echo "File removed"
else
echo "File not removed"
fi
- 2) A menu
-
cat <<EOM
p) print user credentials
a) create user account
h) create home directory
q) quit
EOM
ans=`choice "?" pahq q`
case $ans in
p) print_users ;;
a) create_account ;;
h) create_homedir ;;
q) exit ;;
esac
- 3) A menu including logging all actions
-
Logfile=/var/log/user_management.log
EDRC_LOGFILE=$Logfile; export EDRC_LOGFILE
{
cat <<EOM
p) print user credentials
a) create user account
h) create home directory
q) quit
EOM
ans=`choice "?" pahq q`
case $ans in
p) print_users ;;
a) create_account ;;
h) create_homedir ;;
q) exit ;;
esac
} >> $Logfile
- 4) Run a command until 'n' is pressed
-
while [ `choice "count? <yn>" yn y` = y ]; do
echo "number of processes: `ps -ef | wc -l`"
done
SEE ALSO
edrcintro(1),
input(3),
sh(1),
ksh(1)
NOTES
-
BUGS
-
AUTHOR
choice was developed by Christian Walther. Send suggestions
and bug reports to wa2l@users.sourceforge.net .
COPYRIGHT
Copyright © 2008
Christian Walther
This is free software; see
edrc/doc/COPYING
for copying conditions. There is ABSOLUTELY NO WARRANTY; not
even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
This document was created by man2html
using the manual pages.
Time: 16:17:39 GMT, April 17, 2025