bug-bash
[Top][All Lists]
Advanced

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

Re: Yet another quoting question


From: Chet Ramey
Subject: Re: Yet another quoting question
Date: Fri, 06 May 2011 11:50:34 -0400
User-agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10

On 5/6/11 11:02 AM, Steven W. Orr wrote:
>  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 "address@hidden"
>     eval "vals=(\"address@hidden")"
>     echo "print_array:$aname:$size:address@hidden"
> }
> 
> 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"

You've already done the hard part: getting the values from the array
name passed as an argument into `vals'.  For the rest, you can let
printf do the work for you.  Eric suggested %q, and that works to a
certain degree, but you can also use

printf '"%s" ' "address@hidden" ; echo

and get the double-quoting you want.

For straight debugging output, it's probably ok.  You might have to
play with it a little if you want to make it into something you can
eval from a command substitution to copy an array.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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