Linux Tactic

Mastering User Input in Bash Scripting: Read Command and Stdin

Taking Input from User in a Bash Script

Bash scripting is a powerful tool that can automate repetitive tasks on a computer. A script is essentially a set of instructions that a computer follows to achieve a particular task.

One of the most crucial aspects of a script is taking input from the user. In this article, we will explore two methods of taking input from the user: using the read command and using Stdin.

Using Read Command

The read command is a built-in command in Bash that allows you to take input from the user. You can use this command to take single or multiple inputs from the user, perform arithmetic calculations and even verify the users age.

Taking Multiple Inputs

To take multiple inputs from the user, separate them with a space. For example:

“`

echo “Enter your

first name, last name, and age”

read fname lname age

“`

This will prompt the user to enter their

first name, last name, and age in one line, separated by a space. You can

then use these variables in your script, for example:

“`

echo “Hello, $fname $lname.

You are $age years old.”

“`

Arithmetic Calculation

You can also perform arithmetic calculations using the read command. For example:

“`

echo “Enter two numbers to add:”

read num1 num2

sum=$((num1 + num2))

echo “The sum of $num1 and $num2 is $sum.”

“`

Adding Prompt in Read Command

You can add a prompt to the read command using the `-p` option. This will display a message to the user, prompting them to enter input.

For example:

“`

read -p “Enter your name: ” name

echo “Hello, $name!”

“`

This will display the message Enter your name: and wait for the user to enter their name. Once the user enters their name, it will be displayed on the screen.

Age Eligibility Check

You can also use the read command to verify the users age before proceeding with the script. For example:

“`

echo “How old are you?”

read age

if [ $age -ge 18 ]

then

echo “You are eligible to vote.”

else

echo “You are not eligible to vote.”

fi

“`

This script prompts the user to enter their age and

then checks if they are eligible to vote. If the user is 18 years old or older, the script will display the message You are eligible to vote.

Otherwise, the script will display the message You are not eligible to vote.

Using Stdin

Stdin (standard input) is a stream of data that represents the input from the user or a

file. You can use Stdin to

filter eligible candidates based on certain criteria.

Filtering Eligible Candidates

Consider a scenario where you have a list of candidates and you want to

filter out those who are eligible for a job based on their age. You can use the following script to achieve this:

“`

echo “Enter the minimum age:”

read min_age

echo “Enter the maximum age:”

read max_age

echo “Enter the path to the

file containing the list of candidates:”

read

filename

while read line

do

name=$(echo $line | cut -f1 -d”,”)

age=$(echo $line | cut -f2 -d”,”)

if [ $age -ge $min_age ] && [ $age -le $max_age ]

then

echo “$name is eligible”

else

echo “$name is not eligible”

fi

done < $

filename

“`

This script prompts the user to enter the minimum and maximum age for the job and the path to the

file containing the list of candidates. The script

then reads each line of the

file and extracts the name and age of the candidate.

If the candidates age falls within the range speci

fied by the user, their name is displayed along with the message is eligible. Otherwise, the message is not eligible is displayed.

Conclusion

Taking input from the user is an essential aspect of Bash scripting. The read command and Stdin provide powerful tools for achieving this functionality.

Through the examples provided in this article, you should now have a deeper understanding of how to take input from the user in your Bash scripts and how to

filter data using Stdin. With this knowledge, you can start building more complex and powerful scripts that can automate various tasks on your computer.

In summary, this article explored two methods of taking input from users in Bash scripting: the read command and using Stdin. The read command enables taking single or multiple inputs, arithmetic calculations, and verifying the user’s age.

While Stdin can be used to

filter data and candidates based on certain criteria. These techniques are essential for creating powerful and effective Bash scripts that automate tasks on your computer.

As you become more pro

ficient in Bash scripting, these techniques will help you create scripts that are more complex and ef

ficient. Remember always to make sure the user input is sanitized and ensure the input format is correct.

Popular Posts