LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-02-2017, 09:45 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
how to get zenity code if user close zenity window


hi everyone , i have an issue understanding in how will i get an exit code for zenity .

zenity replies :
0 = ok , yes
1 = false,cancel
5 = timeout

but i need the zenity code if user just closed zenity window and did not pressed any button .

a piece of code to make logic :

default age value is 20 .

-If user write any value in input box , then echo will write that value on age.tmp file .

-If user press OK without writing any input value , then echo will write default "20" on age.tmp file .

-If user press Cancel then echo will not write default "20" on age.tmp file .

But if user just close zenity window then nothing will be written on age.tmp file .

Quote:
#!/bin/bash
rm -f age.tmp
ask=$(zenity --entry --title="age" --width=100 --height=100 --text="write your age" --entry-text="20");
if [ $? = "5" ]; then
echo "20" > age.tmp
fi
if [ $? = "0" ]; then
echo "$ask" > age.tmp
fi
if [ $? = "1" ]; then
echo "20" > age.tmp
fi
cat age.tmp
What i need is to get zenity exit code if user just close the window and ignored the buttons .


Note : according to zenity manual , 1 means "cancel , no , or close window"

https://help.gnome.org/users/zenity/.../usage.html.en
Could this be a bug ?

Last edited by pedropt; 03-02-2017 at 11:15 AM.
 
Old 03-02-2017, 11:39 AM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Your return code is being destroyed by the first "if" construct. From the bash manpage:
if list; then list; [ elif list; then list; ] ... [ else list; ] fi
The if list is executed. If its exit status is zero, the then list is executed. Otherwise, each elif list is executed in turn, and if its exit status is zero, the corresponding then list is executed and the command completes. Otherwise, the else list is executed, if present. The exit status is the exit status of the last command executed, or zero if no condition tested true.
You have to save the return code if you want to test it more than once:
Code:
ask=$(zenity --entry --title="age" --width=100 --height=100 --text="write your age" --entry-text="20")
Rc=$?
if [ $Rc = "5" ]; then
    echo "20" > age.tmp
fi
if [ $Rc = "0" ]; then
    echo "$ask" > age.tmp
fi
if [ $Rc = "1" ]; then
    echo "20" > age.tmp
fi
 
2 members found this post helpful.
Old 03-02-2017, 11:51 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Sorry. Caffeine shortage.

Last edited by Habitual; 03-02-2017 at 11:52 AM.
 
Old 03-02-2017, 12:03 PM   #4
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
nice one , thanks for the help .
 
Old 03-03-2017, 12:13 PM   #5
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
In all cases I would save the resultcode; Either from the PIPESTATUS array or from $?

The bunch of if statements could be replaced by a switch:
Code:
ask=$(zenity --entry --title="age" --width=100 --height=100 --text="write your age" --entry-text="20")
Rc=$?

case $Rc in
   "5") echo "20" > age.tmp ;;
   "0") echo "$ask" > age.tmp ;;
   "1") echo "20" > age.tmp ;;
esac
or use if then elif then fi construction like so:
Code:
ask=$(zenity --entry --title="age" --width=100 --height=100 --text="write your age" --entry-text="20")
Rc=$?

if [[ $Rc = "5" ]]
then
    echo "20" > age.tmp
elif [[ $Rc = "0" ]]
then
    echo "$ask" > age.tmp
elif [[ $Rc = "1" ]]
then
    echo "20" > age.tmp
fi
Both are more logical: you're testing against a fixed value towards the same variable that cannot have changed in between the comparisons; So, having tested one as true, the other cases do not have to be computed, since they cannot be true.

And always, have a care with special (volatile) variables; $? and PIPESTATUS change directly after each command.

For good measure, as I mentioned PIPESTATUS, here's an example:

Code:
somefunction | tee -a logfile.log
RETVAL=${PIPESTATUS[0]}
Pipestatus is (particulary) handy when you want to know the return value of one (or more) commands that have been executed in a pipe; It is an array, and if you want to keep all values, you have to store them all in an array:
Code:
true | false | echo done | grep nothing | true | false | true | false
RETVAL=${PIPESTATUS[*]}

echo ${RETVAL[*]}
 
  


Reply

Tags
zenity



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Zenity script help zvlo Linux - Newbie 7 11-28-2016 10:52 AM
[SOLVED] Zenity - force one dialog / window over another stan79 Linux - General 8 08-10-2013 04:05 PM
[SOLVED] Zenity kumawat10 Linux - Newbie 8 12-16-2011 10:56 PM
GUI Dialog Box w/embedded terminal window. eg. Zenity? ... Sean Moran Linux - Software 2 06-23-2011 03:16 AM
LXer: Make Your Scripts User Friendly with Zenity LXer Syndicated Linux News 0 05-12-2008 01:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration