Multiple Input for Single Read in Bash
Taking input from the user is a mutual job for any programming language. You can take input from a user in bash script in multiple means. A read command is used in the bash script to take information from the user. Single or multiple data tin be taken in bash script by applying different options of the read command. Some common uses of the read command are shown in this tutorial.
Pick of Read Command:
Option | Purpose |
---|---|
-p | It is used to provide a helping message for the user earlier the input prompt. |
-s | It is used to take invisible input from the user. This option is used to take a countersign or hush-hush information. It is chosen silent mode. |
-t | It is used to set time in seconds to await for taking input from the user. |
-north | It is used to prepare the limit of input characters. |
Instance-1: Use of read command without variable
The read command tin can be used without whatsoever variable. The $Reply variable is used to read the input taken from the user by the read command without variable. Create a bash file with the following script to know how to use the read command without any variable.
#!/bin/bash
echo "What is your favorite programming linguistic communication?"
# Accept input without defining variable
read
# Impress the input value
echo "Your answer is $REPLY"
Output:
The following output volition appear after executing the higher up script.
Example-2: Using simple read command
Create a bash file with the following script to know how to use the read command with a variable. Afterwards running the script, the plan will look for the user input. When the user types the information and printing enter, the information will be stored in the respond variable. The value of the answer variable will be printed later on.
#!/bin/bash
echo -n "What is your favorite food: "
# Assign input value into a variable
read answer
# Impress the value of the variable
echo "Oh! you like $answer!"
Output:
The following output volition appear afterward executing the above script.
Example-three: Using read command with options
Create a bash file with the following script to know how to use both –p and –southward options together in the bash script. In this example, the username and password will be taken from the user and compared with the detail value to check the username and countersign are valid or not.
#!/bin/fustigate
# Type your Login Information
read -p 'Username: ' user
read -sp 'Password: ' pass
# Check the username and password are valid or not
if ( ( $user == "admin" && $pass == "12345" ) )
then
echo -e "\nSuccessful login"
else
echo -e "\due northUnsuccessful login"
fi
Output:
The post-obit output will appear later executing the above script.
Example-four: Using read command to take multiple inputs
The multiple inputs can be taken at a fourth dimension by using the read command with multiple variable names. In the following example, four inputs will be taken in four variables by using the read command.
#!/bin/fustigate
# Taking multiple inputs
echo "Type four names of your favorite programming languages"
read lan1 lan2 lan3 lan4
echo "$lan1 is your first selection"
echo "$lan2 is your second selection"
echo "$lan3 is your third choice"
echo "$lan4 is your fourth choice"
Output:
The post-obit output volition appear later on executing the higher up script.
Example-5: Using read command with the fourth dimension limit
Create a bash file with the post-obit script to take fourth dimension-restricted input from the user. Here, the fourth dimension will exist counted in seconds. In the following example, the program will wait for 5 seconds for the user's input, and if the user is unable to type the information within 5 seconds, the program will exit without value.
#!/bin/fustigate
# Take input with time limit
read -t 5 -p "Blazon your favorite colour : " color
# Print the input value
repeat $color
Output:
The following output will appear after executing the above script. The input value has been given in the commencement execution, and in the 2d execution, no input value has been given within 5 seconds.
Example-6: Employ of read command with -due north selection
Create a bash file with the following script to take input of a specific length. According to the script, the user will be able to enter a maximum of fifteen characters every bit input.
#!/bin/bash
echo "Enter your phone number(Maximum xv characters):"
# Accept input of a maximum 15 characters long
read -n 15 phone
# Add a newline
repeat
# Impress the input value
echo "Your phone number is $telephone"
Output:
The following output volition appear after executing the above script.
Example-seven: Checking a taken path is file or directory
Create a bash file with the following script to take input a path value from the final and check the input path is a directory or file.
#!/bin/bash
# Accept the path value from the input
read -p "Enter the valid path: " path
# Check the input values is a directory or non
if [ -d $path ]; and then
echo "$path is a directory."
# Check the input values is a file or not
elif [ -f "$path" ]; then
echo "$path is a file."
else
echo "Invalid path."
fi
Output:
The following output volition appear afterwards executing the above script.
Instance-viii: Initialize array using the read command
The array variable can be alleged and initialized by using the read command. Create a bash file with the post-obit script to know how to create and initialize an array by using the read command. Next, all elements of the assortment, the first element of the array, the first ii elements, and the terminal chemical element of the assortment will be printed.
#!/bin/bash
repeat "Enter five numeric values for the assortment with space:"
# Read values for the array
read -a MyArr
# Impress all array values
repeat${MyArr[@]}
# Print the commencement value of the assortment
repeat${MyArr[0]}
# Print the showtime two values of the array
echo${MyArr[@]:0:ii}
# Print the last value of the array
repeat${MyArr[4]}
Output:
The post-obit output will appear after executing the above script.
Conclusion:
Different uses of the read command take been shown in this tutorial by using multiple examples for helping the bash users to know the uses of this control properly and use it to their script.
manifoldwhateening01.blogspot.com
Source: https://linuxhint.com/bash-script-user-input/
0 Response to "Multiple Input for Single Read in Bash"
Post a Comment