Bash, found a code trap

interpretive language scripts


Moderator: Forum moderators

Post Reply
User avatar
wizard
Posts: 1984
Joined: Sun Aug 09, 2020 7:50 pm
Has thanked: 2648 times
Been thanked: 692 times

Bash, found a code trap

Post 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
Last edited by wizard on Thu Nov 30, 2023 4:25 am, edited 1 time in total.

Big pile of OLD computers

User avatar
fredx181
Posts: 3071
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 374 times
Been thanked: 1309 times
Contact:

Re: YAD, found a code trap

Post 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
User avatar
wizard
Posts: 1984
Joined: Sun Aug 09, 2020 7:50 pm
Has thanked: 2648 times
Been thanked: 692 times

Re: YAD, found a code trap

Post by wizard »

@fredx181

I'm sure I don't understand :lol: . 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

Big pile of OLD computers

User avatar
fredx181
Posts: 3071
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 374 times
Been thanked: 1309 times
Contact:

Re: YAD, found a code trap

Post 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.

User avatar
wizard
Posts: 1984
Joined: Sun Aug 09, 2020 7:50 pm
Has thanked: 2648 times
Been thanked: 692 times

Re: YAD, found a code trap

Post 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

Big pile of OLD computers

Post Reply

Return to “Scripts”