[ This is the final part of the five part series that covers the very basics of Linux shell scripting. For the very beginning, start from Part I here.]
Previous parts discussed the structure of a Linux shell script, how to use Terminal commands effectively through scripts, how to use variables and how loops are useful in doing cool things. This final part will cover user interaction using shell scripts. This simply means how to get input from users, how to read it and process it to give out some useful output.
Now let’s take a look at how Linux shell scripts can interact with users.
The below script will ask a user who runs this script for password and confirms that the password is read without displaying it back on the Terminal :
Output for this is :
This is done by using stty -echo which will hide the typed characters. To turn the displayed characters on, simply use stty echo (right after the password was entered by the user in this case). By the way if any command that seems new to you, just look for what it does and how to use it using the man pages.
Here is one more, this script will read your name and output it back. This is a basic and classic example of how programs work by taking in inputs from user, process them and display the results. The command read is used to store whatever is entered by user in a variable and display it back.
Output is :
What it did was read the user name and stored it in variable $NAME, and then echoed it back that’s all.
To conclude the series, hopefully by now we think you would have picked up the very basics of Linux shell scripting and how to write simple scripts. This is a useful skill to have not only because it saves time when dealing with repetitive or boring tasks but also helps learning the inner workings of Linux.
If using the Linux Terminal is unfamiliar, our free e-book on Linux Terminal basics is a good place to start.
As a bonus, we will be providing some handy shell scripts that we regularly use based on what is covered by this primer and think they will help you too. Stay tuned for the same.
Happy shell scripting.