Bash handle multiple user menu choices with same variables - bash

I currently have script which handles user choice based on presented menu and depending on what option is selected, variables are assigned values and some variables are assigned values based on if conditions. Then some functions are called using that variables. Simplified logic is here
PS3='Please make your choice'
options=("Option 1" "Option 2" "Quit")
COLUMNS=0
select opt in "${options[#]}"
do
case $opt in
"Option 1")
varA="Value1"
varB="Text1"
if something; then
varC="String1"
else
varC="String2"
fi
break
;;
"Option 2")
varA="Value2"
varB="Text2"
break
;;
"Quit")
exit 0
;;
*)
echo $red "Invalid option $REPLY" $txtreset;;
esac
prepare "${varA}" "${varB}"
update "${varA}" "${varB}" "${varC}"
So this logic allows one user choice and then processing happens based on that choice.
Is it possible to capture multiple users choices, like Option 1 and Option 2 for example and then run further script steps in a loop twice. Once using variable values from 1st choice and second time using variable values from 2nd choice.?

Related

Trying to use a select/case inside a for loop in bash

I have created a for loop and wanted to prompt the user after each item when they select Yes and proceeds with the loop. So ideally the experience would be first prompted with question "Do you wish to proceed", then Yes is selected and output would be "we are at item a", then the prompt shows again asking "Do you wish to proceed?" and if Yes is selected a second time the output "we are at item b" displays. I am having the issue that it is always stuck on "item a" no matter what is inputted.
array_of_items=("a" "b" "c")
for i in ${array_of_items}; do
echo "Do you wish to proceed?"
select yn in "Yes" "No"; do
case $yn in
Yes)
echo "we are at item $i"
echo "proceeding to next item";;
No)
exit;;
esac
done
done
A select block will automatically repeat until it hits a break command (a bit like "while true"), so your Yes) case needs to end with a break.
Additionally, $var and ${var} always expand to the first element of the array. You need ${var[#]} to get all elements, like this:
for i in "${array_of_items[#]}"; do
...
done

bash: choose default from case when enter is pressed in a "select" prompt

I'm prompting questions in a bash script like this:
optionsAudits=("Yep" "Nope")
echo "Include audits?"
select opt in "${optionsAudits[#]}"; do
case $REPLY in
1) includeAudits=true; break ;;
2) includeAudits=false; break ;;
"\n") echo "You pressed enter"; break ;; # <--- doesn't work
*) echo "What's that?"; exit;;
esac
done
How can I select a default option when enter is pressed? The "\n" case does not catch the enter key.
To complement Aserre's helpful answer, which explains the problem with your code and offers an effective workaround, with background information and a generic, reusable custom select implementation that allows empty input:
Background information
To spell it out explicitly: select itself ignores empty input (just pressing Enter) and simply re-prompts - user code doesn't even get to run in response.
In fact, select uses the empty string to signal to user code that an invalid choice was typed.
That is, if the output variable - $opt, int this case - is empty inside the select statement, the implication is that an invalid choice index was typed by the user.
The output variable receives the chosen option's text - either 'Yep' or 'Nope' in this case - not the index typed by the user.
(By contrast, your code examines $REPLY instead of the output variable, which contains exactly what the user typed, which is the index in case of a valid choice, but may contain extra leading and trailing whitespace).
Note that in the event that you didn't want to allow empty input, you could
simply indicate to the user in the prompt text that ^C (Ctrl+C) can be used to abort the prompt.
Generic custom select function that also accepts empty input
The following function closely emulates what select does while also allowing empty input (just pressing Enter). Note that the function intercepts invalid input, prints a warning, and re-prompts:
# Custom `select` implementation that allows *empty* input.
# Pass the choices as individual arguments.
# Output is the chosen item, or "", if the user just pressed ENTER.
# Example:
# choice=$(selectWithDefault 'one' 'two' 'three')
selectWithDefault() {
local item i=0 numItems=$#
# Print numbered menu items, based on the arguments passed.
for item; do # Short for: for item in "$#"; do
printf '%s\n' "$((++i))) $item"
done >&2 # Print to stderr, as `select` does.
# Prompt the user for the index of the desired item.
while :; do
printf %s "${PS3-#? }" >&2 # Print the prompt string to stderr, as `select` does.
read -r index
# Make sure that the input is either empty or that a valid index was entered.
[[ -z $index ]] && break # empty input
(( index >= 1 && index <= numItems )) 2>/dev/null || { echo "Invalid selection. Please try again." >&2; continue; }
break
done
# Output the selected item, if any.
[[ -n $index ]] && printf %s "${#: index:1}"
}
You could call it as follows:
# Print the prompt message and call the custom select function.
echo "Include audits (default is 'Nope')?"
optionsAudits=('Yep' 'Nope')
opt=$(selectWithDefault "${optionsAudits[#]}")
# Process the selected item.
case $opt in
'Yep') includeAudits=true; ;;
''|'Nope') includeAudits=false; ;; # $opt is '' if the user just pressed ENTER
esac
Alternative implementation that lets the function itself handle the default logic:Thanks, RL-S
This implementation differs from the above in two respects:
It allows you to designate a default among the choices, by prefixing it with !, with the first choice becoming the default otherwise. The default choice is printed with a trailing [default] (and without the leading !). The function then translates empty input into the default choice.
The selected choice is returned as a 1-based index rather than the text. In other words: you can assume that a valid choice was made when the function returns, and that choice is indicated by its position among the choices given.
# Custom `select` implementation with support for a default choice
# that the user can make by pressing just ENTER.
# Pass the choices as individual arguments; e.g. `selectWithDefault Yes No``
# The first choice is the default choice, unless you designate
# one of the choices as the default with a leading '!', e.g.
# `selectWithDefault Yes !No`
# The default choice is printed with a trailing ' [default]'
# Output is the 1-based *index* of the selected choice, as shown
# in the UI.
# Example:
# choice=$(selectWithDefault 'Yes|No|!Abort' )
selectWithDefault() {
local item i=0 numItems=$# defaultIndex=0
# Print numbered menu items, based on the arguments passed.
for item; do # Short for: for item in "$#"; do
[[ "$item" == !* ]] && defaultIndex=$(( $i + 1)) && item="${item:1} [default]"
printf '%s\n' "$((++i))) $item"
done >&2 # Print to stderr, as `select` does.
# Prompt the user for the index of the desired item.
while :; do
printf %s "${PS3-#? }" >&2 # Print the prompt string to stderr, as `select` does.
read -r index
# Make sure that the input is either empty or that a valid index was entered.
[[ -z $index ]] && index=$defaultIndex && break # empty input == default choice
(( index >= 1 && index <= numItems )) 2>/dev/null || { echo "Invalid selection. Please try again." >&2; continue; }
break
done
# Output the selected *index* (1-based).
printf $index
}
Sample call:
# Print the prompt message and call the custom select function,
# designating 'Abort' as the default choice.
echo "Include audits?"
ndx=$(selectWithDefault 'Yes' 'No', '!Abort')
case $ndx in
1) echo "include";;
2) echo "don't include";;
3) echo "abort";;
esac
Optional reading: A more idiomatic version of your original code
Note: This code doesn't solve the problem, but shows more idiomatic use of the select statement; unlike the original code, this code re-displays the prompt if an invalid choice was made:
optionsAudits=("Yep" "Nope")
echo "Include audits (^C to abort)?"
select opt in "${optionsAudits[#]}"; do
# $opt being empty signals invalid input.
[[ -n $opt ]] || { echo "What's that? Please try again." >&2; continue; }
break # a valid choice was made, exit the prompt.
done
case $opt in # $opt now contains the *text* of the chosen option
'Yep')
includeAudits=true
;;
'Nope') # could be just `*` in this case.
includeAudits=false
;;
esac
Note:
The case statement was moved out of the select statement, because the latter now guarantees that only valid inputs can be made.
The case statement tests the output variable ($opt) rather than the raw user input ($REPLY), and that variable contains the choice text, not its index.
Your problem is due to the fact that select will ignore empty input. For your case, read will be more suitable, but you will lose the utility select provides for automated menu creation.
To emulate the behaviour of select, you could do something like that :
#!/bin/bash
optionsAudits=("Yep" "Nope")
while : #infinite loop. be sure to break out of it when a valid choice is made
do
i=1
echo "Include Audits?"
#we recreate manually the menu here
for o in "${optionsAudits[#]}"; do
echo "$i) $o"
let i++
done
read reply
#the user can either type the option number or copy the option text
case $reply in
"1"|"${optionsAudits[0]}") includeAudits=true; break;;
"2"|"${optionsAudits[1]}") includeAudits=false; break;;
"") echo "empty"; break;;
*) echo "Invalid choice. Please choose an existing option number.";;
esac
done
echo "choice : \"$reply\""
Updated answer:
echo "Include audits? 1) Yep, 2) Nope"
read ans
case $ans in
Yep|1 ) echo "yes"; includeAudits=true; v=1 ;;
Nope|2 ) echo "no"; includeAudits=false; v=2 ;;
"" ) echo "default - yes"; includeAudits=true; v=1 ;;
* ) echo "Whats that?"; exit ;;
esac
This accepts either "Yep" or "1" or "enter" to select yes, and accepts "Nope" or "2" for no, and throws away anything else. It also sets v to 1 or 2 depending on whether the user wanted yes or no.
This will do what you are asking for.
options=("option 1" "option 2");
while :
do
echo "Select your option:"
i=1;
for opt in "${options[#]}"; do
echo "$i) $opt";
let i++;
done
read reply
case $reply in
"1"|"${options[0]}"|"")
doSomething1();
break;;
"2"|"${options[1]}")
doSomething2();
break;;
*)
echo "Invalid choice. Please choose 1 or 2";;
esac
done
Assuming that your default option is Yep:
#!/bin/bash
optionsAudits=("Yep" "Nope")
while : #infinite loop. be sure to break out of it when a valid choice is made
do
i=1
echo "Include Audits?: "
#we recreate manually the menu here
for o in "${optionsAudits[#]}"; do
echo " $i) $o"
let i++
done
read -rp "Audit option: " -iYep
#the user can either type the option number or copy the option text
case $REPLY in
"1"|"${optionsAudits[0]}") includeAudits=true; break;;
"2"|"${optionsAudits[1]}") includeAudits=false; break;;
"") includeAudits=true; break;;
*) echo "Invalid choice. Please choose an existing option number.";;
esac
done
echo "choice : \"$REPLY\""
echo "includeAudits : \"$includeAudits\""
Noticed the line:
read -rp "Audit option: " -eiYep
Also I pulled up $reply to $REPLY so that the case decision works better.
The output would now look like this upon hitting ENTER:
Include Audits?:
1) Yep
2) Nope
Audit option: Yep
choice : ""
includeAudits : "true"
#
As an enhancement over select, read -eiYep will supply Yep default value into the input buffer up front.
Only downside of fronting the default value is that one would have to press backspace a few times to enter in their own answer.

