ersh 3.0 - rsh frontend returning exit status of remote cmd (new)

Maarten Litmaath maart at nat.vu.nl
Wed Jun 5 06:35:11 AEST 1991


This version is more bulletproof.

--------------------cut here--------------------
#!/bin/sh
# @(#)ersh 3.0 91/06/04 Maarten Litmaath
# This rsh front-end returns the exit status of the remote command,
# or 99 if the connection is broken prematurely.
# It works OK with sh/csh-compatible shells on the remote (!) side.
# If there is no remote command present, rlogin is invoked, which
# need not return a meaningful exit status.
# Usage: see rsh(1).

RSH=/usr/ucb/rsh
RLOGIN=/usr/ucb/rlogin

hostname=
lflag=
nflag=
user=

case $1 in
-l)
	;;
*)
	hostname=${1?'hostname expected'}
	shift
esac

case $1 in
-l)
	lflag=-l
	shift
	user=${1?'username expected after -l flag'}
	shift
esac

case $1 in
-n)
	nflag=-n
	shift
esac

case $hostname in
'')
	hostname=${1?'hostname expected'}
	shift
esac

case $# in
0)
	exec $RLOGIN $lflag $user "$hostname"
esac

id=ersh.$$.`date | awk '{ print $4; }'`
hangup=99

AWK='
	prprev == 1 {
		print prev0;
		prprev = 0;
	}
	$1 == "'$id'" {
		prev0 = $0;
		prev2 = $2;
		prev3 = $3;
		prprev = 1;
		next;
	}
	{
		print;
	}
	END {
		if (prprev == 0) {
			exit('$hangup');
		}
		if (prev2 ~ /^[0-9]+0$/) {
			exit(prev2 / 10);
		}
		if (prev2 ~ /^0$/ && prev3 ~ /^[0-9]+$/) {
			exit(prev3);
		}
		exit('$hangup');
	}
'

exec 3>&1

cmd="( ${*-:} ); exec sh -c 'echo $id "'"$0 $1" >&2'\'' $?0 "$status"'

$RSH "$hostname" $lflag $user $nflag "$cmd" 2>&1 >&3 3>&- |
	awk "$AWK" >&2 3>&-



More information about the Comp.unix.shell mailing list