LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-04-2010, 11:03 AM   #1
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 67
[bash] indirect array reference to array with values containing spaces


This _almost_ works. I just can't quite get it to honor the spaces.

Code:
#!/bin/bash

profiles=(
        PROFILE_ONE
)

PROFILE_ONE=(setting1 "setting number 2")

for p in "${profiles[@]}"; do

        echo "original p:         ${p}"

        eval p=(\${${p}[@]})

        echo "new p:              ${p}"
        echo "new p contents:     ${p[@]}"

        echo "new p split:"

        for o in "${p[@]}"; do
                echo "                    $o"
        done
done

echo
echo
 
Old 06-04-2010, 11:21 AM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
You could use IFS to do this... http://tldp.org/LDP/abs/html/internalvariables.html (Note change to PROFILE_ONE)
Code:
#!/bin/bash
IFS_ORIG=$IFS
IFS=","
profiles=(
        PROFILE_ONE
)

PROFILE_ONE=(setting1, "setting number 2")

for p in "${profiles[@]}"; do

        echo "original p:         ${p}"

        eval p=(\${${p}[@]})

        echo "new p:              ${p}"
        echo "new p contents:     ${p[@]}"

        echo "new p split:"

        for o in "${p[@]}"; do
                echo "                    $o"
        done
done

echo
echo
IFS=$IFS_ORIG
 
Old 06-04-2010, 12:43 PM   #3
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Original Poster
Rep: Reputation: 67
Thanks,

One oddity about this though, is that newlines still work as a separator. Can anyone explain this?

Just for anyone coming across this, you only need to change the IFS when going over the loop. Also you need to check for some blank entries:

Code:
#!/bin/bash

profiles=(
        PROFILE_ONE
)

PROFILE_ONE=(setting1, "setting number 2")

IFS_ORIG=$IFS
IFS=","
for p in "${profiles[@]}"; do

        echo "original p:         ${p}"

        eval p=(\${${p}[@]})

        echo "new p:              ${p}"
        echo "new p contents:     ${p[@]}"

        echo "new p split:"

        for o in "${p[@]}"; do
                [ "$o" == "" ] && continue
                echo "                    $o"
        done
done
IFS=$IFS_ORIG

echo
echo

Last edited by Meson; 06-04-2010 at 12:51 PM.
 
Old 06-04-2010, 01:01 PM   #4
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Original Poster
Rep: Reputation: 67
Actually, I still have a problem with this. It has to do with quoted strings:

Code:
#!/bin/bash

profiles=(
        PROFILE_ONE
)

PROFILE_ONE=(
        setting1,
        "setting number 2",
        \"setting number 3\"
)

IFS_ORIG=$IFS
IFS=","
for p in "${profiles[@]}"; do

        echo "original p:         ${p}"

        eval p=(\${${p}[@]})

        echo "new p:              ${p}"
        echo "new p contents:     ${p[@]}"

        echo "new p split:"

        for o in "${p[@]}"; do
                [ "$o" == "" ] && continue
                echo "                    $o"
        done
done
IFS=$IFS_ORIG

echo
echo
Here, setting 2 id displayed as an atom without quotes, setting 3 is displayed as 3 atoms, the first one leading with a quote and the third one trailing with a quote.
 
Old 06-04-2010, 01:02 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The code can be tightened up a little like this, assuming IFS starts with the default value (and legibility is not a prime concern!)
Code:
#!/bin/bash

profiles=(
        PROFILE_ONE
)

PROFILE_ONE=(setting1, "setting number 2")

#IFS_ORIG=$IFS
IFS=","
for p in "${profiles[@]}"; do

        echo "original p:         ${p}"

        eval p=(\${${p}[@]})

        echo "new p:              ${p}"
        echo "new p contents:     ${p[@]}"

        echo "new p split:"

        for o in "${p[@]}"; do
                # [ "$o" == "" ] && continue
                [ "$o" == "" ] || echo "                    $o"
        done
done
#IFS=$IFS_ORIG
unset IFS
Note: If IFS is unset bash behaves in all respects as if it had the default value.
 
Old 06-04-2010, 01:06 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Meson View Post
Actually, I still have a problem with this.
...
Here, setting 2 id displayed as an atom without quotes, setting 3 is displayed as 3 atoms, the first one leading with a quote and the third one trailing with a quote.
Why is that a problem? PROFILE_ONE as initialised is a five element array. AIUI the described display is correct ... ???
 
Old 06-04-2010, 01:42 PM   #7
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Original Poster
Rep: Reputation: 67
The example above displays as:
Code:
setting1
setting number 2
"setting
number
3"
This is not what I want. It should be:

Code:
setting1
setting number 2
"setting number 3"
 
Old 06-04-2010, 01:45 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
But that is not the content of PROFILE_ONE when it is initialised with
Code:
PROFILE_ONE=(
        setting1,
        "setting number 2",
        \"setting number 3\"
)
 
Old 06-04-2010, 02:02 PM   #9
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Original Poster
Rep: Reputation: 67
I don't know what you mean by that. If I set IFS=, then I expect for that declaration of PROFILE_ONE to have 3 items, not 5.
 
Old 06-04-2010, 09:38 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Hadn't the IFS=, been moved after the array initialisation? And anyway setting IFS=, and then using "," as an array element separator doesn't work:
Code:
c@CW8:~$ IFS=,
c@CW8:~$ x=(a, b,c)
c@CW8:~$ echo "'${x[0]}'"
'a,'
c@CW8:~$ echo "'${x[1]}'"
'b,c'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
Bash: Insert output of a commando into an array and compare the values tengblad Programming 2 04-07-2009 03:36 PM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM
Bash indirect reference to array variable(s) sixerjman Programming 6 10-25-2006 11:18 AM
Problem displaying Bash array values. shinobi59 Programming 3 01-17-2006 05:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration