15
submitted 9 months ago* (last edited 9 months ago) by linuxPIPEpower@discuss.tchncs.de to c/linux@lemmy.ml

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:

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:

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:

yad --title "Create a file" --form --field="File name" --field="Content"

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:

touch "$filename"
echo "$filecontent" > $filename

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.

top 8 comments
sorted by: hot top controversial new old
[-] Flyswat@lemmy.dbzer0.com 3 points 9 months ago

If you fill both fields the output in the terminal is file|this is some text|

Wouldn't it be easy to get them using awk by defining | as a field separator?

[-] linuxPIPEpower@discuss.tchncs.de 1 points 9 months ago

It is the only solution I found. I described it in the post but put it behind a "spoiler" "What doesn't work" to make the post shorter.

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.

[-] bizdelnick@lemmy.ml 2 points 9 months ago* (last edited 9 months ago)

Try something like IFS='|' read f1 f2 < <(zenity <...>) where f1, f2 etc. are variable names.

[-] linuxPIPEpower@discuss.tchncs.de 1 points 9 months ago

If you leave some of the field blank will it be able to skip assigning the respective variable? That's one problem with the positional values.

[-] somethingsomethingidk@lemmy.world 2 points 9 months ago

I have an array based solution but it doesn't solve the cases of changing the order or empty fields.

[-] yianiris@kafeneio.social 1 points 9 months ago

grep sda /etc/mtab 2>&1 tee /tmp/tab.txt | yad --title="output" \ --width=154 --text="$(cat /tmp/tab.txt)"

@linuxPIPEpower

[-] linuxPIPEpower@discuss.tchncs.de 1 points 9 months ago

I'm not sure if that is working properly on my system. It opens a dialogue box that just has content "" with cancel/ok buttons .

I tried populating a file tab.txt with a few lines because I am not sure if my results from the first part are what's expected, which is 1 line. No matter what the content the best I can do is get the first line to show in the dialogue but not in an interactive way.

Tbh having a bit of a hard time following what's going on with 2>&1 tee. But I am not sure how it could be the right thing as I don't see more than input?

What I want is to open a dialogue like this:

yad --title "Create a file" --form --field="File name" --field="Content

where the user's input gets directed to some sort of structure. Like an argument As though you had a terminal script with the syntax scriptname --filename="file.txt" --content="red green blue".

[-] yianiris@kafeneio.social 1 points 9 months ago

Did I miss what you are asking or is this it?

cat /etc/mtab 2>&1 | tee /tmp/tab.txt | yad --title="output" --width=154 \
--text="$(cat /tmp/tab.txt)

@linuxPIPEpower

this post was submitted on 28 Jan 2024
15 points (94.1% liked)

Linux

48080 readers
780 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS