Contents
Creating a menu in a bash script can be quite useful and at times necessary. A bash menu can allow you to provide options for user input. Upon user selection of an option in your menu, you can run a command or sequence of commands and display outputs. There are a few ways to create menus in a bash script.
How to make a script executable in Bash?
Make the other script executable, add the #!/bin/bash line at the top, and the path where the file is to the $PATH environment variable. Then you can call it as a normal command; Or call it with the source command (alias is .) like this: source /path/to/script; Or use the bash command to execute it: /bin/bash /path/to/script;
Can you use source instead of bash script?
When you use bash instead of source, the script cannot alter environment of the parent script. The . command is POSIX standard while source command is a more readable bash synonym for . (I prefer source over . ). If your script resides elsewhere just provide path to that script.
How to avoid running bash script if a variable is not defined?
The quickest way is probably to add these two lines to the start of the script: The first line sets the nounset option in the shell running the script, which aborts if you try to expand an unset variable; the second expands $BATCHNUM in the context of a no-op, to trigger the abort before doing anything else.
How does the select function in Bash work?
The select loop will continue to run and prompt for user input until the break command is executed. To demonstrate how the select construct works, let’s take a look at the following simple example: The script will display a menu consisting of list items with an accompanying number and the PS3 prompt.
What does Bash select do on a PS3?
It is a simple calculator that prompt the user for input and performs basic arithmetic operations like addition, subtraction, multiplication, and division. When the script is executed, it displays the menu and the PS3 prompt. The user is prompted to select the operation and then to enter two numbers.
How to select characters and numbers in Bash?
When the user enters a number, the script will print the selected character and number: 1) Sheldon 2) Leonard 3) Penny 4) Howard 5) Raj Enter a number: 3 Selected character: Penny Selected number: 3 Enter a number: Usually, select is used in combination with case of if statements.
What happens if the input is ” no ” in Linux?
The user enters “yes”, in which case the function returns. The user enters “no”, in which case the we break out of the loop and execute exit 1. The read fails due to something like encountering an end-of-input or some other error, in which case the exit 1 is executed.
How to re-run the case statement if the input is?
A variation of the utility function that allows exiting through EOF (e.g. pressing Ctrl+D ): The user enters “yes”, in which case the function returns. The user enters “no”, in which case the we break out of the loop and execute exit 1.
Below is an example using the bash select loop to generate a selectable menu from a bash array, using the PS3 variable to set the user prompt, and displaying the menu and index selected. The menu display a list of animal and object and prompt the user to select one.
How are case statements used in bash script?
Case statements allow you to selectively execute a command that corresponds with the first matched pattern. For example, if we select Pizza from the multiple choice menu, it will execute the command-list that corresponds to that word. For more in-depth information read “ Using case Statements in Bash Scripting “.
When to use a menu driven shell script?
Creating menu driven scripts is helpful when the intended purpose of the script is to perform a wide variety of actions based on user input. We can also implement logic to create more complex menus wherein we can perform nesting of menus such that selecting one menu option in turns opens another menu.