how to use yad in bash scripting?

6.7k views Asked by At

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

2

There are 2 answers

0
syntagma On

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
2
spudgunman On

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