home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3442 < prev    next >
Encoding:
Internet Message Format  |  1991-06-06  |  1.6 KB

  1. From: maart@nat.vu.nl (Maarten Litmaath)
  2. Newsgroups: comp.unix.shell,comp.unix.programmer,alt.sources
  3. Subject: ersh 3.0 - rsh frontend returning exit status of remote cmd (new)
  4. Message-ID: <1991Jun04.213511.1536@nat.vu.nl>
  5. Date: 4 Jun 91 20:35:11 GMT
  6.  
  7. This version is more bulletproof.
  8.  
  9. --------------------cut here--------------------
  10. #!/bin/sh
  11. # @(#)ersh 3.0 91/06/04 Maarten Litmaath
  12. # This rsh front-end returns the exit status of the remote command,
  13. # or 99 if the connection is broken prematurely.
  14. # It works OK with sh/csh-compatible shells on the remote (!) side.
  15. # If there is no remote command present, rlogin is invoked, which
  16. # need not return a meaningful exit status.
  17. # Usage: see rsh(1).
  18.  
  19. RSH=/usr/ucb/rsh
  20. RLOGIN=/usr/ucb/rlogin
  21.  
  22. hostname=
  23. lflag=
  24. nflag=
  25. user=
  26.  
  27. case $1 in
  28. -l)
  29.     ;;
  30. *)
  31.     hostname=${1?'hostname expected'}
  32.     shift
  33. esac
  34.  
  35. case $1 in
  36. -l)
  37.     lflag=-l
  38.     shift
  39.     user=${1?'username expected after -l flag'}
  40.     shift
  41. esac
  42.  
  43. case $1 in
  44. -n)
  45.     nflag=-n
  46.     shift
  47. esac
  48.  
  49. case $hostname in
  50. '')
  51.     hostname=${1?'hostname expected'}
  52.     shift
  53. esac
  54.  
  55. case $# in
  56. 0)
  57.     exec $RLOGIN $lflag $user "$hostname"
  58. esac
  59.  
  60. id=ersh.$$.`date | awk '{ print $4; }'`
  61. hangup=99
  62.  
  63. AWK='
  64.     prprev == 1 {
  65.         print prev0;
  66.         prprev = 0;
  67.     }
  68.     $1 == "'$id'" {
  69.         prev0 = $0;
  70.         prev2 = $2;
  71.         prev3 = $3;
  72.         prprev = 1;
  73.         next;
  74.     }
  75.     {
  76.         print;
  77.     }
  78.     END {
  79.         if (prprev == 0) {
  80.             exit('$hangup');
  81.         }
  82.         if (prev2 ~ /^[0-9]+0$/) {
  83.             exit(prev2 / 10);
  84.         }
  85.         if (prev2 ~ /^0$/ && prev3 ~ /^[0-9]+$/) {
  86.             exit(prev3);
  87.         }
  88.         exit('$hangup');
  89.     }
  90. '
  91.  
  92. exec 3>&1
  93.  
  94. cmd="( ${*-:} ); exec sh -c 'echo $id "'"$0 $1" >&2'\'' $?0 "$status"'
  95.  
  96. $RSH "$hostname" $lflag $user $nflag "$cmd" 2>&1 >&3 3>&- |
  97.     awk "$AWK" >&2 3>&-
  98.