Operating System - HP-UX
1822480 Members
2741 Online
109642 Solutions
New Discussion юеВ

Re: Exit commands not working in shell script

 
Marjorie MacLean
New Member

Exit commands not working in shell script

Hi--
I writing a POSIX shell script on HPUX 11.0 that I intend to have exit at various points with different error codes and the exit statements are not working (they are just being ignored and the script is continuing to execute). A smaller, simpler script containing exit codes does work when executed manually. The script will normally be cronned, but I'm pretty sure that the problem occurs when being run manually (it is difficult to test since it contains LVM and XP commands). Surely this can be done?
15 REPLIES 15
harry d brown jr
Honored Contributor

Re: Exit commands not working in shell script

Can you post the script via an attachment? Are you using "subroutines", especially where the "exits" don't appear to work?
Live Free or Die
Bill McNAMARA_1
Honored Contributor

Re: Exit commands not working in shell script

if you are doing something like this:

xpinfo | grep HP

echo $?

Then you'll have the exit code for the grep not the xpinfo command.

Seperate your commands if you want to get error codes.

Later,
Bill
It works for me (tm)
Marjorie MacLean
New Member

Re: Exit commands not working in shell script

Thanks for your replies! Unfortunately, I cannot post the script for confidentiality reasons. I'm not that I'm having problems getting the right exit code, it's that the script is not exiting at all! I'm not calling other scripts, but I am executing commands that may be forking other processes. It's very strange that the script is continuing past the exit. I'm wondering whether it's an issue with cron...
harry d brown jr
Honored Contributor

Re: Exit commands not working in shell script

Can you post a whitewashed code sniplet, so that we can get a better understanding of what you mean by it ignoring "exits"?
Live Free or Die
James R. Ferguson
Acclaimed Contributor

Re: Exit commands not working in shell script

Hi:

You are probably not executing what you think you are executing. I suggest adding 'set -x' to your script or running it with this option to print commands and their arguments as they are processed:

# sh -x ./my.sh

Regards!

...JRF...
Carlos Fernandez Riera
Honored Contributor

Re: Exit commands not working in shell script

At least in older versions, when you fall in nested loops several times, sh opens new shs to process it.

So, and exit will not break your sh, just the new sh automaticaly generated.

unsupported
Marjorie MacLean
New Member

Re: Exit commands not working in shell script

Thanks again. I'm pretty sure that I know what I'm executing... but it never hurts to double-check. I've attached a portion of a whitewashed version. It's pretty straight forward and does not use subroutines.
harry d brown jr
Honored Contributor

Re: Exit commands not working in shell script

Marjorie,

I agree with you, your script looks sound. I cut out the section that did the mount test and it worked correctly. I'm wondering if it is something in your environment, but I can't imagine what that would be.

Do you have this posix (sh) patch installed?

ftp://ftp.itrc.hp.com/hp-ux_patches/s700_800/11.X/PHCO_23873

Live Free or Die
Darrell Allen
Honored Contributor

Re: Exit commands not working in shell script

Hi Marjorie,

That's an interesting puzzle. There's got to be a logical explaination. I took you attachment, commented out most things, and the exit worked fine. I'd try some things such as:

Try adding an exit just before: if [ "$DIRCNT" -ne 3 ]

Try swapping the exit and echo lines (just for grins).

Put an echo and exit just after the fi ending your DIRCNT -ne 3 logic.

Check the log file again. Perhaps there's a clue hidden in it. I find it hard to believe exit is being ignored if actually seen by the shell.

When you solve this there's a bunch of people who'd like to know the answer! Please post it. Thanks,

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
A. Clay Stephenson
Acclaimed Contributor

Re: Exit commands not working in shell script

Hi Marjorie:

There are a few POSIX sh patches that deal with exit although I didn't find any that fit yours precisely. I would install the latest cumulative patch PHCO_23873.

The other thing I would do is put an echo after your exit in question. That way you know for sure if the problem is somehow related to loops, functions, child-processes, etc.
If it ain't broke, I can fix that.
Marjorie MacLean
New Member

Re: Exit commands not working in shell script

Here's the log excerpt minus stderr (There's nothing in stderr). As you can see, it just keeps on truckin'!
A. Clay Stephenson
Acclaimed Contributor

Re: Exit commands not working in shell script

Hi Marjorie:

I simply cannot reproduce this. I even tried some very creative uses of the trap cmd 0 statement to try to cause the exit to be ignored. I thought I saw one construct which could cause problems. Your echo "something "$XX" something else" which really outght to have the enclosed quotes escaped -> echo "something \"${XX}\" something else" - but that didn't do anything fix it.

My next suggestion is twofold.

1) Put an echo "Set: ${-}" statement in so that you can see any options set.

2) Put some probes in very early in your code with an exit statement.
If it ain't broke, I can fix that.
Wodisch
Honored Contributor

Re: Exit commands not working in shell script

Hello Marjorie,

I do believe your problem stems from the use
of "ls ...|wc -l" in your script. If you do
this interactively, then "ls" does behave
differently than in scripts! Try using the
option "-1" (read: dash one) for the "ls",
then try again - and this time start your
script with "sh -v -x yourscript"...
Then you will see the command BEFORE and AFTER
the shell does the substitution.
BTW, if you insert something like
exec 2>&1 > $LOG
as one of the first lines in your script, then
you do not need all those ">> $log" in all the
lines :-)

Good luck,
Wodisch
Timothy Czarnik
Esteemed Contributor

Re: Exit commands not working in shell script

Marjorie,

In looking at the logs and the script you submitted, I see that when the DIRCNT is NOT EQUAL to 3, this should exit. But in the log I see that the DIRCNT was equal to three by the line :

Only 3 filesystems mounted

Not sure if this will trigger something for someone, but its looks to me as though the DIRCNT was equal to 3, but the script tried to exit anyway...

Anyone?

-Tim
Hey! Who turned out the lights!
Marjorie MacLean
New Member

Re: Exit commands not working in shell script

My apologies on the log excerpt not matching the script excerpt. I sent only a whitewashed version of the script where the log is an actual log exerpt pretty much. The script that generated the log is identical except that it mounts 4 instead of 3 filesystems (with different names and device files). As you noticed, it enter the error processing portion of the code, did the echo, skipped the exit, performed the next echo and on to OmniBack, etc. It's kind of hard to rerun it, but now is my brief window, will try with all suggestions.