9- Bash Scripting- Taking user’s Input using Read command

Last updated on March 12th, 2018 at 08:14 pm

Hello Linux-fanatics. In our last tutorial we discussed for conditional statement, & now we will discuss how we can take user’s input as value for our variables by using READ command & how we can redirect the output of a script to a file.

READ command

Up until now, we were using variables that has values assigned into the script itself. But what if we want to use different values for our variables every-time we are running our script. For ex, when we are doing arithemetic operation or when we have created a script to take backup of files & want to define which files needs to be backed up.

We can take user’s input as values for our variables by using read command.

Syntax for using read comamnd is

read variable_name

Example Script

#!/bin/bash
# assigning user’s input as variable’s value
echo “ what is your name ?”
read name
echo “Welcome, $name”

This was a very simple example of to demonstrate working of read command . Let’s take another example

#!/bin/bash
# taking user’s input to check file or directory
echo “ Please enter the full path of file/directory (ex- /home/dan.new.txt)”
read path
for file in $path
do
if [ -d “$file” ]
then
echo “$file is a directory”
elif [ -f “$file” ]
then
echo “$file is a file”
else
echo “No file found”
done

In this script, user will be asked to input a file path & based on his input script, it will tell if the mentioned path is a file or directory or none.

Also, I would like to discuss couple of options that can be used with read command

  • read –n 5 variable_name will wait for user’s input for 5 seconds, after that it will accept any input
  • read –s variable_name will not show value entered by user on screen. Can be used for passing  sensitive information like a password.

Output Redirection to a file

When running our scripts, we might want to keep the record our output. We can redirect the output of the script to a file, let’s use the same script we used above to show you ,

#!/bin/bash
# output redirection to a file
echo “ Please enter the full path of file/directory (ex- /home/dan.new.txt)”
read path
for file in $path
do
if [ -d “$file” ]
then
echo “$file is a directory”
elif [ -f “$file” ]
then
echo “$file is a file”
else
echo “No file found”
done > result.txt

That’s it, our output will be not be shown on monitor but will be saved to file named “resilt.txt”.

Passing Parameters

Along with providing values to variables using READ command & mentioning values inside the script, we can also define value to variables during the time we run our scripts. Let’s understand it using a script named passing_values.sh

#!/bin/bash
# passing values
echo “Name of the script is $0”
echo “first value is $1”
echo “second value is $2”
echo “total numberof values are $#”
sum=$[$1 + $2]
echo “sum of $1 & $2 is $sum”

Now, we run the script as

$ sh passing_values.sh 10 20

& our script will run fine & will provide us with outputs. Now, let’s see what these variable $0, $1,$2 and $# means

$0, when used in script prints name of the script,

$1 will the first value for the script that we mentioned while running the script i.e.10

$2 will the second value for the script that we mentioned while running the script i.e.20

$# will print total number of values provided, which is this case is 2 (10,20)

In this script we only mentioned 2 variables but we can use a number of variables. However, if we are defining more than 9 values , we have to define them a little differently. Instead of using sum=$[$10 + $11] for our 10th  & 11th  variable, we use sum=$[${10} +${11}].

Our tutorial is now complete. Next up, we will discuss while & until loop statements.

Remember to practice & also mention any queries or suggestions down below.

 

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Donate us some of you hard earned money:

Linux TechLab is thankful for your continued support.

Shusain

Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

2 Responses

  1. marcodellav says:

    HI, I have tried the first script but, unless you insert a space between “if” and “[“, it returns a syntax error.

    I am using bash 4.4 on macOS, have you encountered these errors before?

Leave a Reply