14

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.

The code is this:

#! /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

Usage for the command is simply "$ submit 1" (1 is a single integer, corresponding to which lab I want to submit)

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 cp.

More for the sake of learning than of absolute necessity, I wanted to create a clean prompt that will catch any cp errors (such as file not existing), and be able to output my own error message.

So basically, something like:

echo "Submit lab$1?"

read choice

echo "Send to Prof's dropbox"
cp -rv /path/to/lab$1 /path/to/dropbox

<catch any errors from cp>

if [ cp has errors ]

   echo "Submission failed."

else if [ cp has no errors ]

   echo "Submission successful."

And so on...

I understand that I could use verbose mode, and that cp 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.

Any help would be appreciated.

Thanks!


Also: I have a similar script which submits labs from my laptop, using scp. Assuming I have this question answered, can I apply the solutions to scp exactly the same way?

EDIT: I don't want cp 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."

EDIT #2: jcollado's answer is correct, the problem is on my part. Did not end the nested if-else with the proper "fi."

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.

FINAL EDIT: Ah, what the heck - the redirect was so easy I figured I'd put the solution on here. If using Linux, simply add "2>/dev/null" (if you don't want error messages being saved in any file) after the cp to make "cp -r /first/path /second/path 2>/dev/null"

0

3 Answers 3

25

Although you can test $?, you can also test the exit status of a command directly:

if cp -rv /path/to/lab$1 /path/to/dropbox
then echo Submission successful
fi
exit $?

The errors were already reported to standard error by cp. The exit shown will exit with status 0 (success) if cp was successful; otherwise, it exits with the status that cp exited with.

Clearly, you can wrap that in a loop if you want to, or you can make it non-interactive (but any exit terminates the script).

4
  • Thanks for the alternative solution. Best answer based on simplicity.
    – skippr
    Jan 17, 2012 at 17:53
  • I believe the exit code in the above will always be zero. If cp fails (returning non-0) then if will return 0. bash's echo always returns 0. From the man page, regarding if: The exit status is the exit status of the last command executed, or zero if no condition tested true. Jul 25, 2014 at 16:04
  • 2
    @mr.spuratic: It looks like you're correct. Running if true; then echo true; fi; echo $? in Bash, Korn shell, Heirloom shell prints true and then 0; replace it with if false; then echo true; fi; echo $? and it only prints 0. So, the $? status does not preserve the exit status of the command executed in the if. There are ways around that, of greater or lesser elegance. One such is status=0; if true; then echo true; else status=1; fi; echo $status. Or you can do the inverse: status=1; if true; then echo true; status=0; fi; echo $status. Jul 25, 2014 at 16:21
  • It is possible (bash) to use PIPESTATUS: rather than being set by if's exit status, that is set from the command-list of the condition if the condition was false, and otherwise set within the conditional block as normal. This applies to compound commands if, while and until, and (unsurprisingly) { }. It's not clear to me that this is an explicitly documented behaviour though. I usually just cache $? in a temp or local ASAP in such situations. Jul 29, 2014 at 18:00
14

To check the return code from the previous command, you can use the $? special variable as follows:

if [ "$?" -ne "0" ]; then
  echo "Submission failed"
  exit 1
fi

echo "Submission successful."

Additionally, if you want a clean prompt, you can redired stderr as explained here.

Finally, you can also use this in the script that uses scp, since it works regardless of the command you're using.

5
  • Thanks for the prompt (no pun intended) response! I will look more into stderr redirects, as that seems to be where the "clean prompt" solution lies. However, the code you provided does not work for failed submissions. It does not print "Submission failed." when cp fails. It does however, ignore the "Submission successful" when cp fails.
    – skippr
    Jan 16, 2012 at 21:18
  • I notice that when cp fails, nothing after it executes.
    – skippr
    Jan 16, 2012 at 21:28
  • Ah, problem was on my part. Got it now. Thanks!
    – skippr
    Jan 17, 2012 at 2:18
  • 1
    If you don't mind my asking, what does the integer after "exit" do?
    – skippr
    Jan 17, 2012 at 2:38
  • 2
    The argument to exit is the result code of your own script. It's good practice to make sure your exit code is 0 on success and non-zero otherwise, so that higher-level scripts can call your script and use its result code to handle any errors.
    – tripleee
    Jan 17, 2012 at 7:56
4
echo "Submit lab$1?"

read choice

echo "Send to Prof's dropbox"
cp -rv /path/to/lab$1 /path/to/dropbox &>/dev/null

[ $? -eq 0 ] && { #success, do something } || { #fail, do something }

And so on...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.