Use YAD/Zenity dialogues to populated variables in a bash script?
I am learning some bash scripting.
I am interested to learn about getting input for my scripts via a GUI interface. It seems that yad
(forked from zenity
) is the most robust tool for this. (But if there is a better choice I would like to hear about it too.)
Is it possible to obtain 2 or more named variables using yad
? Not just getting the values based on their positions ($1
, $2
, etc), with awk
. See “What doesn’t work” spoiler for those.
What doesn't workI find how to obtain one named variable, for example: sh inputStr=$(zenity --entry --title=“My Title” --text=“My Text:”)
I also find solutions relying on opening single-variable dialogues sequentially but that’s a terrible interface. Everything else relies on chopping up the output with awk
or based on the positions, $1
, $2
, $3
etc. In this script $jpgfile
is obtained: sh jpgfile=$(echo $OUTPUT | awk ‘BEGIN {FS=“,” } { print $1 }’)
This seems unmanageable because adding a new field or failing to provide input for a field will both change the output order of every subsequent value. It’s way too fragile.
For a simple example, I want to ask the user for a file name and some content. Creating the dialogue is like this:
<span style="color:#323232;">yad --title </span><span style="color:#183691;">"Create a file"</span><span style="color:#323232;"> --form --field</span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#183691;">"File name"</span><span style="color:#323232;"> --field</span><span style="font-weight:bold;color:#a71d5d;">=</span><span style="color:#183691;">"Content"
</span>
If you fill both fields the output in the terminal is file|this is some text|
. How do I get them into variables like $filename
and $filecontent
? So then I can finish the script like this:
<span style="color:#323232;">touch </span><span style="color:#183691;">"$</span><span style="color:#323232;">filename</span><span style="color:#183691;">"
</span><span style="color:#62a35c;">echo </span><span style="color:#183691;">"$</span><span style="color:#323232;">filecontent</span><span style="color:#183691;">" </span><span style="font-weight:bold;color:#a71d5d;">> </span><span style="color:#323232;">$filename
</span>
Is this possible??? I do not find it anywhere. I looked though all kinds of websites like YAD Guide, yad man page, smokey01. Maybe I missed something. On yaddemo I read about bash arrays and it seemed to come close but I couldn’t quite piece it together.
Add comment