Библиотека сайта rus-linux.net
8.1. Displaying user messages
8.1.1. Interactive or not?
Some scripts run without any interaction from the user at all. Advantages of non-interactive scripts include:
The script runs in a predictable way every time.
The script can run in the background.
Many scripts, however, require input from the user, or give output to the user as the script is running. The advantages of interactive scripts are, among others:
More flexible scripts can be built.
Users can customize the script as it runs or make it behave in different ways.
The script can report its progress as it runs.
When writing interactive scripts, never hold back on comments. A script that prints appropriate messages is much more user-friendly and can be more easily debugged. A script might do a perfect job, but you will get a whole lot of support calls if it does not inform the user about what it is doing. So include messages that tell the user to wait for output because a calculation is being done. If possible, try to give an indication of how long the user will have to wait. If the waiting should regularly take a long time when executing a certain task, you might want to consider integrating some processing indication in the output of your script.
When prompting the user for input, it is also better to give too much than too little information about the kind of data to be entered. This applies to the checking of arguments and the accompanying usage message as well.
Bash has the echo and printf commands to provide comments for users, and although you should be familiar with at least the use of echo by now, we will discuss some more examples in the next sections.
8.1.2. Using the echo built-in command
The echo built-in command outputs its arguments, separated by spaces and terminated with a newline character. The return status is always zero. echo takes a couple of options:
-e
: interprets backslash-escaped characters.-n
: suppresses the trailing newline.
As an example of adding comments, we will make the feed.sh
and penguin.sh
from Section 7.2.1.2 a bit better:
|
More about escape characters can be found in Section 3.3.2. The following table gives an overview of sequences recognized by the echo command:
Table 8-1. Escape sequences used by the echo command
Sequence | Meaning |
---|---|
\a | Alert (bell). |
\b | Backspace. |
\c | Suppress trailing newline. |
\e | Escape. |
\f | Form feed. |
\n | Newline. |
\r | Carriage return. |
\t | Horizontal tab. |
\v | Vertical tab. |
\\ | Backslash. |
\0NNN | The eight-bit character whose value is the octal value NNN (zero to three octal digits). |
\NNN | The eight-bit character whose value is the octal value NNN (one to three octal digits). |
\xHH | The eight-bit character whose value is the hexadecimal value (one or two hexadecimal digits). |
For more information about the printf command and the way it allows you to format output, see the Bash info pages. Keep in mind that there might be differences between different versions of Bash.