severin
severin

Reputation: 2126

Parsing yad Output Correctly in Bash

I want to use yad to show a list of items and then execute a shell command on every item. However, yad seems to produce a separator character that does not seem to be a simple whitespace. I need help parsing its output. If the user selectes all three items, this bash script

#!/bin/bash
items=`yad --list --separator='' --height=600 --multiple --column="Items" item1 item2 item3`

echo $items

IFS=' ' read -r -a ARRAY <<< $items
for item in "${ARRAY[@]}"; do
        echo "$item"
done

should output

item1 item2 item3
item1
item2
item3

Instead the script only outputs:

item1 item2 item3
item1

I am using this trick to parse yad's output into an array. It used to work well with yad's predecessor zenity, but it seems to fail with yad.

Echoing $items into a textfile and reading this textfile with cat works as expected: Replacing IFS=' ' read -r -a ADDR <<< $items in the above script with

echo "$items" > tmpfile
IFS=' ' read -r -a ADDR <<< `cat tmpfile`

yields the expected result.

What am I missing here?

Upvotes: 0

Views: 3543

Answers (1)

jedwards
jedwards

Reputation: 30210

I think you're making this more complicated than necessary.

Here is an alternate method that works.

items=`yad --list --separator='' --height=600 --multiple --column="Items" item1 item2 item3`

echo $items

for item in $items; do
        echo "$item"
done

This is the simplest and most straightforward. It splits over elements in the IFS (by default this includes the space, tab and newline character)

No need to create an array, use read or here strings.

Upvotes: 2

Related Questions