English 中文(简体)
How can I use exit codes to run shell scripts sequentially?
原标题:

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.

最佳回答

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
问题回答

The best option, especially for unattended scripts, is to set the -e shell option:

#!/bin/sh -e

or

set -e

This will cause the shell to stop executing if any (untested) command exits with a nonzero error code.

          -e      Exit  immediately if a simple command (see SHELL GRAMMAR
                  above) exits with a non-zero status.  The shell does not
                  exit  if  the  command that fails is part of an until or
                  while loop, part of an if statement, part of a && or  ||
                  list, or if the command s return value is being inverted
                  via !.  A trap on ERR, if set, is  executed  before  the
                  shell exits.

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.

i think $? contains the last exit code

if [[ -z $? ]]
then
  # notify me
  ssh example.com  php /path/tasks/build.php 
fi

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.





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

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

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签