Sujet : Re: (bash) How (really!) does the "current job" get determined?
De : gazelle (at) *nospam* shell.xmission.com (Kenny McCormack)
Groupes : comp.unix.shellDate : 04. Oct 2024, 14:13:59
Autres entêtes
Organisation : The official candy of the new Millennium
Message-ID : <vdopmn$2ptf8$1@news.xmission.com>
References : 1 2
User-Agent : trn 4.0-test77 (Sep 1, 2010)
In article <
20241003170607.397@kylheku.com>,
Kaz Kylheku <
643-408-1753@kylheku.com> wrote:
On 2024-10-03, Kenny McCormack <gazelle@shell.xmission.com> wrote:
Note: This is a "How do things really work - in the real world?", rather
than a "What does the manual say about how things work?" sort of thread.
>
The manual says the answer is "The job most recently started in the
background or most recently stopped." This is not always the case.
>
Observe (this is bash version 5.2.15) (and "j" is aliased to "jobs -l"):
>
It looks buggered.
Indeed it does. What this means from a scripting programmer's
point-of-view is that you can't count on it. You can't rely on the job you
just launched being the "current job". Thus, you have to convert $! into a
job id, via something like:
jobs -l | awk $!' == $2 { print substr($0,2)+0 }'
From the bash developers point-of-view, the question becomes: What specific
set of circumstances triggers this?
Note also that the underlying problem here is that while most of the "job
related" commands that take a "job spec" will take either something like %1
or an actual pid, but the "fg" command only takes %n. So, if you want to
fg the most recent job, you need to obtain the job id (via the command line
above), before passing it to "fg". Note that "fg" with no arg at all would
fg the wrong job.
It must be that Bash has no test cases covering the documented
requirements in this area adequate enough to catch what you have found.
>
Is this automatically tested at all?
>
Testing interactive job control features pretty much requires Bash to be
behind a pseudo-tty; driven by expect or something like it.
Indeed. Good point.
(Or else at least a unit test is required where the function that
identifies the current job is tested in isolation, with the various
conditions mocked up: suspended job introduced while existing job is
stopped, etc.)
Yes.
-- Trump could say he invented gravity, and 40% of the country would believe him...This is where we are at, ladies and gentlemen.