Search code examples
bashyad

How to get values in YAD when combining form and button // Exit codes for user-specified buttons


#!/bin/bash

array=$(yad \
--separator="\n" \
--form \
--field="Number":NUM 1 \
--field="Text":TEXT \
--button="b1:1" \
--button="b2:2" \
--button="b3:3" )
echo $?
echo "${array[@]}"

When pressing b1 or b3, the array is empty. Why? How to modify this to get always the answer of NUM- and TEXT-form-field in the array and the button number as $? ?


Solution

  • From the manpage:

    Exit codes for user-specified buttons must be specified in command line.
    Even exit code mean to print result, odd just return exit code.

    So, you need to use even exit codes for your buttons


    edit:

    I was looking for a way to robustly load yad's output into a bash array, but the --separator option doesn't seem to support the null byte. There is however a --quoted-output for shells that you can use with eval:

    quoted_input=$(
        yad --quoted-output \
            --separator=' ' \
            --form \
            --field='Number':NUM 666 \
            --field='Text':TEXT 'default text' \
            --button='b1':10 \
            --button='b2':20 \
            --button='b3':30
        echo $?
    )
    eval "array=( $quoted_input )"
    
    # show the array (which stores the results)
    declare -p array
    

    No character in the [0x01-0x7f] range can break it, so it is safe.