home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!elroy.jpl.nasa.gov!sdd.hp.com!mips!darwin.sura.net!convex!convex!tchrist
- From: Tom Christiansen <tchrist@convex.COM>
- Subject: Re: Exit status of command executed by rsh
- Originator: tchrist@pixel.convex.com
- Sender: usenet@news.eng.convex.com (news access account)
- Message-ID: <1992Aug20.143623.12511@news.eng.convex.com>
- Date: Thu, 20 Aug 1992 14:36:23 GMT
- Reply-To: tchrist@convex.COM (Tom Christiansen)
- References: <THOMSON.92Aug20073755@zarda.macc.wisc.edu>
- Nntp-Posting-Host: pixel.convex.com
- Organization: Convex Computer Corporation, Colorado Springs, CO
- X-Disclaimer: This message was written by a user at CONVEX Computer
- Corp. The opinions expressed are those of the user and
- not necessarily those of CONVEX.
- Lines: 107
-
- From the keyboard of thomson@zarda.macc.wisc.edu (Don Thomson):
- :I'm executing commands on a remote machine using rsh in a Bourne shell script
- :and then want to test the exit status of the remote command. Of course, after
- :the rsh, the exit status is that of the rsh executed on my machine rather than
- :the command executed on the remote machine. Is there a particularly
- :slick/elegant way to pass back and test the exit status from the remote
- :machine?
-
- Yup, you use the wonderful ersh command from
- Maarten Litmaath <maart@na47sun05.cern.ch>.
- I'm not sure that this is the current version.
-
- --tom
-
- #!/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>&-
- --
- Tom Christiansen tchrist@convex.com convex!tchrist
-
- "The number of UNIX installations has grown to 10, with more expected."
- - _The UNIX Programmer's Manual_, Second Edition, June, 1972.
-