Prev: List full path and file name for selected directory
Next: use variable to do some substitution with awk.
From: Lao Ming on 29 Mar 2010 20:35 I want to write a script which produces output for any number of months throughout the year. Thus, as the script compiles data from each month of the year, it saves the totals for each month in a pseudo Bourne shell array. Thus, it might have 3 totals for 2010 (so far). As the script processes each month in a loop, it produces a simple array of printf formats. Except it hasn't turned out to be so simple (for me). Before the loop, I initialize format to null. In the loop, I append a format string to a variable called format: format="$format %s" When the loop completes, I prepend and append quotes and newline. q='"' format="${q}${format}\\n${q}" When I echo that format, it has the correct number of months but when I printf it, I get null. printf "$format" "$month_args" What did I do wrong? Thanks a bunch.
From: Thomas 'PointedEars' Lahn on 29 Mar 2010 20:54 Lao Ming wrote: > I want to write a script which produces output for any number of > months throughout the year. Thus, as the script compiles data from > each month of the year, it saves the totals for each month in > a pseudo Bourne shell array. ^^^^^^^^^^^^^^^^^^^^^^^^^^^ A *what*? > Thus, it might have 3 totals for 2010 (so far). As the script processes > each month in a loop, it produces a simple array of printf formats. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I beg your pardon? > Except it hasn't turned out to be so simple (for me). > > Before the loop, I initialize format to null. What "null"? > In the loop, I append a format string to a variable called format: > > format="$format %s" > > When the loop completes, I prepend and append quotes and newline. > > q='"' > format="${q}${format}\\n${q}" > > When I echo that format, it has the correct number of months but when > I printf it, I get null. What "null"? > printf "$format" "$month_args" > > What did I do wrong? Given that this is equivalent to printf "\"$format %s\\n\"" "$month_args" perhaps the value of $month_args *is* "null", whatever that is? Impossible to tell for sure without your showing some more code, including the loop. PointedEars
From: Bill Marcum on 30 Mar 2010 02:29 On 2010-03-30, Lao Ming <laomingliu(a)gmail.com> wrote: > > In the loop, I append a format string to a variable called format: > > format="$format %s" > > When the loop completes, I prepend and append quotes and newline. > > q='"' > format="${q}${format}\\n${q}" > > When I echo that format, it has the correct number of months but when > I printf it, I get null. > > printf "$format" "$month_args" > > What did I do wrong? Thanks a bunch. > Does your loop have its input or output redirected? If so, the loop is executed in a subshell. -- THEY'RE IN UR BED, EATING UR DREAMZ
From: Chris F.A. Johnson on 30 Mar 2010 02:56 On 2010-03-30, Bill Marcum wrote: > On 2010-03-30, Lao Ming <laomingliu(a)gmail.com> wrote: >> >> In the loop, I append a format string to a variable called format: >> >> format="$format %s" >> >> When the loop completes, I prepend and append quotes and newline. >> >> q='"' >> format="${q}${format}\\n${q}" >> >> When I echo that format, it has the correct number of months but when >> I printf it, I get null. >> >> printf "$format" "$month_args" >> >> What did I do wrong? Thanks a bunch. >> > Does your loop have its input or output redirected? If so, the loop is > executed in a subshell. That was the case in the Bourne shell; it is not executed in a subshell in any POSIX shell. -- Chris F.A. Johnson, author <http://shell.cfajohnson.com/> =================================================================== Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== ===== and is released under the GNU General Public Licence =====
From: Stachu 'Dozzie' K. on 30 Mar 2010 04:25 On 2010-03-30, Chris F.A. Johnson <cfajohnson(a)gmail.com> wrote: > On 2010-03-30, Bill Marcum wrote: [...] >> Does your loop have its input or output redirected? If so, the loop is >> executed in a subshell. > > That was the case in the Bourne shell; it is not executed in a > subshell in any POSIX shell. Except for bash, for example. #v+ [klekosta(a)uw000419 klekosta]$ bash -c 'f=q; echo 1 | while read f; do echo $f; done; echo $f' 1 q [klekosta(a)uw000419 klekosta]$ bash --version GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu) Copyright (C) 2007 Free Software Foundation, Inc. [klekosta(a)uw000419 klekosta]$ #v- -- Secunia non olet. Stanislaw Klekot
|
Next
|
Last
Pages: 1 2 3 Prev: List full path and file name for selected directory Next: use variable to do some substitution with awk. |