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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-04-2010, 11:03 AM
|
#1
|
Member
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606
Rep:
|
[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
|
|
|
06-04-2010, 11:21 AM
|
#2
|
Senior Member
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833
Rep: 
|
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
|
|
|
06-04-2010, 12:43 PM
|
#3
|
Member
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606
Original Poster
Rep:
|
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.
|
|
|
06-04-2010, 01:01 PM
|
#4
|
Member
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606
Original Poster
Rep:
|
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.
|
|
|
06-04-2010, 01:02 PM
|
#5
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
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.
|
|
|
06-04-2010, 01:06 PM
|
#6
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by Meson
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 ... ???
|
|
|
06-04-2010, 01:42 PM
|
#7
|
Member
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606
Original Poster
Rep:
|
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"
|
|
|
06-04-2010, 01:45 PM
|
#8
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
But that is not the content of PROFILE_ONE when it is initialised with
Code:
PROFILE_ONE=(
setting1,
"setting number 2",
\"setting number 3\"
)
|
|
|
06-04-2010, 02:02 PM
|
#9
|
Member
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606
Original Poster
Rep:
|
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.
|
|
|
06-04-2010, 09:38 PM
|
#10
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
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'
|
|
|
All times are GMT -5. The time now is 04:53 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|