|
December
29, 2003 -
Managing Variable Attributes with typeset
- Part III
|
Our
review of typeset will conclude with a
short shell script to illustrate its
use. The script, named get_answer,
performs the common task of prompting the
user running the script for a Y (yes) or a
N (no), reading the response, and then
validating it against pre-defined values.
|
#!/bin/ksh
#
# SCRIPT: get_answer
# DESCRIPTION: Prompts, reads, and validates answer from user
# DATE: 12/29/2003
# AUTHOR: LiveFire Labs - Online UNIX and Linux Training
#
#
typeset -u answer="A"
while [ "${answer}" != "Y" ] && [ "${answer}" != "N" ]
do
print "Perform operation (Y/N)?"
read answer
done
if [ "${answer}" = "Y" ]
then
print "Performing operation..."
# additional statements go here
fi
exit 0
|
Breaking
the script down helps to understand what
is going on.
|
typeset -u answer="A"
|
This
statement does two things. It sets the
uppercase attribute for the variable answer,
and also sets the initial value of the
variable to A. Defining an
"invalid" value for the variable
prior to the while statement may not always
be programmatically required, but is a good
practice so that you can be sure of what
will be in answer prior to the first
check of the variable.
|
while [ "${answer}" != "Y" ] && [ "${answer}" != "N" ]
do
print "Perform operation (Y/N)?"
read answer
done
|
The
while loop first checks the value of answer,
then prompts the user for a response, and
finally reads the user's input. The
loop will terminate when the user enters a
Y, y, N, or n.
|
if [ "${answer}" = "Y" ]
then
print "Performing operation..."
# additional statements go here
fi
exit 0
|
The if-then
block of code contains the statements to be
performed if the user enters a Y or y.
The script ultimately exits with a return
code of 0.
Below are two sample runs of the get_answer
script:
|
$ ./get_answer
Perform operation (Y/N)?
a
Perform operation (Y/N)?
1
Perform operation (Y/N)?
yyy
Perform operation (Y/N)?
y
Performing operation...
$
$ ./get_answer
Perform operation (Y/N)?
n
$
|
Notice
that the first run continues until a single
lowercase y is entered. Some may argue
that a response like yyy should be
automatically converted to a single y by
setting the variable's width and
justification attributes, but it is wiser to
require the exact response you are looking
for, especially when a critical operation is
either performed or not performed based on
the user's answer.
Since the uppercase attribute is set on the
variable answer, lowercase input is
converted to uppercase prior to checking for
a valid response. As you can see, the
statement inside of the if-then block is
executed because a single y was
entered. In the second example, this
statement is bypassed.
|
|
|
|
|
|
Learn
more...
If you are new to the UNIX or Linux
operating system and would like to learn
more, you may want to consider
registering for LiveFire Labs' UNIX
and Linux Operating System Fundamentals
online training course.
If you already have a solid grasp of the
fundamentals but would like to learn more
about the Korn shell and basic and
advanced shell scripting, taking our Korn
Shell Scripting course will be
beneficial to you.
Our
innovative hands-on training model allows
you to learn
UNIX by completing hands-on
exercises on real servers in our Internet
Lab.
More
Tips...
· Popular
UNIX Tips from the Past
|
|
|
|
 |
 |
|
Receive
the UNIX Tip, Trick, or Shell Script of the
Week by Email
|
|
|