Discussion:
jobs control in a /bin/sh script
(too old to reply)
Nikos Vassiliadis
2016-07-10 15:34:40 UTC
Permalink
Hi,

Is there a way to redirect "jobs" output?
While this works on other bourne-like shells like zsh
or bash, it doesn't on /bin/sh.
$ sleep 100 &
$ sleep 100 &
$ jobs -p
1760
1761
$ jobs -p | cat
$
for j in $jobs
do
(
blah; blah; blah;
) &
done
and I'd like to limit the number of jobs my script could create.

Thanks,
Nikos
Polytropon
2016-07-10 15:52:19 UTC
Permalink
Post by Nikos Vassiliadis
Hi,
Is there a way to redirect "jobs" output?
While this works on other bourne-like shells like zsh
or bash, it doesn't on /bin/sh.
$ sleep 100 &
$ sleep 100 &
$ jobs -p
1760
1761
$ jobs -p | cat
$
for j in $jobs
do
(
blah; blah; blah;
) &
done
and I'd like to limit the number of jobs my script could create.
How about this?

JOBS=`jobs -p`
for j in $JOBS; do
# ... whatever you need to do with $j ...
done

Or the iteration just shortened:

for j in `jobs -p`; do
# ... whatever you need to do with $j ...
done
--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
Nikos Vassiliadis
2016-07-10 18:49:51 UTC
Permalink
Post by Polytropon
JOBS=`jobs -p`
Thank you, that would do what I want. It's rather strange that
the other construct doesn't work. But this workaround would do:)

Thanks,
Nikos
Polytropon
2016-07-11 18:08:06 UTC
Permalink
Post by Nikos Vassiliadis
Post by Polytropon
JOBS=`jobs -p`
Thank you, that would do what I want. It's rather strange that
the other construct doesn't work. But this workaround would do:)
In case of a shell script, calling the "jobs" command will invoke
an internal (shell builtin) command, and this one seems to be
unable to use redirection. There also is the /usr/bin/jobs binary,
but it doesn't seem to work as desired in the scripting context...
--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
Loading...