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.
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 126.96.36.199 a bit better:
michel ~/test> cat penguin.sh #!/bin/bash # This script lets you present different menus to Tux. He will only be happy # when given a fish. To make it more fun, we added a couple more animals. if [ "$menu" == "fish" ]; then if [ "$animal" == "penguin" ]; then echo -e "Hmmmmmm fish... Tux happy!\n" elif [ "$animal" == "dolphin" ]; then echo -e "\a\a\aPweetpeettreetppeterdepweet!\a\a\a\n" else echo -e "*prrrrrrrt*\n" fi else if [ "$animal" == "penguin" ]; then echo -e "Tux don't like that. Tux wants fish!\n" exit 1 elif [ "$animal" == "dolphin" ]; then echo -e "\a\a\a\a\a\aPweepwishpeeterdepweet!\a\a\a" exit 2 else echo -e "Will you read this sign?! Don't feed the "$animal"s!\n" exit 3 fi fi michel ~/test> cat feed.sh #!/bin/bash # This script acts upon the exit status given by penguin.sh if [ "$#" != "2" ]; then echo -e "Usage of the feed script:\t$0 food-on-menu animal-name\n" exit 1 else export menu="$1" export animal="$2" echo -e "Feeding $menu to $animal...\n" feed="/nethome/anny/testdir/penguin.sh" $feed $menu $animal result="$?" echo -e "Done feeding.\n" case "$result" in 1) echo -e "Guard: \"You'd better give'm a fish, less they get violent...\"\n" ;; 2) echo -e "Guard: \"No wonder they flee our planet...\"\n" ;; 3) echo -e "Guard: \"Buy the food that the Zoo provides at the entry, you ***\"\n" echo -e "Guard: \"You want to poison them, do you?\"\n" ;; *) echo -e "Guard: \"Don't forget the guide!\"\n" ;; esac fi echo "Leaving..." echo -e "\a\a\aThanks for visiting the Zoo, hope to see you again soon!\n" michel ~/test> feed.sh apple camel Feeding apple to camel... Will you read this sign?! Don't feed the camels! Done feeding. Guard: "Buy the food that the Zoo provides at the entry, you ***" Guard: "You want to poison them, do you?" Leaving... Thanks for visiting the Zoo, hope to see you again soon! michel ~/test> feed.sh apple Usage of the feed script: ./feed.sh food-on-menu animal-name
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
|\c||Suppress trailing newline.|
|\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.