
Handling user input in Bash enables you to collect, store, and process specific options, commands, or values to execute specific tasks. You can capture and handle user input in Bash using the following options:
read: Allows Bash to interactively capture user input and store the result in a variable.command-line options: Handles positional parameters such as $1, $2, and $3 depending on the option's position. Command-line options can also include flags such as -h for help, or -v depending on a Bash script's functionalities.Getopts: Provides a powerful, structured way to handle command-line options and arguments in a script. Getopts provides advanced functionalities by decoding the user input to specific actions defined in a script. This is important when handling user input with loops or conditional statements.This article explains how to handle and process user input in Bash using the read, command-line options, and getopts commands.
The read command in Bash captures user input and stores the result in a variable. You can retrieve the variable's value to view or manipulate the user input using commands such as echo, conditional statements, or loops in a script. read uses the following command syntax.
In the command, read prompts for user input and variable stores the input value.
Create a new read-command.sh file.
Add the following Bash script contents to the file.
Save and close the file.
The above script uses the read command to prompt for a user input and stores the captured data into the name variable. The echo command retrieves the name variable's value and displays it in the output.
Run the script using Bash.
Enter a name such a John Doe.
Output:
Command-line options are positional parameters you can pass to a Bash script when executing a command. Positional parameters follow strict order special number variables to capture the user input and parse it to a script for processing as follows:
$0: Refers to the Bash script itself.$1: Represents the first argument passed to the script.$2: Represents the second argument passed to the script.$3: Represents the third argument passed to the script.Optional flags modify a Bash script's behavior and start with a hyphen - or double-hyphen -- depending on the script's logic. For example:
-h-v--helpCommand-line options in Bash use the following syntax.
For example, create a new opts-test.sh file.
Add the following Bash script contents to the file.
Save and close the file.
The above script uses a switch case statement to process the user input depending on the command-line options parsed to the script and outputs a result.
Run the Bash script with a command-line option such as John Doe.
Output:
The Bash script processes the user input John as $1 and Doe as $2 to compare the value with a list of patterns in the case statement to execute the echo command after finding a match.
Run the Bash script without any options.
Output:
getopts is a Bash command that manages command line options and arguments during execution. getopts handles flags and options in a structured style to execute specific contents or handle errors in a script. The getopts command uses the following syntax in Bash.
In the above getopts command:
options: A string that specify the script's options or arguments. The command expects arguments if a character is followed by a colon :. For example, hello:world enables the command to search for arguments where -hello requires an argument and -world does not require. Options defined in one form do not require arguments. For example, helloworld instructs the command to search for the options -hello, -world without any arguments.opt: A variable that stores the current option being processed in the getopts command. You can use any variable names but opt and option are natively used as variables in the getopts command.[args]: Optional arguments parsed to the script instead of positional parameters in the getopts command. Options that include : require an argument. For example, in :a:b, a requires an argument and b does not require any arguments due to a missing colon : after the option.The getopts command commonly works with loops such as while to process all options in a script. Conditional statements such as case also perform different actions depending on the current option processed by getopts.
getopts Without ArgumentsCreate a new getopts.sh file.
Add the following Bash script contents to the file.
Save and close the file.
The above script uses the getopts command to handle the -a and -n command-line options. The script then processes the current option using a case statement to output a specific message that matches a pattern. The message Invalid option selected displays and the script terminates with an exit status of 1 if you provide an invalid option.
Run the script with a valid option such as -a.
Output:
Run the script with an invalid option such as -g.
Output:
Query the exit status of the last executed command.
Output:
getopts with ArgumentsCreate a new getopts_args.sh file..
Add the following Bash script contents to the file.
Save and close the file.
The above script uses the getopts command to handle the a and n command-line options that require arguments to process the user input using a case statement to output a specific message that matches a pattern. The variable OPTARG is a built-in getopts variable that stores arguments. The script terminates if the user input contains an invalid or empty argument.
Run the script using Bash with an option such as -a and the argument lamp.
Output:
You can combine read, command-line options, and getopts to capture, handle and process user input in a structured way in Bash. This is useful when creating applications that require specific options and arguments to perform specific tasks. Follow the steps below to create a Bash script that installs a web server stack on a Linux system based on the user input choices.
Create a new example.sh file.
Add the following Bash script contents to the file.
Save and close the file.
The above script prompts the user to input an option -s with a web stack name in either a short or long format such as -a or --apache. getopts handles the command-line options and uses a while loop to compare the result against a list of patterns in a case statement and executes specific commands after finding a match. The nested case statement compares the argument against another list of patterns, prompts for user confirmation, and stores the value in a user_decision variable using the if-else statement. The echo command displays a result after finding a match and the condition evaluates to true.
Run the script using Bash, specify the -s option, and the lemp argument.
Enter a name like John Doe when prompted.
The script outputs the following result after finding a match. Enter no when prompted to install the web stack.
Run the script again and specify the -h option to view the help page.
Output:
You have handled and processed user input in Bash using the read command, command-line options, and getopts. User input allows using multiple options when executing specific commands that require user action. You can integrate user input commands such as getopts with loops or conditional statements such as if-else to filter options and run specific tasks in Bash.
0 Comments
Be the first to comment and share your perspective with the community.