Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a shell script that pulls my repository from git. Normally, it receives the credentials and the pull works just fine.

My question is what happens if the credentials are wrong and I get an authentication failure. How can I catch this error and stop the shell script?

share|improve this question
add comment

1 Answer 1

All commands return a single-byte value (from 0 through 255) after they have completed executing. Usually a 0 return value indicates success and non-0 indicates some sort of problem. Various shells have constructs that check the 0-ness of the return value and can act upon it.

#!/bin/bash
if git ...
then
  echo "git succeeded"
fi

if ! git ...
then
  echo "git failed"
fi

git ... || echo "git failed"
git ... && echo "git succeeded"
share|improve this answer
    
Hi - thanks. What you wrote is working well when I execute the script form command line. However, I execute the script by using php which in this scenario I don't get the response from the git pull command. Any idea? –  Nysd Sbar Oct 24 '12 at 12:23
    
I'm unclear on what you're trying to do. Are you trying to execute the script (or the git pull) from a PHP web page or a PHP-based command-line script? Do you get ANY output? Are there any errors? Can you show us exactly how you're calling the shell script using PHP? If this is in a PHP script, a code snippet will suffice. If you're using the php CLI, the command line should do it. Also note there may be a MUCH better way to do what you're trying to do so you might want to consider filling in the details a bit. –  Jay Allen Oct 30 '12 at 6:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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