Creating Menus Shell Scripting - bash

I'm started learning shell scripting, and I'm trying to figure out a way to add an option that returns the user back to the menu.
for example:
5) echo "Return to the menu"
echo "Return back to the menu? ";;
Hear is the script on hand:
echo "1. I'm number one"
echo "2. I'm number two"
echo "3. I'm number three"
echo "4. Exit from menu "
echo -n "Enter one the following numbers:"
while :
do
read choice
case $choice in
1) echo "You have selected the number one"
echo "Selected number is one";;
2) echo "You have selected the number two"
echo "Selected number is two";;
3) echo "You have selected the number three"
echo "Selected number is three";;
4) echo "Exiting after the information have been received by both devices" # Referring to the TCP/IP protocol. The information has to be established by both client and device to act like a server before data is sent. Ok I'm showing of here :)
exit;
esac
echo -n "Enter one of the four options"
done # Sorry if there are errors in the this code, I wrote it on the fly :)

You can also use select to create a menu
#! /bin/bash
opts=( "I'm number one" "I'm number two" "I'm number three" "Exit from menu" )
PS3="Enter one the following numbers:"
select o in "${opts[#]}"
do
case "$REPLY" in
1) echo "You have selected the number one: $o"
echo "Selected number is one";;
2) echo "You have selected the number two: $o"
echo "Selected number is two";;
3) echo "You have selected the number three: $o"
echo "Selected number is three";;
4) echo "Exiting after the information have been received by both devices" # Referring to the TCP/IP protocol. The information has to be established by both client and device to act like a server before data is sent. Ok I'm showing of here :)
exit;;
esac
done

TLDR ;) Put the usage in it's own function and call that when you want
I'd do something like this(shortened for brevity)
#!/bin/bash
printUsage() {
echo "Enter one the following options:"
echo "1: do 1 stuff"
echo "x: exit"
# add more as necessary
}
printUsage
while true; do
read choice
case $choice in
1)
echo "You have selected the number one"
break
;;
x)
echo "k thx bye"
exit
;;
*)
echo ""
echo "Invalid option: $choice"
echo ""
printUsage
;;
esac
done
echo "Doing $choice stuff"

To add a return to menu feature, try this:
Add a variable accessMenu, set it to true, and update the while loop condition as so, and set accessMenu to false first thing in the loop...
accessMenu=true
while [[ $accessMenu -eq true ]]; do
accessMenu=false
... (code)
done
Lastly, in the case that you want to enable returning to the menu, set the variable to true, and it will pass the condition in the while loop to run again.

Ok so i figured that i can wrap it up inside a function and then run the function whenever i wanted to return back to the menu.
myfunc()
{
echo "1. I'm number one"
echo "2. I'm number two"
echo "3. I'm number three"
echo "4. Return to the menu"
echo "5. Exit from menu "
echo -n "Enter one the following numbers:"
while :
do
read choice
case $choice in
1) echo "You have selected the number one"
echo "Selected number is one";;
2) echo "You have selected the number two"
echo "Selected number is two";;
3) echo "You have selected the number three"
echo "Selected number is three";;
4) echo "Return to the menu"
myfunc;;
5) echo "Exiting after the information have been received by both devices"
exit;
esac
echo -n "Enter one of the four options"
done
}
myfunc

Related

bash menu script exit

I am writing a menu.
This is the script I have.
when I type q it dose not echo "done"
what I need to change in the script to get it to echo "done"?
#!/bin/bash
rap '' 2
while true
do
clear
echo "=============================="
echo "menu"
echo "=============================="
echo "Enter 1 to for option 1t: "
echo "Enter q to exit:"
echo -e "\n"
echo -e "Enter your selection"
read answer
case "$answer" in
1) echo "option 1"
echo "Option 1"
done;;
q) exit
echo "done" ;;
esac
echo -e "Enter return to continue \c"
read input
done
exit 0
there were some minor bugs in your code. 1) case does must not have done. 2) exit before calling echo does not make sense.
while true; do
clear
echo "=============================="
echo "menu"
echo "=============================="
echo "Enter 1 to for option 1t: "
echo "Enter q to exit:"
echo -e "\n"
echo -e "Enter your selection"
read answer
case "$answer" in
1) echo "option 1"
echo "Option 1";;
q) echo "done"
exit
;;
esac
echo -e "Enter return to continue \c"
read input
done
exit 0

How do i stop echo in loop and re-run case statement?

Pretty new to bash scripting , I was learning how to use multiple cases and inputs in case statement.
#!/usr/bin/env bash
echo "Choose from 1,2"
read -p "Enter here : " input1
echo "Choose from a,b"
read -p "Enter here : " input2
while true; do
case $input1+$input2 in
1+a )
echo "You have chosen 1 and a"
break;;
1+b )
echo "You have chosen 1 and b"
break;;
2+a )
echo "You have chosen 2 and a"
break;;
2+b )
echo "You have chosen 2 and b"
break;;
*+* )
echo "Invalid input";;
esac
done
PS \ I know i can use single wildcard asterisk in to cover remaining possibilities , i just used it twice for times when i need wildcard only for single case and not both of them together.
Currently
Everything else works fine , but if i try to invoke invalid output by entering random numbers , it is infinitely spamming - Invalid output .
Expectation
if user inputs anything which is not covered by case statement , echo "invalid output" only once and rerun the case statement ( ie echo those 2 questions again )
Can someone please suggest how do i fix it
Thanks to all the suggested comments , i came up with this
while true; do
echo "Choose from 1,2"
read -p "Enter here : " input1
case $input1 in
1|2 )
break;;
* )
echo "Enter Valid Input"
esac
done
while true; do
echo "Choose from a,b"
read -p "Enter here : " input2
case $input2 in
a|b )
break;;
* )
echo "Enter Valid Input"
esac
done
case $input1+$input2 in
1+a )
echo "You have chosen 1 and a";;
1+b )
echo "You have chosen 1 and b";;
2+a )
echo "You have chosen 2 and a";;
2+b )
echo "You have chosen 2 and b";;
esac

