Common Extensions
Both bash and ksh93 support a large number of extensions over the POSIX shell. This section deals with those extensions that overlap; i.e., where both shells provide the same features, and in the same way.
The select Loop
bash and ksh share the
select
loop, which allows you to
generate simple menus easily. It has concise syntax, but it does quite
a lot of work. The syntax is:
selectname
[inlist
] dostatements that can use
$name ... done
This is the same syntax as the regular for
loop except for the keyword select
. And like for
, you can omit the in
list
and it
will default to "$@
"; i.e., the
list of quoted command-line arguments.
Here is what select
does:
Generate a menu of each item in
list
, formatted with numbers for each choicePrint the value of
PS3
as a prompt and waits for the user to enter a numberStore the selected choice in the variable
name
and the selected number in the built-in variableREPLY
Execute the statements in the body
Repeat the process forever (but see later for how to exit)
An example should help make this process clearer. Suppose you
need to know how to set the TERM
variable correctly for a timesharing system using different kinds of
video display terminals. You don't have terminals hardwired to your
computer; instead, your users communicate through a terminal server.
Although the telnet protocol can
pass the TERM
environment variable, the terminal server isn't smart enough to do so. This means, among other things, that the tty (serial device) ...
Get Classic Shell Scripting now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.