0

I try to make a script bash app with the help of Zenity.

https://help.gnome.org/users/zenity/stable/index.html.en

It works properly, however I encounter a problem ...

Here is my code:

#!/bin/bash

APP_NAME="bealeCrypter"

function select_letter () {
    letter=("FALSE" "1" "Treasury location" "none"
            "FALSE" "2" "Treasury contents" "yes"
            "FALSE" "3" "Treasury beneficiaries" "none")

    LETTER_FILES=$(zenity --list \
        --width=430 \
        --height=235 \
        --title="$APP_NAME" \
        --text="What letter do you want to $1 ?" \
        --checklist \
        --multiple \
        --column=Selection \
        --column=Letter \
        --column=Description \
        --column=Decrypted \
        "${letter[@]}")

    if [ $? -eq 0 ]; then
        if [ -z "$LETTER_FILES" ]; then
            select_letter "$1"
        fi
    else
        app_main
    fi
}

function select_letter_options () {
    letter_options=("TRUE" "1" "Get the number of characters"
                    "TRUE" "2" "Get the smallest number"
                    "TRUE" "3" "Get the biggest number")

    LETTER_OPTIONS=$(zenity --list \
        --width=350 \
        --height=210 \
        --hide-header \
        --title="$APP_NAME" \
        --text="Choose the analyze letter options :" \
        --checklist \
        --multiple \
        --column=Selection \
        --column=Id \
        --column=Description \
        --hide-column=2 \
        "${letter_options[@]}")

    if [ $? -eq 0 ]; then
        if [ -z "$LETTER_OPTIONS" ]; then
            select_letter_options
        fi
    else
        app_main
    fi
}

function app_main () {
    menu=("1" "Analyze a Letter")

    SELECTION=$(zenity --list \
        --width=350 \
        --height=210 \
        --hide-header \
        --title="$APP_NAME" \
        --text="What to do ?" \
        --column=Id \
        --column=Selection \
        --hide-column=1 \
        --cancel-label=Quit \
        "${menu[@]}")

    if [ $? -eq 0 ]; then
        case "$SELECTION" in
            1) select_letter "analyze"
               select_letter_options
               ;;
            *) app_main
        esac

        echo -e "LETTER: $LETTER_FILES\nKEY: $KEY_FILES\nLETTER_OPTIONS: $LETTER_OPTIONS\nKEY_OPTIONS: $KEY_OPTIONS"
    else
        exit 0
    fi
}

app_main

Here is my problem, When I select the following menus/button (in this specific order):

  1. Analyze a Letter
  2. Cancel
  3. Analyze a Letter
  4. Select checkbox 1, 2 or 3 and OK
  5. Select options and OK

the option window opens twice after the validation OK, I don't understand why?

Here is a video demo:

https://streamable.com/np7wzl

I specify that if I do not make a Cancel (point #2) it works correctly.

How is it possible?

I tried to empty the variables before recalling the function but the result and always identical, I do not understand?

Does anyone have an explanation for this?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Momonosoke
  • 145
  • 1
  • 9
  • Tangentially [Why is testing “$?” to see if a command succeeded or not, an anti-pattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – tripleee Jul 24 '22 at 10:14
  • `*) app_main` Just write the loop. `else app_main` that recursive calling is confusing. Just return and let app_main handle where to go next. – KamilCuk Jul 24 '22 at 11:25
  • Thank you for your answers, actually the recursive function is rather confused, for more precise `Else app_main` is the action during the closing button from the window, I need to specify it because I don't want him to leave the program. I thought there was a strange behavior but in fact no, if I continue my program with the next function, everything works and the option window is no longer called twice .. I panicked for nothing ^^ On the other hand why this behavior only arrives during a closed action (otherwise everything works) I do not have the answer. – Momonosoke Jul 24 '22 at 12:20

0 Answers0