From: vidya@akshay.csd.sgi.com (Vidya Alankar)
Subject: Re: C-Shell question
Organization: Silicon Graphics, Inc.
Date: Mon, 9 Nov 1992 21:58:02 GMT

Original posting:
  Hi,
  I am having a problem with a small script I have written.

  #!/bin/csh
  /usr/bin/rm $1
  if ( $status != 0 ) then
    exit 1	
  endif
  exit 0

  If I execute this script in a shelltool I get the right actions, that is,
  if the file is removed the exit code is 0 and if the file doesn't exists
  the exit code is 1. Now to the problem :
  If I execute the script with rsh, that is, (rsh 'hostname' 'scriptname'          'filename')
  I always get exit code 0 independent of the existance of the file.
  It seems like the variable $status is not set correctly in that case.
  (not in the way I expects anyway)
  Can anyone out there explain that ?? 


In article <1992Nov6.115704.23205@cs.utwente.nl>, ahoekstr@cs.utwente.nl (Andre D. Hoekstra) writes:
|> In article <1992Nov5.020850.572@news.csd.sgi.com> vidya@akshay.csd.sgi.com (Vidya Alankar) writes:
|> >The exit status you get is from the command "rsh" and not from the script it
|> >executes. Since "rsh" completes normally, you will see "0" which is its 
|> >"completion ok" status. If you do an "echo $status" inside the script, you
|> >might see the exit status of the command of rm.
|> >
|> >- Vidya
|> 
|> NB: followup redirected to comp.unix.shell
|> 
|> The original question was:
|> 
|> I have a script that goes:
|> 
|> #!/bin/csh # ??
|> 
|> /bin/rm -f file
|> 
|> if ( $status ) then blabla
|> 
|> #end of script; exit
|> 
|> When I run this script normallly it works OK, but when I try to
|> rsh somehost script
|> I always get a $status of 0
|> 
|> As you can see the $status is referenced _inside_ the script, so Mr. vidy,
|> I think you are wrong. Therefore there should a difference in the execution
|> of the rm on thishost and somehost. Maybe the script is not even present on
|> somehost?
|> 
|> Andre--

Now, my (hopefully) final answer:

From man csh:

   Pre-defined and Environment Variables
   
   ...
   
     status     The status returned by the last command.  If it terminated
                                           ^^^^^^^^^^^^
                abnormally, then 0200 is added to the status.  Built-in
                commands which fail return exit status 1, all other built-in
                commands set status 0.

"status" is the csh environment variable which stores the status returned by
the LAST/PREVIOUS command. The status returned by 'rsh' command is NOT the status returned by the 'rm' command nor that returned by the script. It only indicates whether 'rsh' completed normally or not. Unless there is some network/protocol/login-permission problem, rsh will complete normally and return '0'.

Test cases:

akshay 259% rsh guest@coke ls /unix ; echo $status
/unix                                           <<<< command ok
0                                               <<<< rsh returned normally
akshay 275% rsh akshay nocmd ; echo $status
nocmd: Command not found.                       <<<< command not ok
0                                               <<<< rsh returned normally
akshay 260% rsh unknown ls /unix ; echo $status 
unknown: Unknown host                           <<<< command ok, host unknown
1                                               <<<< rsh returned abnormally
akshay 261% rsh coke ls /unix ; echo $status
Login incorrect.                                <<<< command ok, no rlogin perm.
1                                               <<<< rsh returned abnormally
                                                    
If you want to see the '$status' from inside the script, you will have to save it as another variable and echo it. The original script is modified below for
this :

#!/bin/csh
# rmtst - check status after rm
# Original script modified to save status of 'rm' and check for it later
/bin/rm $1
set status1=$status
echo $status1
if ( $status1 != 0 ) then
        exit 1
endif
exit 0

akshay 271% rmtst tstfl; echo $status
rm: tstfl non-existent
2
1
akshay 272% rsh akshay /tmp/rmtst tstfl; echo $status
rm: tstfl non-existent
2
0

I hope the above explanation and test cases will clear the confusion
regarding 'status' variable and 'rsh' command.

- Vidya
Newsgroups: comp.unix.shell