2
#!/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 $? ?

doxi
  • 23
  • 3
  • I don't know anything about `yad`, but that `array` variable is *not* an array, it's just a plain text variable. To make an array from the command's output, you'd need some sort of parsing step to split it into separate elements. Also, `$?` is normally a success/failure status code (0 for success, nonzero for some sort of failure). – Gordon Davisson Dec 13 '21 at 20:22

1 Answers1

2

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.

Fravadona
  • 13,917
  • 1
  • 23
  • 35