Page 1 of 1
Bash, found a code trap
Posted: Wed Nov 29, 2023 7:09 pm
by wizard
Recently while coding a small script with YAD I found a "trap" (anyway for me). I wanted to see the return codes of the buttons that were clicked, so inserted an "echo $?" right after the YAD code. That was a mistake, the echo will show the return code, but at the same time it resets it to zero. The result is none of your "case" or "if" code will work correctly. The code below is an example.
Hope this might keep someone else from wasting time (took me almost a day to figure it out) on the same thing.
Thanks
wizard
Code: Select all
#! /bin/bash
values=( $(yad --form --width=400 --title=" " --text="Please enter your info:" --separator=' ' \
--button=Skip:1 \
--button=Apply:0 \
--field="Username" \
--field="Password" \ ))
echo $? #do not do this, it resets $? to zero
ret=$? #this is ok
echo $ret #this is ok
Re: YAD, found a code trap
Posted: Wed Nov 29, 2023 7:35 pm
by fredx181
For me when I try your code when clicking Skip it returns exit code 1 (no zero, as you say) which is as should be (programmed --button=Skip:1)
The second, echo $ret gives 0 which is right because the previous command succeeded . (edit: perhaps there's confusion for you that the second echo "resets", which is not the case IMO)
EDIT: Better add the ret=$? directly after the yad code e.g. :
Code: Select all
#! /bin/bash
values=( $(yad --form --width=400 --title=" " --text="Please enter your info:" --separator=' ' \
--button=Skip:1 \
--button=Apply:0 \
--field="Username" \
--field="Password" \ ))
ret=$?
echo $ret #this is ok
echo $? #this will give exit code of previous (echo) command
echo $ret #this is still ok as variable $ret is set earlier
Re: YAD, found a code trap
Posted: Wed Nov 29, 2023 8:09 pm
by wizard
@fredx181
I'm sure I don't understand . The result I got was when clicking skip:
echo $? #this returned 1
so ok the code was 1
ret=$? #did this which I thought would result in $ret = 1
echo $ret #this returned 0 (zero)
The fix for me was to eliminate the first echo
If you click skip and have this code:
echo $? #this will return 1
echo $? #this will return 0, why does the value of $? change
Thanks
wizard
Re: YAD, found a code trap
Posted: Wed Nov 29, 2023 8:16 pm
by fredx181
wizard wrote: ↑Wed Nov 29, 2023 8:09 pm
echo $? #this will return 1
echo $? #this will return 0, why does the value of $? change
The second will return 0 because the previous command (echo $?) succeeded without error (=0) so let's say, the $? is refreshed to "actual" , updated.
Re: YAD, found a code trap
Posted: Wed Nov 29, 2023 8:49 pm
by wizard
@fredx181
Ok, so did a little reading on "$?" and it is "the exit status of the last command", aha. So in this case clicking the skip button resulted in $?=1
echo $? resulted in changing $? to 0 (the exit status of the last command)
So the caution in coding is don't echo $? before assigning $? to a variable. Such as:
echo $?
ret=$? #ret will NOT have the value of the YAD button
Should be done like this:
ret=$? #ret will have the value of the YAD button
echo $ret # will echo the value of the YAD button
Thanks
wizard