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

Re: disable IPv6 debian



On 2022-04-15 at 22:52, Greg Wooledge wrote:

> On Fri, Apr 15, 2022 at 09:47:11PM -0400, The Wanderer wrote:
> 
>> On 2022-04-15 at 20:47, Greg Wooledge wrote:

>>> You're also going to exit your script with the exit status from
>>> that last grep command.  That's probably not what you want.  If
>>> it's not, then an explicit "exit 0" at the end might be a good
>>> idea.
>>> 
>>> Or, as another choice, you might want to exit with the exit
>>> status of the *first* grep.  In that case, switching them around
>>> would be better:
>>> 
>>> ps -efw | grep -v grep | grep "$PS"
>> 
>> This would probably result in undesired behavior. I recognize this 
>> pattern; for reasons that I don't entirely grasp but which seem
>> somehow intuitive to me, invoking ps in any of the ways that I've
>> yet found useful and piping the output to grep will result in that
>> grep process - with its arguments - being listed in the ps output.
>> 
>> Because the string you're grepping for is included in that
>> argument list, that line will be matched, and so will be printed.
>> 
>> In order to avoid that, the obvious thing to do is just append ' |
>> grep -v grep' to the pipeline, so that the unwanted result line
>> gets stripped out. I've used that pattern many times.
>> 
>> IOW: Having the "cut out any lines that mention the command that
>> got the search pattern passed to it" command come last is likely to
>> be a necessity.
> 
> Your conclusion is not correct.
> 
> foobar | grep b | grep -v c
> 
> foobar | grep -v c | grep b
> 
> both give the same lines of output.

...Huh. That's so unintuitive that it hadn't even occurred to me to test
it before posting, but I just did test it (with 'ps', not 'foobar',
because there's a reason why 'ps' would be special for this purpose),
and you're correct.

I *think* it makes sense on examination? In that the fact that the first
grep command occurs in the ps output in the first place must mean that
its process is in some sense started before the ps command is run, even
though the ps command comes earlier in the pipeline, so the same must
logically hold true for the *second* grep command as well.

That's a weird fact to have be true to begin with, but given that it
*is* true or we wouldn't have this issue anyway, I suppose it holds
together.

Thanks for getting me to check my own intuition on this...

-- 
   The Wanderer

The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all
progress depends on the unreasonable man.         -- George Bernard Shaw

Attachment: signature.asc
Description: OpenPGP digital signature


Reply to: