2

so i am writing a small bash script to automate a few tasks...and i have stumbled on to a issue here.. my problem is

  val=$ ( yad --center --width=300 --height=100 --title "Alert" --image "dialog-question" --buttons-layout=center --text "Search for Broadcast Stations ?" \ --button=gtk-yes:0 --button=gtk-no:1 )   

if [[ $val == 0 ]]; then

The Above piece of code is not working out ... what i am trying to do here is save the exit code of the YAD window to the val variable then use it in the if then statement.... what mistake am i doing here ???? i know the exit status check is $? but i am totally lost on how to actually implement it though ..!!

  • i understand that the YAD command is outputting a exit status numerical value and i am trying to assign that numerical value to the "val"variable and its not working !!! – Mahesh Chandra May 30 '17 at 00:33
  • `$val` variable is getting the standard output, while the return status of `yad` is going to the `$?` special variable. – Fábio Roberto Teodoro Oct 23 '19 at 21:18

2 Answers2

3

This is a full working example based on YAD Wiki examples:

val=$(yad --center --width=300 --height=100 --title "Alert" --image "dialog-question" --buttons-layout=center --text "Search for Broadcast
Stations ?" --button=gtk-yes:0 --button=gtk-no:1 )   
ret=$?

[[ $ret -eq 1 ]] && echo "No clicked" && exit 0

if [[ $ret -eq 0 ]]; then
    echo "Yes clicked"
    exit 0
fi
syntagma
  • 23,346
  • 16
  • 78
  • 134
0

The last answer works fine, expanded.. button 2 and 1 are used for yes and no to keep things random here. 252 is exit code. the long Yad string is provided one line to keep any word wrap issues away. Simple Yad yes/no dialog box.

question=$(yad --center --width=300 --height=100 --title "Question" --image "dialog-question" --buttons-layout=center --text "Search for Broadcast Stations?" --button=gtk-yes:2 --button=gtk-no:1 )
answer=$?
[[ $answer -eq 252 ]] && echo "Window Closed" && exit 1
[[ $answer -eq 0 ]] && echo "Unused"
[[ $answer -eq 1 ]] && echo "No clicked" && exit 0
if [[ $answer -eq 2 ]]; then
    echo "Yes clicked"
fi
exit 0
chrslg
  • 9,023
  • 5
  • 17
  • 31
  • I am curious: why you, and the other answerer, keep the result (not the exit code. That you got in `answer`. But the output. What would have been printed if not called inside a `$(...)`) of the command, since nothing is done with it afterward. Is it because the OP did? But the OP did it because they, incorrectly, thought that, that way, `$val` would hold exit code. – chrslg Nov 05 '22 at 16:26
  • so first I just kept the original as inline as possible so it isn't diverging to hard. also I am not any expert so dont hold my value high. the unset possibility is also there, I have personally found if/then trapping method problematic overall with Yad and prefer case handling of these matters if important to trap it, for one if any error handling is around this method shown gets more difficult .. anyway.. hope that added some interesting notes? – spudgunman Nov 06 '22 at 17:24