Terminate initially if error found...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Terminate initially if error found...
# 1  
Old 01-14-2011
Terminate initially if error found...

Hi,

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.

Example.
Script1.sh
Script2.sh
Script3.sh
Script4.sh
Script5.sh

If Script1.sh has error, or data is not loaded, the script should terminate at this point and should not go to the Script2.sh to load the data.

Has anyone faced this issue before or have written a script which terminates at Script1.sh and does not proceed to next level.

Please advice.

Thanks
# 2  
Old 01-14-2011
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?
# 3  
Old 01-14-2011
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?

Code:
Script1.sh 
Script2.sh 
Script3.sh 
Script4.sh 
Script5.sh

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.

Last edited by methyl; 01-14-2011 at 06:52 PM.. Reason: typos and Ps.
# 4  
Old 01-17-2011
Hi,
I have attached the script as an attachment.

Please advice as to how to write the script which will stop the subscript if errors found, without proceeding further to the next subscript.
# 5  
Old 01-17-2011
Based on your original request, if you simply want your wrapper script to exit if a subscript exits with a non-0 status, then add set -e, as in:
Quote:
Originally Posted by msrahman
Code:
set -e
Script1.sh
Script2.sh
Script3.sh
Script4.sh
Script5.sh

Now based on your attachement, I would do something like:
Code:
(
  set -e
  Script1.sh
  Script2.sh
  Script3.sh
  Script4.sh
  Script5.sh
)

rc=$?
case ${rc} in
  0)
    echo 'Claim table load Successful'  
    mv /space/dbexport/data/cExport.csv /space/dbexport/archive/cExport-`date +%Y%m%d`.csv
    ;;
 
  2)
    echo 'Claims table load finished with warnings/errors--- Return Code=' $rc  
    mv /space/dbexport/data/cExport.csv /space/dbexport/archive/cExport-`date +%Y%m%d`.csv  
    ;;
 
  4)
    echo 'Claims table load failed--- Return Code=' $rc  
    ;;
esac

exit $rc

but this would treat the 5 sub-scripts as a single unit -- which may or may not be what you need.
# 6  
Old 01-17-2011
Can you please explain to me as to what "sed -e" does here.

I dont want it to consider the whole sub-script as one. They are 5 different sub-scripts and each should run individually.

Appreciate any help or advice.

Thanks!
# 7  
Old 01-17-2011
From the bash manpage:
set [--abefhkmnptuvxBCEHPT] [-o option] [arg ...]
set [+abefhkmnptuvxBCEHPT] [+o option] [arg ...]
-e
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.
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.

You could have also done:
Code:
Script1.sh || Script2.sh || Script3.sh || Script4.sh || Script5.sh

rc=$?
.....

instead of the sub-shell. Up to you which implementation works best for you.

Now if you want to have per-script error processing, then you are going to have to do something like:
Code:
Script1.sh

rc=$?
case ${rc} in
  0) rc0script1stuff ;;
  2) rc2script1stuff ; exit ${rc} ;;
  4) rc4script1stuff ; exit ${rc} ;;
  *) echo Script1 has an undefined exit code - ${rc} ; exit ${rc} ;;
esac

Script2.sh

rc=$?
case ${rc} in
  0) rc0script2stuff ;;
  2) rc2script2stuff ; exit ${rc} ;;
  4) rc4script2stuff ; exit ${rc} ;;
  *) echo Script2 has an undefined exit code - ${rc} ; exit ${rc} ;;
esac

..... and so on .....

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Want to terminate command execution when string found in the command output

Hi Experts, I am very much new to linux scripting, I am currently working on reducing my manual work and hence writing a script to automate few task. I am running below command to snmpwalk the router.. snmpwalk -v 3 -u WANDL_SU -a MD5 -A vfipmpls -x DES -X VfIpMpLs -l authPriv... (19 Replies)
Discussion started by: Hanumant.madane
19 Replies

2. UNIX for Dummies Questions & Answers

Terminate a port connection

I have executed a command which has ports that have to be assigned. then I deleted the files. Now I need to reinstall the command. but it says the ports are not free How to terminate the port connections and reinstall in solaris unix ---------- Post updated at 09:07 PM ----------... (7 Replies)
Discussion started by: sriki32
7 Replies

3. Solaris

mcrypt module is not initially loaded with php

Hi I'm trying to do an upgrade for one application and whenever I run the upgradre the program shows the error that mcrypt module is not initially loaded with php. My server is running xampp 0.9 and I know this library of mcrypt is already installed. I changed the php.ini file in order to... (0 Replies)
Discussion started by: dahr
0 Replies

4. Solaris

Application terminate

OS : Solaris X86 gcc version 3.4.6 when i run my application, C with Motif application crashed and formed core. I collected the truss ouput. Incurred fault #6, FLTBOUNDS %pc = 0xFE8B35CD siginfo: SIGSEGV SEGV_MAPERR addr=0x00000275 Received signal #11, SIGSEGV siginfo:... (2 Replies)
Discussion started by: satish@123
2 Replies

5. Programming

command to terminate

hi all how to terminate command eecution process. can you please show me the way thank you (2 Replies)
Discussion started by: munna_dude
2 Replies

6. Shell Programming and Scripting

Terminate process

We have a system user "oracle_usr" always run some process in the system , but sometimes , these processes will not stop automatically until we terminate the process , can suggest the method how to terminate the process that is run by "oracle_usr" and run over 10 minutes ? thx (5 Replies)
Discussion started by: ust
5 Replies

7. Shell Programming and Scripting

terminate process

I want to have a script to terminate the system process that generated by user oracle_usr and have already processed for over 10 minutes , could suggest the script ? thx (1 Reply)
Discussion started by: ust
1 Replies

8. Shell Programming and Scripting

terminate the process

In my system , there are a system user "cronusr" , it is mainly to run the crontab job in the database .Sometimes the cronjob process will be failure ( due to some reason ) so that many cronusr process are in the system , it affect other process in the system , I want to have a script that can... (2 Replies)
Discussion started by: ust
2 Replies

9. Shell Programming and Scripting

Terminate session on completing script

Hai all.. How do i terminate my telnet session automatically when my java applicatiion exits. i have a file run which executes my java application and takes care of all class and library path settings prior to the execution. I would like to terminate my session when my application exits. The... (4 Replies)
Discussion started by: deepsteptom
4 Replies

10. Shell Programming and Scripting

How to terminate a tail -f

I am developing a script with a tail -f in it. My problem is how to terminate it and execute the next line. (4 Replies)
Discussion started by: DonVince
4 Replies
Login or Register to Ask a Question