0

so a pretty straight forward question , on zenity theres is 2 buttons on each page , ok and cancel . with my script i manage to assign the zenity code to a variable where i can pick a date for my calendar. I would like to use the cancel button for my agenda , so i renamed it but i don't know how to make it work! thanks

#!/bin/bash
calendarinput=$(zenity --calendar \
--title "Scheduler" \
--text "Pick a date" \
--ok-label "Done" --cancel-label "Agenda" \
--date-format "%A %d/%m/%y")

agenda+="$calendarinput"
unset calendarinput
calendarinput="Done"

if [ "$calendarinput"="Done" ];then
    remind=$(zenity --entry)
    agenda+="$remind\n"
fi

zenity --info \
--text "$agenda"

This is a only 1 function of my script.There is another list menu before this one.

2 Answers 2

1

You can find the return code from the buttons in the special parameter $?, which holds the exit code from the last executed command. It can either be 0, 1, or 5, depending on whether the user pressed OK, Cancel, or timeout.

#!/bin/bash

calendarinput=$(zenity --calendar \
--title "Scheduler" \
--text "Pick a date" \
--ok-label "Done" --cancel-label "Agenda" \
--date-format "%A %d/%m/%y")

ret=$?

if ((ret==0)); then
    echo "Done"
else
    echo "Agenda"
fi

The expressions in an if-statement need to be separated with space:

if [ "$calendarinput" = "Done" ]; then

(It will also always evaluate as true, which makes it somewhat unnecessary).

1
  • Thank you very much . I have done everything i wanted with this calendar but not sometimes i have pending cancel button (--no-cancel dont work) , i would like to know if there is a way when the user press cancel button , it just goes out of my function or go back to the last zenity box . thanks Jan 24, 2021 at 12:10
1

set up using case esac instead of if fi

zenity --whatever
case $? in
    0) whatever.script #this is pressing ok
    ;;
    1) $1 # this is pressing cancel
    ;;
    *) $1 #this is closing window etc $1 will kill process
    ;;
esac
1
  • This is neat, but I didn't get the $1 part. How does $1 kill the process?
    – Hippo
    Jun 28, 2022 at 7:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .