Unix Commands  «Prev  Next»
Lesson 7 Printing progress statements
ObjectiveUse printed comments to mark stages of success in a script.

Printing progress statements

If you correct all the syntax errors in your code and it still doesn’t work, you might try inserting progress statements in your code. You can use the –x option to see the values of variables that your code is using, but this is sometimes not enough. Inserting statements that print the values of your variables can determine exactly what is happening in your script.
echo “The value of num is $num, last is $last”, 
This line is temporarily inserted into the code to locate a problem. The code prints the value of the variables num and last each time through the loop so we can see how they change as the loop is run.

A Simple Implementation of a Progress Bar

#! /bin/bash
# progress-bar2.sh
# Invoke this script with bash. It doesn't work with sh.
trap "exit" SIGUSR1
sleep $interval; sleep $interval
while true
echo -n '.' # Use dots.
sleep $interval
done; } & # Start a progress bar as a background process.
trap "echo !; kill -USR1 $pid; wait $pid" EXIT # To handle ^C.
echo -n 'Long-running process '
sleep $long_interval
echo ' Finished!'
kill -USR1 $pid
wait $pid # Stop the progress bar.
trap EXIT
exit $?

The next lesson shows how to trap signals sent to a script.