0

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?

1 Answer 1

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"
2
  • 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, 2012 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, 2012 at 6:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .