title: Bash Script Error Catching
body: <p>I am very new to bash scripts, and for my first script attempt I am submitting files to my professor's dropbox within the same server.</p>
  
  <p>The code is this: </p>
  
  <pre><code>#! /bin/bash
  echo "Submit Lab$1? \c"
  
  read choice
  
  if [ $choice = "y" ]; then
     echo "Sending to Prof's dropbox..."
     cp -r /path/to/lab$1 /path/to/dropbox
  else
     echo "Submission terminated."
  fi
  </code></pre>
  
  <p>Usage for the command is simply "<code>$ submit 1</code>" (1 is a single integer, corresponding to which lab I want to submit)</p>
  
  <p>The bash script appends the entered argument (a single integer) to the necessary filename (a directory), then submits that directory to my prof's dropbox using <code>cp</code>. </p>
  
  <p>More for the sake of learning than of absolute necessity, I wanted to create a clean prompt that will catch any <code>cp</code> errors (such as file not existing), and be able to output my own error message.</p>
  
  <p>So basically, something like:</p>
  
  <pre><code>echo "Submit lab$1?"
  
  read choice
  
  echo "Send to Prof's dropbox"
  cp -rv /path/to/lab$1 /path/to/dropbox
  
  &lt;catch any errors from cp&gt;
  
  if [ cp has errors ]
  
     echo "Submission failed."
  
  else if [ cp has no errors ]
  
     echo "Submission successful."
  
  And so on...
  </code></pre>
  
  <p>I understand that I could use verbose mode, and that <code>cp</code> itself prints out errors, but as stated before, I'm doing this for the purpose of learning error catching in bash and being able to make a clean prompt.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p><strong>Also:</strong> I have a similar script which submits labs from my laptop, using <code>scp</code>. Assuming I have this question answered, can I apply the solutions to scp exactly the same way?</p>
  
  <p><strong>EDIT:</strong> I don't want <code>cp</code> errors to output to the shell! The most common error would probably be "cannot stat blah: no such file or directory." I would like to catch that error, and simply say "Submission failed."</p>
  
  <p><strong>EDIT #2:</strong> jcollado's answer is correct, the problem is on my part. Did not end the nested <code>if-else</code> with the proper "<code>fi</code>."</p>
  
  <p>After fixing that up, it worked as intended. Still need to catch the error output but I believe I know where I need to go to figure that out on my own, so this question is answered as far as I'm concerned.</p>
  
  <p><strong>FINAL EDIT:</strong> Ah, what the heck - the redirect was so easy I figured I'd put the solution on here. If using Linux, simply add "<code>2&gt;/dev/null</code>" (if you don't want error messages being saved in any file) after the <code>cp</code> to make "<code>cp -r /first/path /second/path 2&gt;/dev/null</code>"</p>