home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / shell / 4612 < prev    next >
Encoding:
Text File  |  1992-11-09  |  4.6 KB  |  126 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!darwin.sura.net!sgiblab!sgigate!sgi!fido!news.csd.sgi.com!akshay.csd.sgi.com!vidya
  3. From: vidya@akshay.csd.sgi.com (Vidya Alankar)
  4. Subject: Re: C-Shell question
  5. Message-ID: <1992Nov9.215802.28581@news.csd.sgi.com>
  6. Sender: news@news.csd.sgi.com (Net News CSD)
  7. Organization: Silicon Graphics, Inc.
  8. References: <1992Nov3.130759.27280@eua.ericsson.se> <1992Nov4.080338.9458@eua.ericsson.se> <1992Nov5.020850.572@news.csd.sgi.com> <1992Nov6.115704.23205@cs.utwente.nl>
  9. Date: Mon, 9 Nov 1992 21:58:02 GMT
  10. Lines: 114
  11.  
  12. Original posting:
  13.   Hi,
  14.   I am having a problem with a small script I have written.
  15.  
  16.   #!/bin/csh
  17.   /usr/bin/rm $1
  18.   if ( $status != 0 ) then
  19.     exit 1    
  20.   endif
  21.   exit 0
  22.  
  23.   If I execute this script in a shelltool I get the right actions, that is,
  24.   if the file is removed the exit code is 0 and if the file doesn't exists
  25.   the exit code is 1. Now to the problem :
  26.   If I execute the script with rsh, that is, (rsh 'hostname' 'scriptname'          'filename')
  27.   I always get exit code 0 independent of the existance of the file.
  28.   It seems like the variable $status is not set correctly in that case.
  29.   (not in the way I expects anyway)
  30.   Can anyone out there explain that ?? 
  31.  
  32.  
  33. In article <1992Nov6.115704.23205@cs.utwente.nl>, ahoekstr@cs.utwente.nl (Andre D. Hoekstra) writes:
  34. |> In article <1992Nov5.020850.572@news.csd.sgi.com> vidya@akshay.csd.sgi.com (Vidya Alankar) writes:
  35. |> >The exit status you get is from the command "rsh" and not from the script it
  36. |> >executes. Since "rsh" completes normally, you will see "0" which is its 
  37. |> >"completion ok" status. If you do an "echo $status" inside the script, you
  38. |> >might see the exit status of the command of rm.
  39. |> >
  40. |> >- Vidya
  41. |> 
  42. |> NB: followup redirected to comp.unix.shell
  43. |> 
  44. |> The original question was:
  45. |> 
  46. |> I have a script that goes:
  47. |> 
  48. |> #!/bin/csh # ??
  49. |> 
  50. |> /bin/rm -f file
  51. |> 
  52. |> if ( $status ) then blabla
  53. |> 
  54. |> #end of script; exit
  55. |> 
  56. |> When I run this script normallly it works OK, but when I try to
  57. |> rsh somehost script
  58. |> I always get a $status of 0
  59. |> 
  60. |> As you can see the $status is referenced _inside_ the script, so Mr. vidy,
  61. |> I think you are wrong. Therefore there should a difference in the execution
  62. |> of the rm on thishost and somehost. Maybe the script is not even present on
  63. |> somehost?
  64. |> 
  65. |> Andre--
  66.  
  67. Now, my (hopefully) final answer:
  68.  
  69. From man csh:
  70.  
  71.    Pre-defined and Environment Variables
  72.    
  73.    ...
  74.    
  75.      status     The status returned by the last command.  If it terminated
  76.                                            ^^^^^^^^^^^^
  77.                 abnormally, then 0200 is added to the status.  Built-in
  78.                 commands which fail return exit status 1, all other built-in
  79.                 commands set status 0.
  80.  
  81. "status" is the csh environment variable which stores the status returned by
  82. 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'.
  83.  
  84. Test cases:
  85.  
  86. akshay 259% rsh guest@coke ls /unix ; echo $status
  87. /unix                                           <<<< command ok
  88. 0                                               <<<< rsh returned normally
  89. akshay 275% rsh akshay nocmd ; echo $status
  90. nocmd: Command not found.                       <<<< command not ok
  91. 0                                               <<<< rsh returned normally
  92. akshay 260% rsh unknown ls /unix ; echo $status 
  93. unknown: Unknown host                           <<<< command ok, host unknown
  94. 1                                               <<<< rsh returned abnormally
  95. akshay 261% rsh coke ls /unix ; echo $status
  96. Login incorrect.                                <<<< command ok, no rlogin perm.
  97. 1                                               <<<< rsh returned abnormally
  98.                                                     
  99. 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
  100. this :
  101.  
  102. #!/bin/csh
  103. # rmtst - check status after rm
  104. # Original script modified to save status of 'rm' and check for it later
  105. /bin/rm $1
  106. set status1=$status
  107. echo $status1
  108. if ( $status1 != 0 ) then
  109.         exit 1
  110. endif
  111. exit 0
  112.  
  113. akshay 271% rmtst tstfl; echo $status
  114. rm: tstfl non-existent
  115. 2
  116. 1
  117. akshay 272% rsh akshay /tmp/rmtst tstfl; echo $status
  118. rm: tstfl non-existent
  119. 2
  120. 0
  121.  
  122. I hope the above explanation and test cases will clear the confusion
  123. regarding 'status' variable and 'rsh' command.
  124.  
  125. - Vidya
  126.