exiting a child without stopping the parent

Greets all. This is using bash/sh on Slackware Linux 12....

I have a parent and MANY children scripts. The parent would be:

for script in scripts/*.sh; do
   sh $script || exit 1
done

I'm trying to get a child script to quit running without bombing my parent script. This would be the child:

set -e
variable1=1
variable2=2

if [ "$variable1" = "2" ]; then
   what goes here? I need to keep truckin and skip this script.
elif [ "$variable1" = "1" ]; then
    true
fi

# lots more stuff to run

I could hack around it but surely I'm overlooking something simple. I'm fairly inept with bash so thats my problem here. Thanks for any help in advance...

Presumably the for loop in the parent is intended to stop when a child returns an error?

A child should "exit 0" to complete with no errors.

Thanks, that was the first thing I tried but that makes the parent crap out with "exit 0"...

Take the "|| exit 1" out of your parent.

Put the exit command in the child instead of "what goes here..."

If the condition you're testing for is acceptable, exit 0 if your child script is finished. If it is not acceptible, then exit 1 (or whatever number would be helpful... Obviously you would not exit your script after every test. In those cases, set a flag you can check before exiting.

If you want more than just acceptible/not acceptible status for a test's results then pick a number that means something else.

You would want to somehow document the numbers you choose so that if you forget why your child process returned an exit status of 3 you can understand when reviewing your documentation/comments for the script.
For example, say you were expecting a string of 8 characters, got 9, but it's okay with 9 anyway, your child script might return an exit status of 3. In this case the 3 would mean something like "the script ran succesfully but there was a non critical issue." You could also use 3 to mean it failed because such and such happened...

In your parent script, you can test for the exit status of the child script right after calling the child script.
Don't ignore commonly expected standards though, so if the child script returns 0 then it was successful. If it returns 1 it failed. Beyond that, I believe it is up to you.

It's a good idea to just get in the habit of returning exit status from every script you write.

I agree. Thanks for the info. I need to brush up on my trapping instead of always using exit 1 to catch errors.

I have one parent and about 150 children. If I remove the || exit 1 from the parent then I'm in big trouble because the previous child needs to complete sucessfully before the next one is run or it will snowball on me.

I just need to check that $variable = $variable before the child is run (or before the child actually gets going), but $variable is ever changing in each script so I can't set it from the parent.. If $variable != $variable then it needs to be skipped and the next child in line needs to be ran... I'll hack it for now and put it on my TODO list...

set -e
variable1=1
variable2=2

if [ "$variable1" = "2" ]; then
   bail=yes
elif [ "$variable1" = "1" ]; then
    true
fi

if [ "$bail" != "yes" ]; then
   # lots more stuff to run in this child script
fi

Ugly but will work. Thanks again.

Hey... exit 0 does work. The error I get is related to a later command in the parent but I thought it was coming from the child. You are dead right about returning the exit status.

Ughh... One problem down but another one crops up... :slight_smile:

Thanks again guys for your help.