Question on returning back a level with menus

Hi Agian

Sorry for all these questions...:

Last one for a whie, I promise

This is an example of a menu script I am using. It works OK...when I get to the 2nd menu level, if the command is issued or canceled, I would like to go back to the first menu (# MAIN SECTION)
This is NOT the menu I have created..its a bit large so I have created a smaller version below to make things easier...ish

#! /bin/bash

# MAIN SECTION
activeWM=$(whiptail --backtitle "Hi" --title "PRESS YES OR NO " --radiolist "PRESS YES OR NO " 30 60 20 "ON" "PRESS YES OFF" ON "OFF" "PRESS NO" OFF 3>&1 1>&2 2>&3)
echo "User selected Ok and entered $activeWM"

OPTION='ON'
 
 
if [ "$OPTION" == "$activeWM" ];
then
echo "You pressed ON('$activeWM')"
whiptail --title "You pressed ON" --yesno "This is an example of a yes/no box." 8 78
 
exitstatus=$?
if [ $exitstatus = 0 ]; then
echo "User selected Yes."
else
echo "User selected No."
fi
 
echo "(Exit status was $exitstatus)"

else
echo "You Pressed OFF('$activeWM')"
whiptail --title "You pressed OFF" --yesno "This is an example of a yes/no box." 8 78
 
exitstatus=$?
if [ $exitstatus = 0 ]; then
echo "User selected Yes."
else
echo "User selected No."
fi
 
echo "(Exit status was $exitstatus)"

 
fi

Something like a goto back to the main section..and only then can someone cancel from the top menu,,,

Thanks again

You can use a goto as you suggested. You could also wrap all of what you have inside something like:

while true
do      your current script (except add a break when you're ready to leave the loop)
done
1 Like

Hi Don

I know I am doing something pretty silly..but just cant find it :slight_smile:

I added the loop in and have done it in a few places but it just loops everything..and I normally just kill it and modify if possible

I am trying to go to the
First menu and when pressing OK, where it brings up one of 2 other menus depending on user selection
and once there if I press OK to do something or cancel...I was hoping it would to revert back to the first one again so that the user can select another option ..And ONLY when I do a cancel in the first menu, the script stops...

Its hard to explain but will work with the loop a bit and hope I can get it in the right place

Thanks again

#! /bin/bash
 
# MAIN SECTION 
while true
do
activeWM=$(whiptail --backtitle "Hi" --title "PRESS YES OR NO " --radiolist "PRESS YES OR NO " 30 60 20 "ON" "PRESS YES OFF" ON "OFF" "PRESS NO" OFF 3>&1 1>&2 2>&3)
echo "User selected Ok and entered $activeWM"
 

# 2nd Menus
OPTION='ON'
 
 
if [ "$OPTION" == "$activeWM" ];
then
echo "You pressed ON('$activeWM')"
whiptail --title "You pressed ON" --yesno "This is an example of a yes/no box." 8 78
 
exitstatus=$?
if [ $exitstatus = 0 ]; then
echo "User selected Yes."
else
echo "User selected No."
fi
 

 
else
echo "You Pressed OFF('$activeWM')"
whiptail --title "You pressed OFF" --yesno "This is an example of a yes/no box." 8 78
 
exitstatus=$?
if [ $exitstatus = 0 ]; then
echo "User selected Yes."
else
echo "User selected No."
fi
 
done
 
 
fi

Looks like you have to reverse the order of done and fi at the end of the script to make it run correctly. If you want to leave the script with <cancel>, capture the exitstatus of the first whiptail cmd and evaluate:

activeWM=$(whiptail ...)
        exitstatus=$?
        echo "User selected Ok and entered $activeWM"
        if [ $exitstatus -eq 1 ]; then break; fi;
1 Like

Hi RudiC

Will try that later and see how it goes & give u a update... Appreciate all your help with this BTY

Regards
Dan

None of the systems I use have the whiptail utility, so I can 't really test it out. But, I agree with RudiC that the done needs to be moved after the fi at the end of the script.

Just by looking at the man page, I would guess that the:

if [ $exitstatus -eq 1 ]; then break; fi;

should be changed to:

if [ $exitstatus -ne 0 ]; then break; fi;

so the user can get out of the loop if the user got out of whipcode by hitting the ESC key and if whipcode detects an error (exit code -1), as well as by the user selecting a NO or CANCEL button (exit code 1).

1 Like

@Don Cragun: Agreed. Good point. I did not test all options. Thanks!

1 Like

You are both stars

Between you, I believe its working great
"2nd menu goes back to first menu every time & I can cancel from first menu if needed

WIll modify my main script and double check all OK...

But great work by both of you...Much appricaited :smiley:

Regards

---------- Post updated at 07:23 AM ---------- Previous update was at 05:10 AM ----------

Hi

Yes, works perfect

Corrected my main script which uses quite a lot of menu's. Having 2 of them loop the way you showed me, improves my script a lot

Cheers again :smiley: