in

How To Write Linux Shell Scripts (Part 3)

- - No comments
In the previous part we learned about variables and how to use them. This time around we’ll learn how to take input from the user and store it in a variable.

So far we know that variables are places in memory where you store specific data. In case you haven’t forgotten, we declare a variable like this:

myvariable=predator

In the above statement, you store the value of predator in the variable myvariable. But what if we didn’t know what value to store in the variable? What if we wanted to ask the user what they prefer and accordingly write our bash script? Well, this is where the read statement comes in handy. The read statement takes input from user and stores it in a variable. Then this variable can be used to perform further operations. This is particularly useful in writing bash scripts wherein you have to ask the user certain things before proceeding with the execution (C programmers can consider this as Bash’s version of scanf).


The read statement

Let’s see the read statement in action now. Open your favorite text editor and type in or paste the following script:

#!/bin/bash
#Script to read your name from the keyboard
echo "Welcome to BASH"
echo "Please enter your name: "
read name
echo "Hello $name, how are you?"


Save this file as saymyname.sh and then run it using the following commands:

$chmod +x saymyname.sh
$./saymyname.sh


The terminal will show the following output:

$./saymyname.sh
Welcome to BASH
Please enter your name:


Now, here, simply type in your name and press enter. Et voila! You’ll have the following output:

Hello , how are you?



Code Explained
:

echo "Please enter your name: "

In this line we simply print out an instruction so that the user knows when to input his name. Then, on the next line we use the read command:

read name


This instructs the shell to ‘read’ whatever the user types in and store it in a new variable called name. This is the same thing as declaring:

name=TechSource 

However, the only difference here is that you are giving the value to the variable after the script is executed.

You then use the ‘name’ variable to print out a greeting to the user.

Now that we’ve understood how it all works, it’s time to try something a little more complex.

Open the same file, that is saymyname.sh and make the following modifications to it:

#!/bin/bash
#Script to read your name from the keyboard
echo "Welcome to BASH"
echo "Please enter your full name and age"
read fname lname
echo "Hello $fname $lname, how are you?"
echo "Please enter your age"
read age
echo "So, $fname, you are $age years old!"


Save the file and execute it by using the following command:

$./saymyname.sh

You’ll get the following output:

Welcome to BASH
Please enter your full name and age
James Bond << Here you can enter any name you want >>
Hello James Bond, how are you?
Please enter your age
40 << Enter your age here >>
So, James, you are 40 years old!


Code Explained
:

In this example, we have modified the script slightly so that we can get more than one values from the user. Look at the following line:

echo "Please enter your full name and age"
read fname lname


The read command now takes two variables at once. This means that whenever the user is asked for input, they have to write their full name, that is, type their name and surname with a space in between. In this way, you can ask the user to enter multiple variables at once and use those variables individually later on in the script. As you can see, we have used the first name, that is $fname variable later on in the script without the $lname variable.


That’s all for today. We’ll see you in the next lesson. In the meantime you can try out these exercises:

1. Ask the user about his favorite ice cream flavor and print out “I love (favorite flavor) ice cream!”

2. Ask the user to type in his or her age, height and weight in one line and print it out in the following format:

Age: 32
Weight : 85 kgs
Height: 6 feet


Written by: Abhishek, a regular TechSource contributor and a long-time FOSS advocate.


No comments

Post a Comment