On 5/6/2011 12:05 PM, Chet Ramey wrote:
On 5/6/11 12:00 PM, Greg Wooledge wrote:
On Fri, May 06, 2011 at 11:50:34AM -0400, Chet Ramey wrote:
Eric suggested %q, and that works to a
certain degree, but you can also use
printf '"%s" ' "${vals[@]}" ; echo
and get the double-quoting you want.
Fails horribly if the array elements contain double quotes of their own.
Read what I wrote:
"For straight debugging output, it's probably ok."
Presumably Steven understands something about the expected values and will
be able to cope with a stray double-quote.
Chet
Thanks to everyone. printf -q was something I didn't know about, but then I
also found out about a single %s that would service each element of an array.
Chet's and Greg's warnings about printing things that might contain double
quotes are good to know about. In my particular case, the arrays may contain
strings which are passed as arguments to ssh commands, so the level of
evaluations are non-trivial to begin with. But because of how they are used,
they already contain enough backslashes to get them happily through the printf
using '"%s" '
Here's one example so people can snicker at me. :-)
xx="ls /usr/src | awk '/^linux-/ && ! /obj/ {sub(/linux-/, \\\"\\\", \\\$1);
print \\\$1}'"
This is all one line and ends up being the first element of an array that was
created by doing this
set -- $(eval echo \"\${${name}[@]}\")
to another array whose index got me to a string that was delimited by a lbs
signs. The set then got me each element.
Again, Thanks :-)
--
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