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?

jonsca
  • 4,077
  • 15
  • 35
  • 47
Nysd Sbar
  • 1
  • 1
  • 1

1 Answers1

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"
Ignacio Vazquez-Abrams
  • 111,361
  • 10
  • 201
  • 247
  • 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 06:39