title: how to get the return code of scp command failing with connection lost error in shell script
body: <p>I have a shell script in which i am pulling a remote server's *.gz file using below command and after scp , i am executing gunzip command.
  Issue is that while doing scp ,connection is getting lost, so incomplete *.gz file is being saved on my local server directory and when i am trying to gunzip in the next line below scp command ,its gunzipping the file succesfully and when i open the file ,it contains garbage values.</p>
  
  <pre><code>scp ${HostUser}@${HostServer}:$4/*$no*.gz 
  gunzip 
  </code></pre>
  
  <p>When i debugged i found below reason:
  1. Incomplete File transfer due to connection lost
  2. While executing the gunzip command manually, it was giving end of the file not found error.So it was randomly creating garbage value in file and my script is going in success which is not correct.
  So my queries are:</p>
  
  <ol>
  <li><p>Can I store return code of scp command something through which i can come to know whther complete file transfer was done .If complete trasnfer is not done then the fail the script so that gunzip command dont try to open incomplete file and store garbage value after gunzip command</p></li>
  <li><p>I am also facing strange issue that while running scp command for first time,connection is lost but when i fire scp command again from same session,its successful. But in production everytime it wil be new session , so we cant afford to have regular failures of job
  Also one particular file ,connection is lost with 5 seconds. I checked with remote server timeout session is 5 minutes</p></li>
  </ol>
  
  <p>please suggest</p>