[buug] loops and pipe sinks are subshells, aaaargh
Michael Paoli
Michael.Paoli at cal.berkeley.edu
Fri Aug 5 14:30:08 PDT 2011
> From: "Tony Godshall" <togo at of.net>
> Subject: Re: [buug] loops and pipe sinks are subshells, aaaargh
> Date: Fri, 5 Aug 2011 11:47:12 -0700
> [IZ]
>>> Sigh. When I wrote my post I assumed the problem I described would be
>>> instantly recognized by any Unix veteran who (I kept assuming) ...
>
> Ian:
>
> Well, color us whooshed. By the way, you'll get better answers
> faster if you provide complete working code in your questions.
> It's not always about skill: it can also be about time.
Yes, (relatively) true ... doesn't have to be "complete working code" -
that can be, and often is overkill, ... but reduce it down to what's
essentially required and/or needs to be done/solved or at least
sufficiently illustrative example of that, but not (much) more.
"Everything should be made as simple as possible, but no simpler." -
Albert Einstein
> [MP]
>> Yes, instantly recognized. I believe it's also well covered in shell
>> FAQ(s) ...
> ...
>> Among possible approaches:
>>
>> #!/bin/sh
>> output_foo(){
>> echo 'foo 1 ...
>
> Michael:
>
> Well, you still don't get the variable to the outside of the subshell,
> the point of the original question.
Well, yes, but ... easy/trivial enough to (also) do that (or at least I
thought that would likely be sufficiently obvious once the command
substitution was shown - may or may not have been as obvious to all,
but likely quite so to at least the original poster), ... and I have a
tendency to optimize things - in the example the shell variable wasn't
used beyond once as a single argument to an echo command, and the
variable would be an integer - I optimized it down to simply outputting
it, rather than assigning it, echoing it, and then never referring to
that variable/parameter again. At least one of the earlier examples in
the thread I'd optimized in my head down to a tail -n 1, making the
"problem"/"solution" relatively moot for what I thought was the "meat"
of the question, so I waited for a more complete example problem (and I
still optimized it some reasonable bit).
> This does:
... yes, ... and there are also additional approaches (maybe I'll write
up at least one more, or maybe someone else will beat me to it - or
better yet find URL(s) to FAQ(s) or the like that have already better
illustrated the matter and "best" (of class, for POSIX) shell solutions
for that "problem"/issue).
Ian also mentioned case/example at meeting yesterday, that's less
trivial, e.g. one wants to count for not just one string/item, but say
several items. I may write up an example solution for that (have it in
my head ... just have to type, test, post ... all that requires is ...
time - of which I usually seem to have too little). I'll drop a hint
on how that might be approached (and without temporary files): eval
Other note (generally applies) - these are mostly illustrative examples,
and for simplicity/clarity of illustration, are generally lacking in the
error checking and robustness that would typically be appropriate (if
not required) for actual production code, e.g. see also:
http://www.rawbw.com/~mp/unix/sh/#Good_Programming_Practices
More information about the buug
mailing list