Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
Shells: User's Guide: HP 9000 Computers > Chapter 7 Programming Tips

Debugging

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Index

When you use pipes in shell programs, it becomes difficult to debug since you do not see output from commands in the pipe. One suggestion to help debug pipes is to add cat statements in the pipes to show you what the intermediate output would be. For example, you could add a cat command followed by an exit at one point in a pipe. The pipe will then list the output at that stage, and it will exit the program (to avoid further errors, and to indicate exactly where in the pipe you are).

Now, when you are ready to test the program, you need not exit the editor (which we are assuming is vi), run the program, see the output, then enter vi again to make changes. Rather there is a more convenient way to debug: save the program using vi command ":w"; run the program from vi by using the command:

:! script [arguments]

The :! command executes commands in the shell outside of vi. When you see the output, you then go back to vi (when prompted) make any necessary changes, and try it again. You can also execute a shell from vi (by typing ":sh") then execute the script.

For making this process quicker, you can: add the "cat" statements in the program, save the program, run the program from vi, return to vi and use the u (undo) command which will get rid of the "cat" statements (as long as you do not execute any other text manipulation commands since the last insert).

Another suggestion is to use the "tee" command instead of "cat". "tee" will transcribe the standard input to the standard output and makes a copy in a file(s) which are arguments to the command. The format is:

tee [-i ] [-a] [ file]...

where the -i option ignores interrupts, and the -a option causes the output to be appended to the files rather than overwriting them. More than one file can be specified.

Creating Optional Pieces in a Pipe

There may come a time when you need a pipe with an optionally inserted piece. In other words, you wish to execute "a | c" if one condition exists and "a | b | c" if another condition exists. To do this, consider the following example:

optional=""
if [ condition ]
then
optional='b ||'
fi

eval "a | $optional c"

If condition is true, optional becomes "b |", and thus the eval statement executes "a | b | c". Otherwise, "a | c" is executed.

You can also use this same idea in optional redirection statements.

Halting Background Processes

If you are running several background processes and a foreground process, you may wish to be able to terminate all processes at the same time (instead of using the kill command for each). This may be helpful for instrumentation related work.

Let's say you wish to use the Break key to terminate three processes: the one in the foreground (terminated automatically) and two in the background. Here is a script which would accomplish this:

# initialize the process list
proc=

echo starting process 1
process1
# add process number to list
proc="$proc $!"

echo starting process 2
process2
# add process number to list
proc="$proc $!"

# the BREAK key will kill everything
trap "kill $proc;trap 2;exit" 2

echo starting process 3
# foreground process
process3

The first line initializes a parameter proc to a null value. The next two sections start the two background processes: first, a comment is echoed to the screen so you know the process was started, then, the process is started in the background (using the & operator), and then, the parameter proc is set to the process id of the process just run (the parameter $! is the process id if you recall).

The line containing the trap command looks for the signal 2 (which means interrupt). When this signal is received, it executes the commands in the double quotes: kill $proc will kill the two background processes since $proc is a list of the process ids.

The last command section starts the foreground process.

So, when this script is executed, the three processes are run. If you press the Break key, the trap is activated killing the two background processes (process1 and process2). The foreground process (process3) is automatically terminated.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-1991 Hewlett-Packard Development Company, L.P.