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


READ command in Linux

Up until now, we were using variables that have values assigned to the script itself. But what if we want to use different values for our variables every-time we are running our script. For example, when we are doing an arithmetic operation or when we have created a script to take a 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 the read command.

The syntax for using the read command 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 to demonstrate the working of the 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

read command in linux

In this script, the 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 a 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 the 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 a record of 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 the monitor but will be saved to a file named “resilt.txt”.

Passing Parameters

Along with providing values to variables using the READ command & mentioning values inside the script, we can also define the 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 number of 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 the total number of values provided, which in 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 on how to use the Read command in Linux to take user input for a bash script. 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

Linux TechLab is thankful for your continued support.