[Haifux] Haifux: Quick and Dirty Bash strikes again!
amos.shapira at gmail.com
Wed Jan 1 23:26:56 IST 2014
A new trick I learned since the previous round (as far as I remember):
"jobs -x command which takes pid %" will pass the job's pid to the command,
$ long running command &
$ jobs -x watch pstree -pall %
Will pass "52282" to pstree.
You can stick any background job reference instead of just plain "%" (which
refers to the "current default" background job), and of course you can
stick multiple job references anywhere on the command line.
You can use the "jobs" on the same command line as the one when the
background job is started:
$ find / > /dev/null 2>&1 & jobs -x watch pstree -pall %
This would give the same result as "$!" but more flexible about which
background job is used, e.g. I commonly do:
$ command > /tmp/file 2>&1 & tail --pid $! -F /tmp/file
to watch a long running background process' output and keep its log output
in a file. tail's "--pid" means that it'll exit when that pid terminates
and Bash replaces the "$!" by the pid of the last started background job.
It's probably useful for bash functions/aliases (e.g. I'd like to alias the
"tail" command above so I don't have to repeat the output file name in both
the watched command and the tail), though I haven't got around to construct
such an alias yet (submissions would be gladly accepted :) ).
On 2 January 2014 07:19, Eli Billauer <eli at billauer.co.il> wrote:
> Hello all,
> On Monday (January 6th), Haifux will re-run its good old Schlager "Quick
> and Dirty Bash". Anyone who wants to get a better grip on one-liners in
> bash and some basic scripting, this is the place and time.
> The slides are available at
> This is an extra announcement, as the lecture was added at short notice.
> See you,
> Web: http://www.billauer.co.il
> Linux-il mailing list
> Linux-il at cs.huji.ac.il
[image: View my profile on LinkedIn]
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haifux