From: Chris F.A. Johnson on
On 2010-03-30, Stachu 'Dozzie' K. wrote:
> 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.

In bash, redirection (other than a pipe) does not cause a loop to
be executed in a subshell.

--
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
On 2010-03-31, Chris F.A. Johnson <cfajohnson(a)gmail.com> wrote:
> On 2010-03-30, Stachu 'Dozzie' K. wrote:
>> 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.
>
> In bash, redirection (other than a pipe) does not cause a loop to
> be executed in a subshell.

And you think Bill meant anything else than a pipe, which is quite
common mistake? I saw him on this group for long time and I don't think
he would believe that redirection to a file executes subshell.

--
Secunia non olet.
Stanislaw Klekot
From: Ed Morton on
On 3/30/2010 1:26 PM, Lao Ming wrote:
> On Mar 29, 11:29 pm, Bill Marcum<marcumb...(a)bellsouth.net> wrote:
>> On 2010-03-30, Lao Ming<laoming...(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.
>
> It doesn't seem to be an issue with the output since I do obtain
> correct output -- just not formatted output because some simple
> problem which I can't see is affecting the format variable. If the
> subshell affects the format variable below, shouldn't it also affect
> the accompanying monthly_args variable as well? It must something
> else I've done or not done.
>
> Is there a good source on the web for reading about subshells? While
> I understand the issue, I have never fully understood every aspect of
> it.
>
> Sorry for bothering you all with what seems to be a trivial issue. I
> should have been able to figure it out by myself. Thanks.
>
>
>
> for mm in 01 02 03 04 05 06 07 08 09 10 11 12
> do
> if [ "$mm" -le "$current_mm" ]
> then
> kmonthly=`./kdaily "$yyyy" |grep "^${yy}${mm}" |grep -w "k" |wc -
> l`
>
> format="$format %s"
> monthly_args="$monthly_args $k_monthly"
>
> fi
> done
> format="\"${format}\\n\""
>
> printf "$format" $monthly_args
>
>
> As to using a variable instead of \" -- all I can say is old habits
> die hard. :)
>

Do you realise you're calling "kdaily" once for every month instead of just
calling it once and then looping through the months? If you post an example of
"kdaily" output and explain what you're really trying to do we could probably
help you come up with a better approach.

Ed.