0

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?

Community
  • 1
  • 1
severin
  • 2,106
  • 1
  • 17
  • 25
  • 1
    How about saving in array as `items=($(yad --list --separator='' --height=600 --multiple --column="Items" item1 item2 item3))`, then `for item in "${items[@]}"; do echo "$item" done` – another.anon.coward Jul 07 '12 at 16:26
  • Thanks! This works just fine! But do you also have an idea why the original code doesn't work? Admittedly, I am a bash beginner but I'd still like to understand what the problem is. – severin Jul 07 '12 at 16:55
  • You should try to avoid changing the IFS, or if you do, change it back as soon as you can. Otherwise it can cause some weird unexpected side effects. Changing it to the space is not as risky as what is usually done (changing it to newline), but it still good practice to avoid it or store it and change it back. – jedwards Jul 07 '12 at 16:58
  • @jedwards If I understand correctly, only the subsequent `read` would be affected by the change. See the comments of @JohannesSchaub-litb's answer in [this thread](http://stackoverflow.com/questions/918886/split-string-based-on-delimiter-in-bash). I might be wrong though!? – severin Jul 07 '12 at 17:11
  • @severin, in this case, thats correct because you set it like you did. But there are usually better ways to do what you want than change the IFS. In your case, the space character is already in the standard IFS, so it doesn't look like you even need to change it, unless you're worried about it splitting over tabs or newlines. – jedwards Jul 07 '12 at 17:27

1 Answers1

2

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.

jedwards
  • 29,432
  • 3
  • 65
  • 92