Funny csh output?

David Elliott dce at smsc.sony.com
Thu Mar 1 09:23:29 AEST 1990


In article <6669 at cps3xx.UUCP> davisd at cpsvax.UUCP (Dug) writes:
>In article <12251 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn) writes:
>#In article <6662 at cps3xx.UUCP> davisd at cpsvax.cps.msu.edu (Dug) writes:
>#>% jobs | wc
>#>       0       0       0
>#
>#There are no suspended jobs in the shell that's running the pipeline
>#(it's a subprocess of the one that printed the prompt).
>
>But it that's true then why can I redirect it to a file and have my
>stopped jobs show up there?  (i.e.  jobs > test  will result in "test"
>having the correct output of jobs)

Mainly because it's an easier problem to solve.

With redirection of a builtin, all you have to do is to save the
current file descriptor state, run the builtin, and then put it back.
When you work with pipes, you have to fork new subshells to run each
piece.

If you take a look at the csh source (sh.h, specifically), you'll find
that csh starts out by saving the standard file descriptors so that it
can handle redirection for builtins easily.

It is interesting to note that some builtins do work with pipes.
History, for example, does just what you'd expect with a pipe
(in most cases -- on the machine I'm using right now, history | more
doesn't work correctly).

If you do need to send the output of jobs through a pipe, your
best bet is to just use a temp file.  In my .cshrc, I have the
following alias which allows me to use the output of "jobs -l"
to send me to the working directory for any given job:

	alias jd 'jobs -l >! $home/.jobdir ; eval `jobdir < $home/.jobdir`'

The jobdir command is a simple shell script:


#!/bin/sh
#
# jobdir - print a command to cd to the directory where the named job
#	   is executing.  The argument can be +, -, or a 1- or 2-digit
#	   number.  No argument is the same as '+'.
#

PATH=/bin:/usr/bin:/usr/ucb

#
# Global variables
#

Myname=`basename "$0"`

main()
{

	case "$1" in
	""|+)
		dir=`getplus`
		;;
	-)
		dir=`getminus`
		;;
	[0-9]|[0-9][0-9])
		dir=`getnum "$1"`
		;;
	*)
		echo "echo bad directory"
		exit
		;;
	esac

	case "$dir" in
	"")
		echo "cd ."
		;;
	*)
		echo "cd $dir"
		;;
	esac
	exit 0
}

getplus()
{
	sed -n 's/^[^ ]*  +.*(wd: \(.*\))$/\1/p'
}

getminus()
{
	sed -n 's/^[^ ]*  -.*(wd: \(.*\))$/\1/p'
}

getnum()
{
	sed -n 's/^\['"$1"'\].*(wd: \(.*\))$/\1/p'
}

main ${1+"$@"}
exit 0
-- 
David Elliott
dce at smsc.sony.com | ...!{uunet,mips}!sonyusa!dce
(408)944-4073
"...it becomes natural, like a third sense." -- Homer Simpson



More information about the Comp.unix.questions mailing list