Bash - Iterating over select menu options after choosing an option?

I am reading up on Select menus in Bash:
Making menus with the select built-in
I am quite confused when it comes to iterating over the options in the menu and would appreciate any feedback on this.
Using the code here as a starting point:
How can I create a select menu in a shell script?
We get the following output:
root#dev:~/# ./test.sh
1) Option 1
2) Option 2
3) Option 3
4) Quit
Please enter your choice: 1
you chose choice 1
Please enter your choice: 2
you chose choice 2
Please enter your choice: 4
root#dev:~/#
What I would like to try to do, is show the select options again once an option has been chosen, so the output would be like:
root#dev:~/# ./example.sh
1) Option 1
2) Option 2
3) Quit
Please enter your choice: 1
you chose choice 1
1) Option 1
2) Option 2
3) Quit
Please enter your choice: 2
you chose choice 2
1) Option 1
2) Option 2
3) Quit
Please enter your choice: 3
root#dev:~/#
So I have given this a Bash (L) and and tried to loop through the options (array?):
#!/bin/bash
PS3='Please enter your choice: '
options=("Option 1" "Quit")
select opt in "${options[#]}"
do
case $opt in
"Option 1")
echo "you chose choice 1"
# Create an incrementing value
loop=1;
# Loop through each option
for option in ${options[#]} ; do
# Echo the option
echo "$loop) $option";
# Increment the loop
loop=$((loop+1));
done
;;
"Quit")
break
;;
*) echo invalid option;;
esac
done
But then I get an output like:
root#dev:~/# ./stumped.sh
1) Option 1
2) Quit
Please enter your choice: 1
you chose choice 1
1) Option
2) 1
3) Quit
Please enter your choice:
So it seems that the array values are separated by spaces here?
To my understanding: options=("Option 1" "Quit") is creating an array of 2 values and not of 3 however it is being interpreted in Bash as 3 and I am not sure why.
Could someone enlighten me and explain why this is happening?
Let's create a function that shows the menu and echoes the user's choice:
function show_menu {
select option in "${OPTIONS[#]}"; do
echo $option
break
done
}
Now, we can wrap that function in a loop, and only break out if the user picked Quit:
while true; do
option=$(show_menu)
if [[ $option == "Quit" ]]; then
break
fi
done
Voila!
When you want the numbered options shown each iteration, let select do that for you.
options=("Option one" "two" three "and number four" quit)
while true; do
select item in "${options[#]}" ; do
if [ -n "${item}" ]; then
break
fi
echo "Sorry, please enter a number as shown."
break
done;
if [[ "${item}" = "quit" ]]; then
break
fi
if [ -n "${item}" ]; then
echo "Your answer is ${item}"
fi
done

How to use goto statement in shell script [duplicate]

This question already has answers here:
Is there a "goto" statement in bash?
(14 answers)
Closed 4 years ago.
I am beginner in shell script. I don't have any idea about how to use goto statement. I am using the following code.
start:
echo "Main Menu"
echo "1 for Copy"
echo "2 for exit"
read NUM
case $NUM in
"1")
echo "CopyNUM"
goto start:;
"2")
echo "Haiiii";
goto start:
*)
echo "ssss";
esac
As others have noted, there's no goto in bash (or other POSIX-like shells) - other, more flexible flow-control constructs take its place.
Look for heading Compound Commands in man bash.
In your case, the select command is the right choice.
Since how to use it may not be obvious, here's something to get you started:
#!/usr/bin/env bash
echo "Main Menu"
# Define the choices to present to the user, which will be
# presented line by line, prefixed by a sequential number
# (E.g., '1) copy', ...)
choices=( 'copy' 'exit' )
# Present the choices.
# The user chooses by entering the *number* before the desired choice.
select choice in "${choices[#]}"; do
# If an invalid number was chosen, $choice will be empty.
# Report an error and prompt again.
[[ -n $choice ]] || { echo "Invalid choice." >&2; continue; }
# Examine the choice.
# Note that it is the choice string itself, not its number
# that is reported in $choice.
case $choice in
copy)
echo "Copying..."
# Set flag here, or call function, ...
;;
exit)
echo "Exiting. "
exit 0
esac
# Getting here means that a valid choice was made,
# so break out of the select statement and continue below,
# if desired.
# Note that without an explicit break (or exit) statement,
# bash will continue to prompt.
break
done
Here is a short example using a select loop to accomplish your goal. You can use a while loop with a custom menu if you want custom formatting, but the basic menu is what select was designed to do:
#!/bin/bash
## array of menu entries
entries=( "for Copy"
"for exit" )
## set prompt for select menu
PS3='Selection: '
while [ "$menu" != 1 ]; do ## outer loop redraws menu each time
printf "\nMain Menu:\n\n" ## heading for menu
select choice in "${entries[#]}"; do ## select displays choices in array
case "$choice" in ## case responds to choice
"for Copy" )
echo "CopyNUM"
break ## break returns control to outer loop
;;
"for exit" )
echo "Haiiii, exiting"
menu=1 ## variable setting exit condition
break
;;
* )
echo "ssss"
break
;;
esac
done
done
exit 0
Use/Output
$ bash select_menu.sh
Main Menu:
1) for Copy
2) for exit
Selection: 1
CopyNUM
Main Menu:
1) for Copy
2) for exit
Selection: 3
ssss
Main Menu:
1) for Copy
2) for exit
Selection: 2
Haiiii, exiting

