bug-bash
[Top][All Lists]
Advanced

[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:38:55 +0200

> Am 29.04.2016 um 15:32 schrieb Reuti <address@hidden>:
> 
> 
>> Am 29.04.2016 um 14:15 schrieb Greg Wooledge <address@hidden>:
>> 
>> On Thu, Apr 28, 2016 at 10:38:53AM -0600, Eric Blake wrote:
>>> Bash has code to special-case 'jobs |' when it can obviously tell that
>>> you are running the jobs builtin as the sole command of the left side of
>>> a pipe, to instead report about the jobs of the parent shell,
>> 
>> Oh, that's interesting.  I didn't know that.
> 
> Me too.
> 
> 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.

In detail: the output of the `jobs` command. - Reuti


> but that
>>> special-case code cannot kick in if you hide the execution of jobs,
>>> whether by hiding it inside a function as you did, or by other means
>>> such as:
>>> $ eval jobs | grep vim
>> 
>> In general, if you want to filter the output of "jobs" or some other
>> builtin that changes its behavior when invoked in a subshell, then
>> you need to avoid the subshell.  That means no pipeline, no command
>> substitution, etc.  Basically that leaves you with a temporary file.
>> 
>> tmpfile=...  # boilerplate code to create a temp file on whatever OS
>> trap 'rm -f "$tmpfile"' EXIT
>> jobs > "$tmpfile"
>> if grep -q vim "$tmpfile"; then ...
> 
> Depending on the overall program, this might work to avoid a subshell:
> 
> if grep -q vim < <(realjobs); then ...
> 
> -- Reuti
> 
> 
> 
> 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]