home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / shell / 3545 < prev    next >
Encoding:
Text File  |  1992-08-20  |  2.9 KB  |  125 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!sdd.hp.com!mips!darwin.sura.net!convex!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: Exit status of command executed by rsh
  5. Originator: tchrist@pixel.convex.com
  6. Sender: usenet@news.eng.convex.com (news access account)
  7. Message-ID: <1992Aug20.143623.12511@news.eng.convex.com>
  8. Date: Thu, 20 Aug 1992 14:36:23 GMT
  9. Reply-To: tchrist@convex.COM (Tom Christiansen)
  10. References: <THOMSON.92Aug20073755@zarda.macc.wisc.edu>
  11. Nntp-Posting-Host: pixel.convex.com
  12. Organization: Convex Computer Corporation, Colorado Springs, CO
  13. X-Disclaimer: This message was written by a user at CONVEX Computer
  14.               Corp. The opinions expressed are those of the user and
  15.               not necessarily those of CONVEX.
  16. Lines: 107
  17.  
  18. From the keyboard of thomson@zarda.macc.wisc.edu (Don Thomson):
  19. :I'm executing commands on a remote machine using rsh in a Bourne shell script
  20. :and then want to test the exit status of the remote command.  Of course, after
  21. :the rsh, the exit status is that of the rsh executed on my machine rather than
  22. :the command executed on the remote machine.  Is there a particularly
  23. :slick/elegant way to pass back and test the exit status from the remote
  24. :machine?
  25.  
  26. Yup, you use the wonderful ersh command from 
  27. Maarten Litmaath <maart@na47sun05.cern.ch>.  
  28. I'm not sure that this is the current version.
  29.  
  30. --tom
  31.  
  32. #!/bin/sh
  33. # @(#)ersh 3.0 91/06/04 Maarten Litmaath
  34. # This rsh front-end returns the exit status of the remote command,
  35. # or 99 if the connection is broken prematurely.
  36. # It works OK with sh/csh-compatible shells on the remote (!) side.
  37. # If there is no remote command present, rlogin is invoked, which
  38. # need not return a meaningful exit status.
  39. # Usage: see rsh(1).
  40.  
  41. RSH=/usr/ucb/rsh
  42. RLOGIN=/usr/ucb/rlogin
  43.  
  44. hostname=
  45. lflag=
  46. nflag=
  47. user=
  48.  
  49. case $1 in
  50. -l)
  51.     ;;
  52. *)
  53.     hostname=${1?'hostname expected'}
  54.     shift
  55. esac
  56.  
  57. case $1 in
  58. -l)
  59.     lflag=-l
  60.     shift
  61.     user=${1?'username expected after -l flag'}
  62.     shift
  63. esac
  64.  
  65. case $1 in
  66. -n)
  67.     nflag=-n
  68.     shift
  69. esac
  70.  
  71. case $hostname in
  72. '')
  73.     hostname=${1?'hostname expected'}
  74.     shift
  75. esac
  76.  
  77. case $# in
  78. 0)
  79.     exec $RLOGIN $lflag $user "$hostname"
  80. esac
  81.  
  82. id=ersh.$$.`date | awk '{ print $4; }'`
  83. hangup=99
  84.  
  85. AWK='
  86.     prprev == 1 {
  87.         print prev0;
  88.         prprev = 0;
  89.     }
  90.     $1 == "'$id'" {
  91.         prev0 = $0;
  92.         prev2 = $2;
  93.         prev3 = $3;
  94.         prprev = 1;
  95.         next;
  96.     }
  97.     {
  98.         print;
  99.     }
  100.     END {
  101.         if (prprev == 0) {
  102.             exit('$hangup');
  103.         }
  104.         if (prev2 ~ /^[0-9]+0$/) {
  105.             exit(prev2 / 10);
  106.         }
  107.         if (prev2 ~ /^0$/ && prev3 ~ /^[0-9]+$/) {
  108.             exit(prev3);
  109.         }
  110.         exit('$hangup');
  111.     }
  112. '
  113.  
  114. exec 3>&1
  115.  
  116. cmd="( ${*-:} ); exec sh -c 'echo $id "'"$0 $1" >&2'\'' $?0 "$status"'
  117.  
  118. $RSH "$hostname" $lflag $user $nflag "$cmd" 2>&1 >&3 3>&- |
  119.     awk "$AWK" >&2 3>&-
  120. -- 
  121.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  122.  
  123. "The number of UNIX installations has grown to 10, with more expected."
  124.     - _The UNIX Programmer's Manual_, Second Edition, June, 1972.
  125.