Tuesday, February 12, 2008

cdjob: Another Multitasking Tool

Have you ever started a long, background make, switched to a different directory to do some work, moved around a bit, and then thought, "I need to look at a file in the make directory"?
$ make &> make.OUT &
[1] 25009
$ cd /tmp
$ cd /etc
...
$ cat make.OUT
cat: make.OUT: No such file or directory
At this point, I used to do something like this:

$ jobs
...
[5]+ Running make >&make.OUT & (wd: /home/jsh/tmp/bar/mumble/frabitz)
$ cd /home/jsh/tmp/bar/mumble/frabitz
To save myself some typing, though, I use

$ cdjob %5
Here's cdjob(), which I define in ~/.bashrc
cdjob ()
{
local d;
: ${1:?"usage $FUNCNAME %N"};
d=$(jobs $1 | perl -lane 'print "cd $1" if m/.*\(wd: (.*)\).*/');
test "$d" && eval $d
}

No comments: