[bash] Zenity loop (raspbian)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash] Zenity loop (raspbian)
# 1  
Old 10-04-2016
[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 :/
Code:
#!/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

# 2  
Old 10-04-2016
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, breaking 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.

Last edited by RudiC; 10-04-2016 at 11:00 AM..
# 3  
Old 10-04-2016
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

Code:
#!/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

# 4  
Old 10-04-2016
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.
# 5  
Old 10-04-2016
okay so should look like this?

Code:
#!/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"
# 6  
Old 10-04-2016
NO. Just one single outer case ... esac.
Just to SEE the logics, try a top-down approach:

Code:
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.
# 7  
Old 10-04-2016
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.

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Menu using zenity

Hi, I'm new to bash, and have an example menu script using Zenity. It works fine if the user enters A B or C, but if the user enters nothing, I can only figure out how to exit the script. How do I get the menu to reappear if no input is selected? The script is: title="Select example"... (2 Replies)
Discussion started by: allen11
2 Replies

2. Shell Programming and Scripting

Help on for loop in bash

Hi, In the code "for loop" has been used to search for files (command line arguments) in directories and then produce the result to the standard output. However, I want when no files are named on the command line, it should read a list of files from standard input and it should use the command... (7 Replies)
Discussion started by: Ra26k
7 Replies

3. Shell Programming and Scripting

PWM script for raspbian

Hello Community, I'm a little new to shell scripting and i was trying to write a servo control script for my Raspberry PI, using raspbian. here is the code. #!/bin/sh #Signal time signal_time_gpio27=0.0015s #Delay time signal_delay_gpio27=0.02s if grep -lq 0... (1 Reply)
Discussion started by: drux
1 Replies

4. Shell Programming and Scripting

Bash for loop with two variables

Hi, I have the following folder structure: TST500-1000 TST500-2000 TST500-3000 TST700-1000 TST700-2000 TST700-3000 TST900-1000 TST900-2000 TST900-3000 I would like to store the numbers in the first column (considering "-" as column separator) into a variable then the numbers in... (3 Replies)
Discussion started by: alex2005
3 Replies

5. Shell Programming and Scripting

Bash For Loop Help

This is a short program I wrote to search through a directory and move files containing the keyword to a folder. #!/bin/bash echo 'What is the directory?' read DIR echo -e '\n' echo 'What is the keyword?' read KEY echo -e '\n' cd $DIR rmdir 'relevant_files' mkdir 'relevant_files'... (5 Replies)
Discussion started by: zenyoul
5 Replies

6. Shell Programming and Scripting

If loop in bash

Hello, I have a script that runs a series of commands. Halfway through the script, I want it to check whether everything is going alright: if it is, to proceed with the script, if it isn't to repeat the last step until it gets it right. My code so far looks like this, simplified a bit: ... (3 Replies)
Discussion started by: Leo_Boon
3 Replies

7. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

8. Shell Programming and Scripting

Bash: Help with output from a for loop

Hi, Sorry I'm new to shell scripting.. my loop is as follows: let i=0 for item in ${APPSARRAY} do #..some code to get a unique value called $result let i=i+1 done What I want to do is within the for loop, create a comma seperated list: ... (3 Replies)
Discussion started by: mjwoodford
3 Replies

9. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies
Login or Register to Ask a Question