Check for invalid input in a menu

I am making a script where the user selects a number 1-5 and it will loop until the user enters 5. I don't want to use the exit command. I wanted to check to make sure the user doesn't enter anything but 1-5 and if they do display invalid input.
any help would be appreciated
#!/bin/bash
PS3='Please enter your choice: '
options=("1.Move empty files" "2.Check file size" "3.Which files is newer" "4.File check rwx" select opt in "${options[#]}")
while($opt != 5); do
case $opt in
"Option 1")
echo "you chose choice 1"
;;
"Option 2")
echo "you chose choice 2"
;;
"Option 3")
echo "you chose choice 3"
;;
"Option 4")
echo "you chose choice 3"
;;
"Option 5")
break
;;
) echo invalid input;;
You seem confused. I don't even know where to begin correcting whatever misconceptions you have about how this works
In your original code the way you set options is unlikely to do anything useful.
options=("1.Move empty files" "2.Check file size" "3.Which files is newer" "4.File check rwx" select opt in "${options[#]}"
printf '%s\n' "${options[#]}"
This will emit
1.Move empty files
2.Check file size
3.Which files is newer
4.File check rwx
select
opt
in
The select command will not have been executed.
Here's something that does what you seem to want.
options=(
'Move empty files'
'Check file size'
'Which file is newer'
'File check rwx'
)
PS3='Please enter your choice: '
select opt in "${options[#]}" ; do
[[ -n $opt ]] && break || {
echo "invalid input"
}
done
echo "user chose '$opt'"
You could go with a while and case solution and get nearly the same results e.g.:
options=(
'Move empty files'
'Check file size'
'Which file is newer'
'File check rwx'
)
for (( i=0 ; i < ${#options[#]} ; i++ )) ; do
printf '%d) %s\n' $((i+1)) "${options[$i]}"
done
while true ; do
read -p 'Please enter your choice: ' opt
case "$opt" in
[1-5])
opt="${options[$opt]}"
break
;;
*)
echo "invalid input"
;;
esac
done
echo "user chose '$opt'"
But you don't need both and, as you can see, using select is much simpler.

Return to previous commands in bash script?

I'm trying to implement a prev option in my bash script to go back to the previous "menu", as well as a way for the script to ask for user input again if no variable is set for $name.
Heres my bash script:
#!/bin/bash
#Menu() {
for (( ; ; ))
do
beginORload=
echo "Choose option:"
echo "1 - Begin"
echo "2 - Load"
read -p "?" beginORload
#}
#Begin() {
if [ "$beginORload" -eq "1" ]
then
clear
for (( ; ; ))
do
echo "Beginning. What is your name?"
read -p "?" name
#If "prev" specified, go back to #Menu()
if [ "$name" -eq "prev" ]
then
Menu
fi
#If nothing specified, return to name input
if [ -z ${name+x} ]
then
Begin
else
break
fi
echo "Hi $name !"
done
fi
done
In batch, I could simply do:
:menu
echo Choose option:
echo 1 - Begin
echo 2 - Load
[...]
:begin
[...]
if "%name%==prev" goto menu
if "%name%==" goto begin
the issue is I keep running into errors all over the place, and I can't figure out what to type to get it to work
im running Yosemite btw. Thankyou
Something like this is close to what you expect:
while [[ $answer -ne '3' ]];do
echo "Choose option:"
echo "1 - Begin"
echo "2 - Load"
echo "3 - Exit"
read -p "Enter Answer [1-2-3]:" answer
case "$answer" in
1) while [[ "$nm" == '' ]];do read -p "What is your Name:" nm;done # Keep asking for a name if the name is empty == ''
if [[ $nm == "prev" ]];then nm=""; else echo "Hello $nm" && break; fi # break command breaks the while wrapper loop
;;
2) echo 'Load' ;;
3) echo 'exiting...' ;; # Number 3 causes while to quit.
*) echo "invalid selection - try again";; # Selection out of 1-2-3 , menu reloaded
esac # case closing
done # while closing
echo "Bye Bye!"
As a general idea you can wrap up your case selection in a while loop which will break under certain circumstances (i.e If Option 3 is selected or if a valid name is given (not blank - not prev)
PS1: In bash you compare integers with -eq , -ne, etc but you compare strings with == or !=
PS2: Check the above code online here

how to create a do-while loop using bash?

I want the Menu to run once and then ask if the user wants it to run again, if they say yes it it continues to run until they say no. I saw some examples online but im not understanding how to implement it.
#Menu
echo "Menu"
echo "1. Display the message that indicates when the <script> command stared"
echo "2. Dispay the message that indicates when the <script> command terminated"
echo "3. game score"
read answer
if[[ $answer == 1 ]]; then
echo""
elif[[ $answer == 2 ]]; then
echo""
elif [[ $answer == 3]]; then
fi
echo "Do you want to repeat this routine?(Y/N)"
read yesno
Other approach:
#the action functions
do_script_start() {
echo "this is a message - start"
}
do_script_term() {
echo "this is a message - term"
}
do_score() {
echo "this is a game score"
}
# main
ans=(
"Display the message that indicates when the <script> command stared"
"Dispay the message that indicates when the <script> command terminated"
"game score"
"exit menu"
)
PS3="Select what you want>"
select answer in "${ans[#]}"
do
case "$REPLY" in
1) do_script_start ;;
2) do_script_term ;;
3) do_score ;;
4) break ;;
esac
done
echo "here..."

Resources