How to Read the Number of Files From the User Input Linux
Taking input from the user is a mutual task for any programming language. You tin can accept input from a user in bash script in multiple ways. A read control is used in the bash script to take data from the user. Single or multiple information can be taken in fustigate script by applying different options of the read command. Some common uses of the read command are shown in this tutorial.
Option of Read Command:
Option | Purpose |
---|---|
-p | Information technology is used to provide a helping bulletin for the user earlier the input prompt. |
-southward | It is used to have invisible input from the user. This option is used to take a password or secret data. It is chosen silent mode. |
-t | It is used to set time in seconds to expect for taking input from the user. |
-n | It is used to set the limit of input characters. |
Example-i: Utilize of read control without variable
The read command can exist used without any variable. The $REPLY variable is used to read the input taken from the user past the read command without variable. Create a fustigate file with the post-obit script to know how to use the read command without any variable.
#!/bin/bash
echo "What is your favorite programming language?"
# Accept input without defining variable
read
# Impress the input value
echo "Your answer is $Respond"
Output:
The following output will announced after executing the higher up script.
Example-2: Using simple read control
Create a fustigate file with the following script to know how to utilise the read command with a variable. After running the script, the program will wait for the user input. When the user types the data and press enter, the data volition be stored in the reply variable. The value of the answer variable will exist printed later.
#!/bin/bash
repeat -n "What is your favorite nutrient: "
# Assign input value into a variable
read respond
# Print the value of the variable
echo "Oh! you like $answer!"
Output:
The post-obit output will announced afterward executing the above script.
Example-3: Using read command with options
Create a bash file with the following script to know how to use both –p and –south options together in the bash script. In this example, the username and password volition be taken from the user and compared with the item value to bank check the username and password are valid or not.
#!/bin/fustigate
# Type your Login Data
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 -due east "\northwardUnsuccessful login"
fi
Output:
The following output volition appear after executing the higher up script.
Example-4: Using read control to accept multiple inputs
The multiple inputs tin be taken at a time past using the read command with multiple variable names. In the following example, four inputs will be taken in iv variables past using the read control.
#!/bin/fustigate
# Taking multiple inputs
echo "Blazon four names of your favorite programming languages"
read lan1 lan2 lan3 lan4
repeat "$lan1 is your kickoff pick"
repeat "$lan2 is your second pick"
echo "$lan3 is your third selection"
echo "$lan4 is your quaternary choice"
Output:
The following output will appear afterward executing the in a higher place script.
Case-5: Using read command with the time limit
Create a bash file with the post-obit script to have fourth dimension-restricted input from the user. Here, the time volition be counted in seconds. In the following example, the plan will await for five seconds for the user'southward input, and if the user is unable to type the data within v seconds, the program will exit without value.
#!/bin/bash
# Accept input with time limit
read -t 5 -p "Blazon your favorite colour : " colour
# Print the input value
echo $color
Output:
The following output will appear later executing the in a higher place script. The input value has been given in the beginning execution, and in the second execution, no input value has been given inside v seconds.
Example-6: Use of read control with -north pick
Create a bash file with the post-obit script to have input of a specific length. According to the script, the user will be able to enter a maximum of xv characters as input.
#!/bin/bash
echo "Enter your telephone number(Maximum 15 characters):"
# Have input of a maximum xv characters long
read -north 15 phone
# Add a newline
echo
# Impress the input value
echo "Your phone number is $phone"
Output:
The following output will announced afterward executing the above script.
Example-7: Checking a taken path is file or directory
Create a fustigate file with the post-obit script to take input a path value from the terminal and check the input path is a directory or file.
#!/bin/bash
# Take 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 so
repeat "$path is a directory."
# Check the input values is a file or not
elif [ -f "$path" ]; then
echo "$path is a file."
else
repeat "Invalid path."
fi
Output:
The following output will announced after executing the above script.
Example-viii: Initialize assortment using the read command
The array variable can be declared and initialized by using the read control. Create a bash file with the following script to know how to create and initialize an array by using the read control. Next, all elements of the array, the first element of the array, the first 2 elements, and the last element of the array will be printed.
#!/bin/bash
echo "Enter five numeric values for the assortment with space:"
# Read values for the array
read -a MyArr
# Print all array values
repeat${MyArr[@]}
# Print the first value of the assortment
echo${MyArr[0]}
# Impress the outset two values of the assortment
echo${MyArr[@]:0:2}
# Impress the last value of the array
echo${MyArr[4]}
Output:
The following output will appear afterward executing the to a higher place script.
Conclusion:
Dissimilar uses of the read command accept been shown in this tutorial by using multiple examples for helping the bash users to know the uses of this command properly and apply information technology to their script.
Source: https://linuxhint.com/bash-script-user-input/
0 Response to "How to Read the Number of Files From the User Input Linux"
Post a Comment