YAD, how to assign bash variables

interpretive language scripts


Moderator: Forum moderators

Post Reply
User avatar
wizard
Posts: 1586
Joined: Sun Aug 09, 2020 7:50 pm
Has thanked: 2127 times
Been thanked: 503 times

YAD, how to assign bash variables

Post by wizard »

YAD is a "dialog box" program that is more powerful than Xdialog and easier to use than gtkdialog. YAD creates a GUI interface for input/output with BASH programs. It gets input from users and then outputs it. Assigning the output to variables is a task for BASH.

I've seen multiple examples of how to do it (most look a little cryptic), the one I found easiest to understand and implement will be shown here.

Let's start with things we have to know about BASH and YAD.
BASH:
-any character or name starting with a $ is treated as a variable
Example:
--open a terminal
--type: test=5
--press: enter
--type: echo $test
--press: enter
The terminal should reply 5, which is the value we gave for "test"
-BASH uses the ? (question mark) as a special character that contains the exit status (return code) of the last command.

YAD:
-open a terminal
-type: yad
-press: enter

yad-basic.jpg
yad-basic.jpg (3.24 KiB) Viewed 531 times

This gives you a basic YAD dialog box. The user can take one of four actions.

  1. click OK, closes the window and outputs any information it contains
  2. click Cancel, close the window with no output (you can change this, see NOTE)
  3. click the "X" in the right corner, close the window with no output
  4. press ESC key, close the window with no output

Each action also outputs the return code that tells you what the user did.

These are the default return codes:
OK=0
Cancel=1
"X"=252
ESC=252

NOTE: you can create custom buttons that also use YAD's output by giving them an "even" return code

Here is a basic code example with two field boxes and two custom buttons. You can copy and paste it into a geany file, save and make it executable to test it if you like.

Code: Select all

#! /bin/bash
#Yad assign variables with BASH

yad --form --width=400 --title=" " --text="Please enter your info:" --separator=' '  \
--button=Skip:1 \
--button=Apply:0 \
--field="Username" \
--field="Password" \
yad-bash1.jpg
yad-bash1.jpg (7.4 KiB) Viewed 531 times

First, to capture the YAD output we assign the YAD command to a variable we'll call "values". So the code looks like this:

Code: Select all

values=( $(yad --form --width=400 --title=" " --text="Please enter your info:" --separator=' '  \
--button=Skip:1 \
--button=Apply:0 \
--field="Username" \
--field="Password" \ ))

Next, we want to assign the value of $? (the return code) to a variable we'll call "return", so we add this code:

Code: Select all

return=$?
echo $return #just shows us the value

Next, assign the YAD output to separate variables (they start with zero):

Code: Select all

username="${values[0]}"
password="${values[1]}"

Last, we add some logic based on the users action return code:

Code: Select all

if [ $return == 0 ]; then
    echo You entered:
    echo $username
    echo $password
    exit
fi

exit

Here is the final code:

Code: Select all

#! /bin/bash
#Yad assign variables with BASH

values=( $(yad --form --width=400 --title=" " --text="Please enter your info:" --separator=' '  \
--button=Skip:1 \
--button=Apply:0 \
--field="Username" \
--field="Password" \ ))

return=$?
echo $return #just shows us the value

username="${values[0]}"
password="${values[1]}"

if [ $return == 0 ]; then
    echo You entered:
    echo $username
    echo $password
    exit
fi

exit

That's it, hope it works for you. Take a look at the @smokey01 site for a comprehensive tutorial on YAD http://smokey01.com/yad/

wizard

Big pile of OLD computers

Post Reply

Return to “Scripts”