Bash Shell Scripting Part 6 – User Input

Bash Shell ScriptingIn order to handle shell script arguments, it is needful to know that the arguments are required.

It can be a big hassle for people running the script to figure out what exactly is required to be able to run it.

In fact, in many cases — such as those where the user does not know how to write and thus read the bash script code — using a script correctly can be just about impossible.

To solve this problem, there is a little something called user input, which allows a script to be written in such a way as to prompt the user for input, wait until it is received, and carry on with execution.

When this solution is in use, any further issues usually boil down to PEBCAK*.

The read Command

The read command is used to accept and handle user input.

A variable should be specified with the read command, to store the input in.

read UserInput1

It can be preceded by an echo prompt, with text that explains what to do.

echo "Please Enter Your Pet's Name:"
read UserInput1

Alternatively, the read command itself can be used to specify the prompting text on the same line.

The -p option allows for the prompt text to be added.

read -p "Please Enter Your Pet's Name: " PetName

The -s option stands for “silent” and will not show the user’s input as it is typed in. This is ideal when handling passwords and other sensitive data.

read -s Password
read -s -p "Password: " Password

Now, let’s put together a simple script that you can save, try out, and modify to suit your needs.

#!/bin/bash
echo -e "This is a silly example script in which you can say whatever you want.\nPlease type something and press Enter"
read RandomText
read -p "Please Enter Your Dog's Name: " DogName
read -sp "Please Enter Your Dog's Password: " DogPass
echo -e "\nYour dog's name is $DogName."
echo "Your dog probably doesn't have a password, but you said its password is $DogPass."
exit 0

You should already know most of what is going on in this example script, but there are one or two new additions.

The -e option on the echo command enables interpretation of backslash escapes, and the \n is subsequently interpreted as a new line.

As seen in this next example, the results of a single input prompt can be stored in multiple variables. Each word entered (separated by spaces) will be saved to its own variable.

(If more variables exist than there are words entered, the remaining variables will be empty, or NULL.)

(If more words are entered than there are variables, the last variable will contain multiple words, or all remaining data.)

#!/bin/bash
read -p "Please Enter Your First and Last Names: " FirstName LastName
echo -e "\nYour first name is $FirstName and your last name is $LastName."
exit 0

Conclusion

As you can see, accepting user input is very simple, and can be done via a variety of methods.

Now we can move on to some of the most useful aspects of scripting.

*PEBCAK = Problem Exists Between Chair And Keyboard

Leave a Reply

You have to agree to the comment policy.