1

I have a code

menu "Pasirinkite:" 0 0 0 \
iraso_ivedimas "iraso irasymas i kontaktu knygele" \
iraso_pdialog --title "Meniu" \
--menu "Pasirinkite:" 0 0 0 \
iraso_ivedimas "iraso irasymas i kontaktu knygele" \
iraso_paieska "iraso paieska pagal varda" \
iraso_perziura "viso iraso perziura" \
iraso_salinimas "iraso salinimas pagal eiles numeri" 2>f.txt

kint=$?
case $kint in
0)p=`cat f.txt`
        case $p in
        iraso_ivedimas)dialog --inputbox "Iveskite varda" 0 0 2>kvardas.txt
        vardp=`cat kvardas.txt`
        dialog --inputbox "Iveskite pavarde" 0 0 2>kpavarde.txt
        pavardp=`cat kpavarde.txt`
        dialog --inputbox "Iveskite telefono numeri" 0 0 2>ktel.txt
        telk=`cat ktel.txt`
    dialog --inputbox "Iveskite el. pasta" 0 0 2>kpastas.txt
        pastk=`cat kpastas.txt`
        echo "$vardp $pavardp $telk $pastk" >> kontaktinf.txt
        ;;
        iraso_paieska)dialog --inputbox "iveskite varda" 0 0 2>ieskvard.txt
        survard=`cat ieskvard.txt`
        if grep $survard kontaktinf.txt
        then
        grep $survard kontaktinf.txt >> svard.txt
        ttss=`cat svard.txt`
    dialog --msgbox "$ttss" 0 0
        else
        dialog --msgbox "nesurasta" 0 0
        fi
        ;;
        iraso_perziura)
        n=`wc -l< kontaktinf.txt`
        pps=`cat kontaktinf.txt`
        if [ -s kontaktinf.txt ]
        then
    dialog --inputbox "iveskite eiles numeri" 0 0 2>eilnr.txt
        eilesnr=`cat eilnr.txt`
        sed -n "$eilesnr"'p' kontaktinf.txt > pta.txt
        pts=`cat pta.txt`
        dialog --yesno "ar tikrai norite salinti $pts" 0 0
        kinn=$?
        case $kinn in
        0)sed -i "$eilesnr"'d' kontaktinf.txt;;
        1)exit;;
        255)exit;;
        esac
    else
        echo "sarasas tuscias"
        fi
esac;;
1)exit;;
255)exit;;
esac

rm -f kvardas.txt
rm -f kpavarde.txt
rm -f ktel.txt
rm -f kpastas.txt
rm -f eilnr.txt
rm -f svard.txt
rm -f pta.txt

there are 4 menu points. iraso_ivedimas) iraso_paieska) iraso_perziura) iraso_salinimas). My question is, how do I give them 1)exit;; (cancel button) 255)exit;; (esc), to each of them because when I try to do it, it just goes to every menu point till end if i press esc or cancel

  • Have a look at [How do I prompt for Yes/No/Cancel](https://stackoverflow.com/a/27875395/1765658)! I've posted a full demo of using dialogs, whiptail and others! – F. Hauri - Give Up GitHub Jan 27 '20 at 07:12
  • Dialog result if alway `0` when user answer correctly! So you just have to test for non-zero result code. `dialog --func .... 2>&1 >/dev/tty || exit 1` – F. Hauri - Give Up GitHub Jan 27 '20 at 07:27
  • You really want to avoid this chaotic use of temporary files. Try http://shellcheck.net/ to get many common errors diagnosed, often with suggested fixes. – tripleee Jan 27 '20 at 07:56
  • @tripleee shellcheck recommand to replace `var=\`cat file\`` by `var=$(cat file)` where `var=$(< file)` could be best. And at all `var=$(dialog..)` will be shorter and simplier than `dialog >file;var=$( – F. Hauri - Give Up GitHub Jan 27 '20 at 08:08

2 Answers2

1

Please take a look at this example of dialog's from my project sshto

#------------------------{ First dialog - Select host }--------------------------------------------
first_dialog () {
    target=$(dialog --extra-button --extra-label "RUN COMMAND"         \
                    --ok-label "CONNECT" --cancel-label "EXIT" --colors \
                    --help-button --help-label  "CONTENTS" --output-fd 1 \
                    --menu "Select host to connect to. $USERNOTE" 0 0 0 "${list[@]}")
    case $target:$? in
       -*\ *\ *-:*) first_dialog ;;
               *:0) go_to_target  ; first_dialog;;
               *:2) contents_menu;;
               *:3) second_dialog;;
               *:*) bye;;
    esac
}

#------------------------{ Second dialog - Select command }----------------------------------------
second_dialog () {
    cmdlist_renew
    command=$(dialog --ok-label "RUN" --cancel-label "BACK" --output-fd 1 \
                     --extra-button    --extra-label "CONNECT" --colors    \
                     --menu "Select command to run on host \Z4$target\Z0. $USERNOTE" 0 0 0 "${cmdlist[@]}")
    case $command:$? in
           Sshkey:0) add_sshkey  ;;
            Alias:0) add_aliases ;;
             Info:0) system_info ;;
             Copy:0) copy_files  ;;
         Username:0) username    ;;
           Upload:0) upload      ;;
             Dest:0) downpath    ;;
         Download:0) dlst; down  ;;
            Local:0) local_port  ;;
           Remote:0) remote_port ;;
           Tunnel:0) portunneling;;
                *:3) go_to_target;;
                *:0) run_command ;;
                *:*) first_dialog;;
    esac;            second_dialog
}

Dialogs wrapped in functions(first_dialog, second_dialog) and starts one after other and vice versa.

Ivan
  • 6,188
  • 1
  • 16
  • 23
1

Use non-zero exit code of dialog as user abort.

Little sample

#!/bin/bash

die() {
    echo >&2 "$0 ERROR: $@"
    exit 1
}

userExit() {
    die "User abort."
}

vardp=$(
    dialog --inputbox "Iveskite varda" 0 0 2>&1 >/dev/tty
    ) || userExit
pavardp=$(dialog --inputbox "Iveskite pavarde" 0 0 2>&1 >/dev/tty) || userExit
telk=$(dialog --inputbox "Iveskite telefono numeri" 0 0 2>&1 >/dev/tty) || userExit
pastk=$(dialog --inputbox "Iveskite el. pasta" 0 0 2>&1 >/dev/tty) || userExit

#  And so on...

printf "%-8s:%s\n" vardp "$vardp" pavardp "$pavardp" telk "$telk" pastk "$pastk" 
F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137