I have written a shell script which is a combination of 5 scripts into one.
We have a Record Claim indicator in the scpt ($rc) with which we can come to an conclusion if the script failed to load the data or if the data loaded successfully.
Can any one please help me as to how to write a script which terminates if the $rc fails initially or in the middle.
Well, exit 0 is true, so you can test with logic operators or $?, but make sure you catch errors and return not 0 if there is a problem. In critical unattended scripts, it is good to catch $? on everything down to binary code. Ecen on a pipeline, you cna subshell each command and check for error. Did you know sort errors out if it cannot write all its data?
Please show us how you run these scripts? Are you typing the commands at a keyboard? Are you using unix "cron"? Is there a wrapper script - if so, what does the script contain?
Ps. As DGPickett implies you can indeed cause a sub-Script to exit with a non-zero exit status (e.g. "exit 2")which you can check in a wrapper script. Test the value of the variable $? on the line immediately following the invocation of the sub-Script in a numeric comparison. This is good scripting technique and not at all unusual. Confine exit codes to simple and low numbers because no Shell will allow a non-numeric exit code and many Shells cannot deal with large numbers in exit codes.
Difficult to suggest suitable scripting without knowing the possible values of $rc and knowing whether the variable contains a number or a string.
Ps. It always helps to know what Operating System and version you have and what Shell you prefer.
From the man bash (linux) manpage:set [--abefhkmnptuvxBCEHPT] [-o option] [arg ...]
set [+abefhkmnptuvxBCEHPT] [+o option] [arg ...]-e [INDENT]Exit immediately if a pipeline (which may consist of a single simple command), a subshell command enclosed in parentheses, or one of the commands executed as part of a command list enclosed by braces (see SHELL GRAMMAR above) exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test following the if or elif reserved words, part of any command executed in a && or list except the command following the final && or , any command in a pipeline but the last, or if the command's return value is being inverted with !. A trap on ERR, if set, is executed before the shell exits. This option applies to the shell environment and each subshell envi- ronment separately (see COMMAND EXECUTION ENVIRONMENT above), and may cause subshells to exit before executing all the commands in the subshell.
[/INDENT]So the set -e causes the sub-shell to exit if any command exits with a non-zero status. Each sub-script is executed in order, the execution just stops when one fails.
Of course, all the child scripts must know how to exit not zero on any sort of error, a recursive search for unexpected exits and unchecked conditions.
I tried with the pre-script error processing, with "sed -e". Looks like its working fine, but for what ever case (0 ,2, 4), it should send the e-mail to the user, stating the result.
But as per the script, its not sending e-mail for any failure, but the user is receiving the e-mail for any success.
Can you please look at the sub-script attached to this e-mail and let me know as to where i am going wrong.
Now i am facing a new issue.
When i execute the shell script, it just shows me blank with no progress.
-bash-3.00$ ./sqlldrclaims_backup_new.sh
Password:
SQL*Loader: Release 11.2.0.1.0 - Production on Tue Jan 18 10:26:39 2011
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
-e Execute the ERR trap, if set, and exit if a command has a
nonzero exit status, and is not part of the compound list
following a if, until, or while keyword, and is not part of
an AND or OR list, and is not a pipeline preceded by the !
reserved word. This mode is disabled while reading
profiles.
Good idea to put a line in after saving $rc to display the value. This will tell you if the line is ever executed when the preceding command returns a non-zero exist status.
You can set -ex and get a progress report as child processes are started. However, if there is information to catch in the return code, you need to capture $? in another variable, IMMMEDIATELY, before it is overwritten by your error handling code.
But that is your choice, in terms of error handling architecture. The child script could log everything and just return 0 or 1, so when testing a child, you do not need the parent or a hand coded echo $? to see the result (and you might overlook that in a quick test). Logging should be clear, so it is good to have the parent log just say 'parent started at this time, started child at this time with these arguments, child can write log location, child ended good or bad at this time, parent ended at this time. This way, clutter can be logged down at the leaf level, and in some case rejects or errors are potentially large enough to go in a specific separate file, not the main log, but with the side log path and its stats in the main log. If you reuse the same log path, there should be internal start/end headers and code to prevent two runs at once.
You, too, deserve good error checking, handling, logging. Think of it as a real product, just like processed data. When x runs, it owes me an output file and a log of happy ending or a log of why not, all the time, 100%, unity, 1.0 probability!