Bash script, case statement and sub-menus

I wish to run a script with a case statement leading to choices between other lists of choices (sort of sub-menus) between scripts :
#!/bin/bash
echo "Your choice ?"
echo "1) Foo"
echo "2) Bar"
echo "3) Stuff"
read case;
case $case in
#and this is where I would like to allow
#(here a simplified example I do not manage to code)
1) What script ? # may I use another case statement ?
a) Foo1;;
b) Foo2;;
2) What script ? # d°
a) Bar1;;
b) Bar2;;
c) Bar3;;
esac
Foo1, Foo2, Bar1, Bar2 and Bar3 being in fact bash scripts, I would like to call, eg, sh Foo1 from within the script.
How must I proceed :
May I include a case statement within a case statement (better than if statement, if the choices are numerous) ?
And how do I call a script from within another script ?
Thanks in advance for your help
ThG
Dennis' answer should do what you want. Just to illustrate how to do it with functions, so as to make the script more readable:
function which_foo {
local foo_options=("foo1" "foo2" "cancel")
local PS3="Select foo: "
select foo in "${foo_options[#]}"
do
case $REPLY in
1) ./foo1.sh
break ;;
2) ./foo2.sh
break ;;
3) break ;;
esac
done
}
ACTIONS=("foo" "bar" "quit")
PS3="Select action: "
select action in "${ACTIONS[#]}"
do
case $action in
"foo") which_foo
break ;;
"bar") which_bar # Not present in this example
break ;;
"quit") break ;;
esac
done
As an aside, note:
the use of $REPLY in the case inside which_foo, which is automatically set to the number selected by the user, instead of the text of the option.
the use of break in each case options. Without them, select puts you in a loop, so the user would be asked to enter a choice again.
that select never changes the value of PS3 for you, so you have to do it yourself when switching between the different levels of your menu.
Yes, you can nest case statements. You should also consider using a select statement instead of all those echo and read statements.
options=("Option 1" "Option 2" "Option3" "Quit")
optionsprompt='Please enter your choice: '
sub1=("Option 1 sub 1" "Option 1 sub 2")
sub1prompt='Please enter your choice: '
PS3=$optionsprompt
select opt in "${options[#]}"
do
case $opt in
"Option 1")
echo "you chose choice 1"
PS3=$sub1prompt
select sub1opt in "${sub1[#]}"
do
case $sub1opt in
"Option 1 sub 1")
echo "you chose choice 2"
;;
"Option 1 sub 2")
echo "you chose choice 2"
;;
esac
done
;;
"Option 2")
echo "you chose choice 2"
;;
"Option 2")
echo "you chose choice 3"
;;
"Quit")
break
;;
*) echo invalid option;;
esac
done
The first level menu would look like this:
1) Option 1
2) Option 2
3) Option3
4) Quit
Please enter your choice:
However, that can get very hairy very quickly. It would be better to break it up into functions where the inner select/case statements are in separate functions or are abstracted so you can pass arguments to control them.
To call another script, you might want to set a variable that holds the directory. It would look something like this:
basedir=/some/path
case ...
...
$basedir/Foo # call the script

Resources