views:

477

answers:

5

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better.

Here is what I have so far

svn update /var/www/k12/
#svn log --revision "HEAD" /var/www/code/  | head -2 | tail -1 | awk '{print $1}' > /var/www/path/version.txt

# upload the files
rsync -ar --verbose --stats --progress --delete --exclude=*.svn /var/www/code/ example.com:/home/path

# bring database up to date
ssh example.com 'php /path/tasks/dbrefactor.php'

# notify me
ssh example.com 'php /path/tasks/build.php'

Only thing is the other day I changed the paths and forgot to update the rsync call. As a result the "notify me" step ran several times while I was figuring stuff out.

I know in linux you can do command1 && command2 and if command 1 "fails" command2 will not run, but how can I observe the "failure/success" exit codes for debugging purposes. Some of the scripts I wrote myself and I'm sure I will need to do something special.

+1  A: 

The return value of the last-run command is stored in the variable $?. You can use that to determine which command to run next. Overview of special variables.

Stephan202
Thank you for the useful link
Josh Ribakoff
+1  A: 

i think $? contains the last exit code

if [[ -z $? ]]
then
  # notify me
  ssh example.com 'php /path/tasks/build.php'
fi
Question Mark
+4  A: 
Greg Hewgill
How do I view the error code for testing purposes.
Josh Ribakoff
The (nonzero) exit code detected by the shell will be the return value of the shell when it returns. So you would use `$?` after the shell script exits.
Greg Hewgill
+1  A: 

The exit code of a previous process happens to be in $? variable right after its execution. Usually (that's not required, but it's the convention everyone follows) the exit code of a successful command will be equal to 0, and any other value means an error.

Remember of the caveats! One of them is that after these commands:

svn log --revision "HEAD" /var/www/code/  | head -2 | tail -1 | awk '{print $1}'
echo "$?"

the zero result would most likely be returned, because in the $? the return code of awk is contained. To avoid it, set the pipefail option somewhere above the code:

set -o pipefail 1
Pavel Shved
Thanks! I will test this out
Josh Ribakoff
Josh, since you have more than 15 reputation, you might want and are able to "vote up" to the answers you liked and/or found useful--try the buttons near the big numbers.
Pavel Shved
A: 

I would suggest you can use the exit non zero at the points where the failure is expected and before processing step further you will check if [ $? -neq 0 ] then there is a failure.


The $? will always return a non zero number if the last process does not executed successfully.

Sachin Chourasiya