[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Cannot grep jobs list when jobs called in a function
From: |
Reuti |
Subject: |
Re: Cannot grep jobs list when jobs called in a function |
Date: |
Fri, 29 Apr 2016 15:49:41 +0200 |
> Am 29.04.2016 um 15:44 schrieb Greg Wooledge <wooledg@eeg.ccf.org>:
>
> On Fri, Apr 29, 2016 at 03:32:22PM +0200, Reuti wrote:
>> I understand that the behavior of the builtin `jobs` changes, in case it
>> discovers that it's run a subshell. But what is happening here:
>>
>> $ sleep 300 &
>> [1] 31766
>> $ function lister() { date; jobs; }
>> $ lister
>> Fri Apr 29 15:29:46 CEST 2016
>> [1]+ Running sleep 300 &
>> $ lister | cat
>> Fri Apr 29 15:30:00 CEST 2016
>> [1] Done date
>>
>> My question is: why does the `date` command show up as "done" at all? I
>> would expect the output to be just empty.
>
> I don't fully understand either. Note that the "Done" job is reported
> as "date" instead of "sleep". It would seem the subshell is trying to
> keep track of processes but has become confused.
>
>> Depending on the overall program, this might work to avoid a subshell:
>>
>> if grep -q vim < <(realjobs); then ...
>
> No, that runs "realjobs" in a background subshell. You're thinking of
> the use of a process substitution to keep the grep in the foreground,
> but here it's the "realjobs" part that has to be kept in the foreground.
I tested with this:
$ function lister() { date; jobs; }
$ sleep 300 &
[1] 1931
$ if grep sleep < <(lister); then echo Yes; fi
[1]+ Running sleep 300 &
Yes
-- Reuti