Linux Know-How provides a collection of introductory texts on often needed Linux skills. |
Home Bash Guide for Beginners Writing interactive scripts Displaying user messages | ||||||||||||||||||||||||||||||
See also: Example Bash script | ||||||||||||||||||||||||||||||
Search the VIAS Library | Index | ||||||||||||||||||||||||||||||
Displaying user messagesInteractive or not?Some scripts run without any interaction from the user at all. Advantages of non-interactive scripts include:
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:
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. Using the echo built-in commandThe 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:
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
For more information about the printf command and the way it allows you to format output, see the Bash info pages.
|
||||||||||||||||||||||||||||||
Home Bash Guide for Beginners Writing interactive scripts Displaying user messages |