bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Yet another quoting question


From: Steven W. Orr
Subject: Yet another quoting question
Date: Fri, 06 May 2011 11:02:32 -0400
User-agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10

 4.0.35(1)-release (x86_64-redhat-linux-gnu)

I have a bunch of arrays, and some of the arrays' values are null or might contain spaces.

I wanted to write a routine to print out an array. It just takes the name of the array as an argument. Because some of the values of the array are null, when they print out, they show as two spaces between their neighboring values.

I want the arrays to be printed with surrounding double quotes and I'm having problems doing it. Since expert bash people are the sneakiest people in the world, I thought I'd try you guys. :-)

Here's my print function which does not wrap the printed values in double 
quotes:

print_array()
{
    typeset aname=$1
    typeset -i size=0
    typeset -a vals

    eval "size=\${#$aname[@]}"
    eval "vals=(\"\${$aname[@]}\")"
    echo "print_array:$aname:$size:${vals[@]}"
}

aa=(aaa bbb ccc ddd)
print_array aa
result is:
print_array:aa:4:aaa bbb ccc ddd

I want the output to say
print_array:aa:4:"aaa" "bbb" "ccc" "ddd"

Here's what I tried:
bb="${aa[@]/#/\"}"
echo "${bb[@]}"
"aaa "bbb "ccc "ddd "eee "fff

Now I want to put a double quote at the end of each value. So I try this:
 cc="${bb[@]/%/\"}"
*525 > echo "${cc[@]}"
"aaa "bbb "ccc "ddd "eee "fff"

This only put a double quote at the end of the last value.

So, having said all that:

1. How can I put a double quote at the end of each value (after having computed bb)? 2. Can I do it in one operation instead of having to use intermediate arrays, bb and cc? Is there syntax for this?
3. Is there a way of referring to the pattern part that matched?
4. Are regexes useful here?

I did try also setting extglob. This was the closest I got.
 echo "${aa[@]/*(.*)/\"${BASH_REMATCH[1]}\"}"
""aaa ""bbb ""ccc ""ddd

I'd love to see an elegant solution. :-)

TIA

--
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net



reply via email to

[Prev in Thread] Current Thread [Next in Thread]