An interactive script is a one that interacts with the user and requires user input. In this type of scripts, data flow in both directions (from the user to the script, and from the script back to the user).
When the user types the required input, the script first validates the input to make sure it belongs to the correct type and/or format (as we have did in the past article Validating User Input). After validating the input, the script could either directly make some text processing on the input string (like the uppercase example we wrote earlier in this series), perform a mathematical operation (like calculating the factorial for an input number), or use the input to make some decisions, like selecting between different choices. Each of these choices will either execute a command, or re-prompt the user to enter a new input to work on.
If not provided the expected input, the script remains in a waiting state until the user provides the input, or the process is killed manually (using ctrl+c or the kill command from another shell), or the connection times out.
In this article, we are going to learn how to write interactive shell scripts.
The read command
The first star in today’s series: the read command. This command prompts the user to enter a line of input, and assigns that input to one or more variables.
read –p "MESSAGE TO THE USER" var1 var2 ... varn
var1, var2 through varn are the variables to which the user inputs will be assigned.
The following is a read command that prompts the user to enter two numbers, and assigns those numbers to variables x and y.
You can write scripts that present multiple options to the user to choose from. Such multi-option views are called menus.
The read command can help create such menus.
The following is a script that displays a menu with few options to choose from, and prompts the user to select a choice. Based on the selected choice, the script will execute some simple commands.
This is the longest script we have written since we started this series. Let’s see how this will behave.
This is when selecting option 1. When selecting option 3, we get the following.
Now, let’s explain things:
- Line 6: defines a variable HORIZONETALLINE that contains a long list of equal sign character ‘=’ that is when printed looks like a horizontal line.
- Line 7: clears the screen.
- Lines 8 and 10: print two horizontal lines (to simulate something like an incomplete rectangular box)
- Line 9: prints the title of the menu.
- Lines 11-14: print the options.
- Line 15: prints a third horizontal line.
- Line 16: uses the read command to prompt the user to type the option he selects.
- Line 17: an if condition that will check if the provided choice is truly a number. If not, the else part (Line 31 and 32) will be executed, printing a message to the user saying he didn’t enter a number.
- If the user has provided a number, Line 18 will be the next line to execute:
- Line 18: checks if the user has provided a number not in the range between 1 and 4.
- If so, Line 19 is executed. It prints a message to the user that he should enter a number between 1 and 4.
- Line 20: else, if the choice was 1, Line 21 is executed printing the current system date and time.
- Similarly, if the choice was 2, Lines 23 and 24 will be executed, printing the calendar of the month.
- This will be repeated for options 3 and 4.
- Line 30: the closing fi for the inner if statement.
- Line 33: the closing fi for the outer if statement.
The select Statement
Some references introduce this tool under the Loops topic, and really it is. The structure of the select statement (having do and done), and the way it works (repeating some block of code for each value in a list of values) looks very similar to the way loops work. In this series, I preferred to introduce the select statement under the topic wherein it serves best, i.e. as a menu maker.
select var in VALUE1 VALUE2 … VALUEn
case var in
In this example, we are going to rewrite the previous example using the select statement. Consider the following script.
Let’s see how this will execute.
Notice that the menu doesn’t exit after executing the code block matching the user selection. The reason is simple; it is because the select tool is actually a loop (as I told you 15 minutes earlier). You can add an option that when selected quits the loop (and the program).
* * * * * *
In this article, we have talked about interactive shell scripts. As the name indicates, they are scripts that wait for inputs from the user, and respond back by an output that depends on the user’s input. Information flow in this type of scripts is bidirectional. We have also learned how to create menus. Menus display set of options, and prompt the user to choose from. We have seen how to use the read and select tools to create such menus.
See you in the next article.