Redirect Output in the Middle of a Program??

Brandon Allbery allbery at ncoast.UUCP
Thu May 5 05:44:36 AEST 1988


As quoted from <2841 at cvl.umd.edu> by liuqing at cvl.umd.edu (Larry Liuqing Huang):
+---------------
| Now more people are dialing into a system via telephone lines. It is
| not unusual that the connection died out in the middle of a big C
| program which you hate to start running from the beginning again.
| Is it possible for the C program or Shell to detect the line problem?
+---------------

>From the time the program starts, the shell has no control over the program's
I/O.  You would have to patch stdio to detect SIGHUP on an fd attached to the
controlling terminal.  (Ecch!)

I use the following scripts instead.  (4.xBSDers will, of course, want to
change the names....)

----------------------------------- cut here -----------------------------------
: bg - run program in the background (essentially nohup)
case $# in
0)	echo "Usage: bg command [args]" >&2
	exit 1
esac
> ./do.out
{
	[ "$TERM" = vt100 ] && echo '\033[1q\c'
	trap '' 3 2 1
	{
		eval nice -20 "$@"
	} > ./do.out 2>&1
	[ "$TERM" = vt100 ] && echo '\033[q\c'
	if [ $? -ne 0 ]; then
		echo '\007\c'
		[ "$TERM" = vt100 ] && echo '\033[4q\c'
		sleep 1
	fi
	echo '\007\c'
	sleep 1
	echo '\007\c'
} &
----------------------------------- cut here -----------------------------------

----------------------------------- cut here -----------------------------------
: fg - run command in background, catching output which is displayed
case "$#" in
0)	echo "usage: fg command [args]" >&2
	exit 1
	;;
esac
> ./do.out
{
	SHELL=/bin/sh export SHELL
	case "$TERM" in
	vt1??)	echo '\033[q\033[1q\c' ;;
	esac
	trap '' 3 2 1
	eval nice -20 "sh -c '$@' > ./do.out 2>&1 < /dev/null"
	rc=$?
	case "$TERM" in
	vt1??)	echo '\033[q\c' ;;
	esac
	case $rc in
	0)	;;
	*)	echo '\007\c'
		case "$TERM" in
		vt100)	echo '\033[4q\c' ;;
		vt1??)	echo '\033[2q\c' ;;
		esac
#		sleep 1
	esac
	echo '\007\c'
	sleep 1
	echo '\007\c'
	kill -2 $$ 2> /dev/null
} &
exec tail -f ./do.out
----------------------------------- cut here -----------------------------------
-- 
	      Brandon S. Allbery, moderator of comp.sources.misc
	{well!hoptoad,uunet!marque,cbosgd,sun!mandrill}!ncoast!allbery
Delphi: ALLBERY						     MCI Mail: BALLBERY



More information about the Comp.unix.wizards mailing list