|
|
|
|
April
28, 2003 (Tip) -
Other
Prompt String Shell Variables (PS2, PS3,
PS4)
|
|
PS2
When
you issue a command that is incomplete,
the shell will display a secondary prompt
and wait for you to complete the command
and hit Enter again. The default
secondary prompt is > (the greater than
sign), but can be changed by re-defining
the PS2 shell variable.
|
Example 1
- using the default secondary prompt:
$ echo "this is a
> test"
this is a
test
$
Example 2 - re-defining PS2 with a
customized prompt:
$ PS2="secondary prompt->"
$ echo "this is a
secondary prompt->test"
this is a
test
$
|
PS3
(Korn Shell)
The
select command is used for creating an
interactive menu. If you are
unfamiliar with building a menu with
select, it will be covered in next week's
tip.
The default prompt string used by select
is #? and can be changed by re-defining
PS3. select will display the string
stored in PS3 when it is ready to read the
user's selection.
PS4
(Korn Shell)
A great tool for debugging shell scripts
is an execution trace. An execution
trace lists each command before the shell
runs it. You can enable execution
trace for your script by simply adding set
-x to the beginning of it.
The default execution trace prompt is +
(the plus sign), but can be changed by
re-defining the PS4 shell variable:
|
Contents of
test.debug script:
#!/bin/ksh
set -x
for i in 1 2 3
do
echo $i
done
exit
Execution output:
$ test.debug
+ echo 1
1
+ echo 2
2
+ echo 3
3
+ exit
$
|
A
common practice is to use the LINENO
variable within the PS4 prompt to display
the line number in the script that
corresponds with the trace line. For
example:
|
Updated
test.debug script:
#!/bin/ksh
set -x
PS4='[${LINENO}]+'
for i in 1 2 3
do
echo $i
done
exit
Execution
output:
$ test.debug
+ PS4=[${LINENO}]+
[8]+echo 1
1
[8]+echo 2
2
[8]+echo 3
3
[11]+exit
|
Notice
that line numbers 8 and 11 from the
test.debug script are identified in the
execution trace output.
Learn
more...
If you are new to the UNIX or Linux
operating system and would like to learn
more about shells, shell commands, and
shell variables, 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
|
|
|
|