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 :)
CodePudding user response:
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
CodePudding user response:
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.
CodePudding user response:
I recently made a function that does this, all you need to do is break
to return to the previous function
https://stackoverflow.com/a/74793424/793088
selectVideoAction() {
# Needed so listVideoActions can do a check to see if a 5 was the
# last $REPLY before a 6
[ "$REPLY" != 6 ] && lReply="$REPLY"
[ $allSelected -eq 1 ] && PS3="Select an action($lAction): "\
|| PS3="Select an action: "
printf "\n"
[ "$REPLY" = 2 ] && displayVideoInformation "$1" && lAction=$REPLY
[ "$REPLY" = 3 ] && searchNameOpensubtitles "$1" && lAction=$REPLY
[ "$REPLY" = 4 ] && searchHashOpensubtitles "$1" && lAction=$REPLY
}
listVideoActions() {
local actions=("Go back to video selection") # 1
actions =("Show video information") # 2 repeatable
actions =("Search name on opensubtitles") # 3 repeatable
actions =("Search hash on opensubtitles") # 4 repeatable
[ $allSelected -eq 1 ] && actions =("Next video") `# 5` \
&& actions =("Repeat last action on remaining files"); # 6
actions =("Quit") # 5 or 7
printf "${BBlue}$2${Color_Off}\n"
[ $allSelected -eq 1 ] && PS3="Select an action($lAction): "\
|| PS3="Select an action: "
if [ $repeatLast -eq 0 ]; then
select action in "${actions[@]}"; do
selectVideoAction "$1"
if [ "$REPLY" = 1 ]; then allSelected=0; break;
elif [ "$REPLY" = 5 ] && [ $allSelected -eq 1 ]; then break
elif [ "$REPLY" = 5 ] && [ $allSelected -eq 0 ]; then exit 1
elif [ "$REPLY" = 6 ] && [ $allSelected -eq 1 ]; then
# Check that a repeatable action has previously been selected
if [ ! -z "$lAction" ]; then
# If the previous action $REPLY was to go to the next video
# then the repeatable action needs to be run before going
# to the next video
[ "$lReply" = 5 ] && REPLY=$lAction && selectVideoAction "$1"
repeatLast=1
break
fi
PS3="No last action. Select an action($lAction): "
elif [ "$REPLY" = 7 ] && [ $allSelected -eq 1 ]; then exit 1; fi
done
elif [ $repeatLast -eq 1 ]; then
# Run the last repeatable action automatically for every remaining video
sleep 2 | yes "$lAction" | select action in "${actions[@]}"; do
selectVideoAction "$1"
break
done
fi
}
listVideos() {
local lReply="" # The last action $REPLY
local lAction="" # The last repeatable action $REPLY 2, 3 or 4
local repeatLast=0 # When set to 1 $lAction is automatically repeated
local allSelected=0 # When set to 1 all videos have been selected
local options=("${videos[@]}")
options =("Select All")
local numOptions=$((${#options[@]}))
PS3="Select a video: "
select file in "${options[@]##*/}"; do
# Strip all spaces so arithmetic(( )) comparison doesn't fail
REPLY=$(echo "$REPLY" | tr -d ' ')
printf "\n"
if [ "$REPLY" = "$numOptions" ]; then
allSelected=1
local video
for video in "${videos[@]}"; do
listVideoActions "$video" "${video##*/}"
# The action $REPLY 1 was selected to return to the video list
[ $allSelected -eq 0 ] && break
done
break
elif (( "$REPLY" > 0 && "$REPLY" < "$numOptions" )); then
listVideoActions "${videos[$REPLY-1]}" "${videos[$REPLY-1]##*/}"
break
fi
done
listVideos
}