PDA

View Full Version : [SOLVED] Bash/Zenity



Dale Sexton
January 1st, 2009, 11:13 PM
I'm using this as a test, but the answer is always 'Yes 127'. I thought $? was supposed to equal 0, 1 or -1.

I've tried several different ways to do the if statement, but $? always equals 127. Where am I wrong?



#!/bin/bash

zenity --question
if [[ $? == 0 ]] ; then
zenity --info --text="Yes $?";
else
zenity --info --text="No $?";
fi

kaibob
January 2nd, 2009, 02:03 AM
The script works fine on my computer. Something else is involved.

Gwasanaethau
January 2nd, 2009, 02:09 AM
I think the '$?' in the '--text="Yes $?"' is catching the exit status of the 'if […]' block rather than 'zenity' command prior to it. I could be wrong…does this give you a different result?

#!/bin/bash

zenity --question
ZENEXIT=$?
if [[ $ZENEXIT == 0 ]] ; then
zenity --info --text="Yes $ZENEXIT";
else
zenity --info --text="No $ZENEXIT";
fi

Dale Sexton
January 2nd, 2009, 03:28 AM
Thanks,
that's what did it.

Dale Sexton
January 2nd, 2009, 03:39 AM
Hmmm... on closer examination, when I click OK, I get 'No 0' and when I click Cancel, I get 'No 1'.

Gwasanaethau
January 2nd, 2009, 04:46 AM
Hmm, intriguing…

It works OK here - did you do any cutting-and-pasting, out of interest? If so, you might have forgotten to change a 'No' to a 'Yes'. Other than that I have no idea why that's happening…sorry!

Dale Sexton
January 2nd, 2009, 04:57 AM
I've cut and pasted exactly as you have it and also have several variations. They all do the same. I've copied it a couple of times to make sure I got everything. ???

kaibob
January 2nd, 2009, 05:19 AM
I'm new to shell scripts, but I don't believe either of those lines of your script should return an exit status 127, which is explained as follows:

Exit Code Number: 127
Meaning: "command not found"
Comments: possible problem with $PATH or a typo

http://www.museum.state.il.us/ismdepts/library/linuxguides/abs-guide/exitcodes.html


If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126.

http://theory.uwinnipeg.ca/localfiles/infofiles/bash/bashref_43.html

Perhaps one of the more knowledgeable members of this forum will have an answer.

Dale Sexton
January 2nd, 2009, 05:25 AM
I'm really starting to think I've got a fragged computer.

Dale Sexton
January 2nd, 2009, 02:10 PM
After alot of play, here's what worked:



#!/bin/bash

zenity --question
# zenity --info --text="$?"
tTest=$?
if [ "$tTest" -eq "0" ] ; then
zenity --info --text="Yes $tTest";
else
zenity --info --text="No $tTest";
fi


(I still think my computer is fragged if the other codes worked on other computers.)

Dale Sexton
January 2nd, 2009, 02:31 PM
127 was the code if $? was put directly into the if statement. Once I used a variable other than $? it gave me the correct code. Not sure why I had to encase the variable in quotations or why I had to use -eq instead of ==.

Dale Sexton
January 3rd, 2009, 06:54 PM
I'm wondering if it might be my version of Ubuntu? I'm using 8.04. You guys?

kaibob
January 4th, 2009, 01:13 AM
I'm wondering if it might be my version of Ubuntu? I'm using 8.04. You guys?

I'm also running Ubuntu 8.04 and your original script still works fine (see thumbnails).

Dale Sexton
January 5th, 2009, 03:26 AM
Seems I may be limited in programming with this computer.:(