Bash Exit Status and Scripts
Not sure if this is the right forums for this... but...
is it possible to have an if statement cover an entire script, so I can check if at any point there is an exit status that isn't 0? |
Quote:
The question feels like you are asking for help with a solution that is probably not the best solution to the problem. How about you tell us what the problem is? You may find LQ's How To Ask a Question helpful. |
Well I am writing several backup scripts. 1 for each server. They are being rsync'd and I want to be able to execute a certain command if at any point to script fails. There are a few otehr things being run other than just the rsync command (archiving the data, making folders, etc)
|
Quote:
2. Specify the exit code you want, don't leave that to chance either: Code:
$ (commmand) || exit (choose a number 0-255) 3. Read the exit codes in the parent script: Code:
$ (script_name) |
Okay thanks, let me make sure I understand this correctly. The way I've scripted this so far, it seems like this will integrate quite nicely...
I have 1 script that executes all scripts. It's something like this: FINAL_SCRIPT: script1.sh script2.sh script3.sh etc etc etc So, can I do this? script1.sh || exit 1 if [ $? -eq 1 ]; then echo "Oh noes!!" fi script2.sh..... etc |
Quote:
From your example it looks like you want to stop the sequence on the first error. If so, do it this way: script1.sh && script2.sh && script3.sh (etc..) |
No, I don't want to stop it, I just want to stop the specific script itself. Each script is backing up a different server and each script appends a line of text to a file (which says if the backup succeeded or not) and then the main script emails the report after backing everything up.
Edit: It's just a pain editing 5 scripts and after each of the 4+ commands adding an if statement to it. |
Quote:
Quote:
Do you want to make a report about success or failure after the script exits? Is this the goal? Then: $ script-name && echo "success!" >> destination_log Add things like date and time and the name of the script to the "echo" quoted text. The bottom line is that to program a computer, you have to be very, very specific. It turns out the same thing is true here. |
Thanks, you have the right idea now and that should do now that I think of it...
|
What you usually have to do is for each command that you run in the script, check the exit status. You cannot do it like you say and somehow put a big if statement, it would always return true. So check each command, either put each in an if statement or just use the or || notation.
|
If you want to keep your master script short you could run the sub-scripts in a loop
Code:
for script in script1.sh script2.h ... |
Extending the above; each script1.sh etc probably has multiple cmds, any of which can fail
Code:
# empty log file for this run |
All times are GMT -5. The time now is 08:31 AM. |