title: Linux if exit codes in csh
body: <p>How can you pass the exit code of a failed script in an if else statement in csh? e.g.</p>
  
  <pre><code>if ( "test" != "test" ) then
     /bin/true
  else 
      /bin/false
  endif
  
  echo $?
  </code></pre>
  
  <p>gives an exit code of 0. However just running </p>
  
  <pre><code>/bin/false
  echo $?
  </code></pre>
  
  <p>gives an exit code of 1. </p>
  
  <p>It seems as if the endif just overwrites the exit code of whatever the output of the commands are within the if else statement.</p>
  
  <pre><code>endif 
  echo $?
  </code></pre>
  
  <p>exit code: 0</p>
  
  <p>So is there a better way of passing the exit code from the statement apart from setting it to a variable after running each of the commands? </p>
  
  <p>e.g.</p>
  
  <pre><code>if ( "test" != "test" ) then
     /bin/true
     set exitcode = $?
  else 
      /bin/false
     set exitcode = $?
  endif
  </code></pre>
  
  <p>The /bin/false and /bin/true are just examples of scripts in those positions either failing of succeeding, and how the endif overrides their exit codes. So the exits codes of either of these statements could be anything [0-255]. I'm not trying to capture a simple 1 or 0. </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The below suggestion by Thomas seems to work if you create a csh script.</p>
  
  <pre><code>#!/bin/csh
  if ( "test" != "test" ) then
      /bin/true
      exit $?
  else 
      /bin/false
      exit $?
  endif
  </code></pre>
  
  <p>But it doesn't appear to work if using ssh</p>
  
  <pre><code>ssh -A -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $USER@`hostname` '
  &gt;if ( "test" != "test" ) then
  &gt;    /bin/true
  &gt;    exit $?
  &gt;else 
  &gt;    /bin/false
  &gt;    exit $?
  &gt;endif'
  echo $?
  0
  </code></pre>