[bash] Zenity loop (raspbian)

Hello folks,

I have created a GUI but i face some issue with the loop either i am stuck in the loop or it close the program

In the code below it work fine for the "go to menu" if i press cancel i go back to the main menu but for "test" i m stuck in the menu

Appreciate some help :confused:

#!/bin/bash
 
while true; do
  choice="$(zenity --width=200 --height=150 --list --column "" --title="test" \
  "Go to next menu" \
 "test"
  "Exit ")"
 
  case "${choice}" in
    "Go to next menu" )
      while true; do
        choice2="$(zenity --width=200 --height=150 --list --column "" --title="test" \
        "Do Something" \
        "Do Something Else " \
        "Back")"
 
        case "${choice2}" in
          "Do Something" )
            echo "hello"
          ;;
          "Do Something Else " )
            echo "hello"
          ;;
          *)
            break
          ;;
        esac
      done
    ;;
    *)
      break
    ;;
esac
done
 
while true; do
  case "${choice}" in
    "test" )
 
      while true; do
        choice3="$(zenity --width=200 --height=150 --list --column "" --title="test" \
        "Do Something" \
        "Do Something Else " \
        "Back")"
 
        case "${choice3}" in
          "Do Something" )
            echo "hello"
          ;;
          "Do Something Else " )
            echo "hello"
          ;;
 
          *)
            break
          ;;
 
        esac
      done
 
    ;;
    *)
      break
    ;;
  esac
done

Not sure I fully understand the logics you tried to put into your script, but it seems there may be a flaw in it:
You have two sequential while loops; in the first you "read" a value into choice and then run a case statement on that variable, break ing out of the loop unless "Go to next menu" is selected. When entering the second loop, choice is never modified; so, if it holds "test" it is in an infinite loop. Any other value will make it break out immediately.

well i got lost trying to make it work.

I woul like a main menu to redirect to sub menu .

I don't understand how to make a loop for the second menu in order to get back to the main when i press cancel.

I also try this but only " go to menu" appear on the main

#!/bin/bash
 
while true; do
  choi1="$(zenity --width=200 --height=150 --list --column "" --title="test" \
  "Go to next menu" \	
 
 
  "Exit ")"
 
  case "${choi1}" in
    "Go to next menu" )
      while true; do
        choi1="$(zenity --width=200 --height=150 --list --column "" --title="test" \
        "Do Something" \
        "Do Something Else " \
        "Back")"
 
        case "${choi1}" in
          "Do Something" )
            echo "hello"
          ;;
          "Do Something Else " )
            echo "hello"
          ;;
          *)
            break
          ;;
        esac
      done
    ;;
    *)
      break
    ;;
esac
done
 
while true; do
  choi2="$(zenity --width=200 --height=150 --list --column "" --title="test" \
  "test2" 
 
case "${choi2}" in
    "test2" )
      while true; do
        choi2="$(zenity --width=200 --height=150 --list --column "" --title="test" \
        "Do Something" \
        "Do Something Else " \
        "Back")"
 
        case "${choi2}" in
          "Do Something" )
            echo "hello"
          ;;
          "Do Something Else " )
            echo "hello"
          ;;
 
		 *)
 
            break
 
          ;;
        esac
      done
    ;;
 
 
     *)
      break
    ;;
esac
done

Don't create a second "while true" loop, but make another entry "test" in the first loop's case ... esac statement, and, below it, the sub menu.

okay so should look like this?

#!/bin/bash

while true; do
  choice="$(zenity --width=200 --height=150 --list --column "" --title="test" \
  "Go to next menu" \
 "test"
  "Exit ")"

  case "${choice}" in
    "Go to next menu" )
      while true; do
        choice2="$(zenity --width=200 --height=150 --list --column "" --title="test" \
        "Do Something" \
        "Do Something Else " \
        "Back")"

        case "${choice2}" in
          "Do Something" )
            echo "hello"
          ;;
          "Do Something Else " )
            echo "hello"
          ;;
          *)
            break
          ;;
        esac
      done
    ;;
    *)
      break

    ;;
