| #!/usr/bin/ksh |
| # |
| # Run 'cmd arg ...' on another host |
| # |
| # usage: runh [-h host] cmd arg ... |
| # |
| # -h host: use 'host' instead of $REMOTEHOST |
| # |
| # The default is $REMOTEHOST |
| # |
| |
| # |
| # qargs - quote arguments to ensure that single arguments with embedded |
| # spaces are passed to remote host as a single argument |
| # |
| function qargs { |
| _tmp="" |
| for a in "$@"; do |
| _tmp="$_tmp "\'$a\'; |
| done |
| print $_tmp |
| } |
| |
| # |
| # determine the name of the remote host |
| # |
| host=$REMOTEHOST |
| if [ "$1" = "-h" ]; then |
| host=$2; |
| shift 2; |
| fi |
| |
| # |
| # compute the directory containing the command to run; this is added to the |
| # remote path; some compiler shells require that this is the case. |
| # |
| SRC="${1%/*}" |
| if [ "$SRC" = "$1" ]; then |
| SRC="./" |
| fi |
| |
| # |
| # Set the PATH to include rsh, rm and cat |
| # |
| PATH=/bin:/usr/bin:$PATH |
| export PATH |
| |
| # |
| # create a temporary error log file and a file that signals that the exit |
| # status of the command is not 0; ensure that both files are removed when this |
| # script exits |
| # |
| elog=.tmp$$ |
| exitfile=.tmpex$$ |
| trap "rm -f $elog $exitfile" 0 |
| |
| # |
| # run the specified command on the remote host |
| # |
| rsh $host ksh <<ENDCMD 2> $elog |
| PATH=$PATH:\$PATH:$SRC:/usr/bin:/bin; export PATH; \ |
| LD_LIBRARY_PATH=$LD_LIBRARY_PATH:\$LD_LIBRARY_PATH; \ |
| export LD_LIBRARY_PATH; \ |
| cd `/bin/pwd`; \ |
| `qargs "$@"` |
| if [ $? -ne 0 ]; then |
| echo $? > $exitfile |
| fi |
| ENDCMD |
| |
| # |
| # if the error log is non-empty, print it |
| # |
| if [ -s $elog ]; then |
| cat $elog; |
| fi |
| |
| # |
| # if remote command status is not 0, it is saved in $exitfile, but for now |
| # we just exit with 1, instead of reading that status |
| # |
| if [ -e $exitfile ]; then |
| exit 1 |
| fi |
| exit 0 |