esac
done

  case "${choice}" in
    "test" )
      while true; do
        choice3="$(zenity --width=200 --height=150 --list --column "" --title="test" \
        "Do Something" \
        "Do Something Else " \
        "Back")"

        case "${choice3}" in
          "Do Something" )
            echo "hello"
          ;;
          "Do Something Else " )
            echo "hello"
          ;;
          *)
            break
          ;;
        esac
      done
    ;;
    *)
      break
    ;;
  esac
done
   

if i remove the first esac done (the one outisiide the case)the progrmam does not start then if i put it a the end same issue
in this code i the loop only work for "go to menu"

NO. Just one single outer case ... esac .
Just to SEE the logics, try a top-down approach:

while true
  do    read choice
        case $choice in
                "go to menu")   ... ;;
                "test")         ... ;;
                "exit")         ... ;;
                *)              error handling ;;
        esac
  done

, then refine/detail each of the menu items. As you can see, proper indenting helps to follow the logics and keeps things together that belong together.

I would suggest that having to rely on break is probably a bad idea. The only place where i think it might be the only solution is within a select loop.

I would suggest that you change your loop from a while true to being based on a condition that is initialised and then changed when you want to leave the loop.

keep_looping=1
while [ $keep_looping -eq 1 ]
do
   ...
   ...
   ...
   if [ some condition ]
   then
      echo "Leaving loop"
      keep_looping=0
   fi
done

Okay guys thanks for your help but i'm completly lost.

Rudic i tryed this....

#!/bin/bash

while true
  do   choice="$(zenity --width=200 --height=150 --list --column "" --title="test" 
 
        case $choice in
                "go to menu")   ... ;;
                "test")         ... ;;
                "exit")         ... ;;
                *)              error handling ;;
        esac
  done

rbatte1 i tryed your code i am able to get the main menu but when i want the sub menu gets complicated . i don't get with the if how to select the variable and apply it

how can i say if [select "go to menu" do .... ]
the code:

#!/bin/bash
keep_looping=1
while [ $keep_looping -eq 1 ]
do choice="$(zenity --width=200 --height=150 --list --column "" --title="test" \
  "Go to next menu" \
 "test"
  "Exit ")"
   
   if [  "${choice}" in     
    "Go to next menu" )
      while true; do
        choice2="$(zenity --width=200 --height=150 --list --column "" --title="test" \
        "Do Something" \
        "Do Something Else " \
        "Back")" 
  
        
      done  ]
   then
      echo "Leaving loop"
      keep_looping=0
   fi
done

Did you REALLY try that pseudo code verbatim?

well i though i could manage to do some with it.

And what did you do with it?

absolutly nothing !

---------- Post updated at 10:54 AM ---------- Previous update was at 10:40 AM ----------

i agree i am from understanding well bash i just need example related to my code to get it...

Maybe there's a misunderstanding here.

When someone posts a problem in these forums, quite a lot of volunteers read it and might want to jump in to help, sometimes giving hints, sometimes pointing out errors, sometimes, if you're lucky, providing an entire working program, eventually even including error handling. The latter shouldn't be expected. And, it can happen only if the situation is absolutely clear, i.e. the requirements are clearly stated, own attempts and representative input data are posted, a desired output is shown, and possible error messages are depicted.

This is not the case with your posts. Unless you give us way more details, I see the help we can give is very general hints only. Like the structural approach models given. You would need to apply some creativity to adapt those to your own problem. If you get stuck, post your attempt and where you're stuck, and we're happy to help out further.

there is no misunderstanding no need to post that much i know how it works.

i post my code wich is working but have a loop issue then you gave me some advice wich i try to apply at best .
then
your colleague gave another way to do it wich i tryed also but has it's not my code anymore i don't know how to use it that's why i ask an "example" related to my code.

I didn't ask to write the code for me but just an example to help me understand

And this kind of answer "Did you REALLY try that pseudo code verbatim?" does not provide help even if it look stupid to you