THE LAST few lines I'm not sure of. Which will work?
1)....
exit $?
cp blah_blah_1.log blah_blah.$NOW.log
fi

OR

2)....
cp blah_blah_1.log blah_blah.$NOW.log
exit $?
fi

The idea will be that the "exit $?" statement is included in the .log. Having stated that my instinct is to used #2, however, I'm not really sure if "exit $?" will terminate the script at that point and not even look for "fi".

Ideas?

macemoneta

07-11-2006 05:10 PM

You need a modified #2. The return code in $? will be changed by any commands that you issue, so you need to save the value at the point it's significant, then exit with the saved value.

---
db2 blah
RC=$?
.
.
.
exit $RC
---

Any commands you place after the exit will NOT be executed, because the script will be terminated by the exit command.

chrism01

07-12-2006 01:40 AM

Actually, dpending on how you are 'capturing the output, you'll prob need to
echo $RC
then
exit $RC
as exit will exit the prog, but does not write the rtn code anywhere, except in the calling shell's $? var.
Also, where you've got '{' and '(',')', use '[[' and ']]'
and use
if [[ $? -eq 0 ]]

How does that look? What does "[[" and "]]" mean compared to "(" and ")"? Thanks a bunch for your help gents.

macemoneta

07-12-2006 09:53 AM

You only set the RC variable at the top, so it contains the return code at that point, which is likely zero. You need to set the vaiable at the point you want to preserve the return code from the command executed.

And my understanding of "$?" is the return code for the previous command. Will using the RC=$? method allow the RC to have a set value at any point that it's declared and afterwards be separate from $? ? The reason I ask is because to me it seems that RC at that point should hold that value but still let $? return values based off the 'last command' which means that the $? value in my script will be affected by my cp command. Does that makes sense?

beeblequix

08-09-2006 06:11 PM

Thx for the tips. I don't know that I'm going to use the return code value now since I don't want to have to code all possible return values, but I have more questions. Hope someone can help me understand this.

Notice my script and how I want to test a few levels deep. Can a /bin/sh script even do that? I'm trying to employ a little error correction, reattempts, etc in a sh script.

In the following example I'd like to connect to the database. IF it cannot connect the first time I'd like it to try again. IF it fails on the second attempt I'd like it to rename an output file, email a few of us and exit the script. I'm having trouble with the logic or even knowing if what I want it to do is possible with Bourne shell.

#!/bin/sh
# this is one chunk of the script (nothing uber-sensative)

# the following is me having fun with variables...;)
EMAIL1=me@mycompany.com #my email
EMAIl2=you@yourcompany.com #email of someone
DB_NM=The_Rings_of_Wrath_and_Khan #the name of the database
USRNM1=ralph.nader #username login credentials
PZWD1=money_is_green_too #$USRNM1's password

Can a /bin/sh script do this? Can I embed tests that deep? Do I have to put two 'fis' at the bottom? Is that exit statement near the end necessary? Help appreciated.

ß

unSpawn

08-09-2006 06:50 PM

I don't know that I'm going to use the return code value now since I don't want to have to code all possible return values
You could focus on the OK exit status and fill in the rest later. For example:

Code:

/bin/false; case "$?" in 0) echo OK;; *) ;; esac

Notice my script and how I want to test a few levels deep. Can a /bin/sh script even do that?
Sure:

I'm trying to employ a little error correction, reattempts, etc in a sh script.
That's what exit codes are for...

I'm having trouble with the logic
Eh?

or even knowing if what I want it to do is possible with Bourne shell.
If something is impossible you'll find out soon enough. BTW, what you want, what you really really want, is a copy of the ABS or Advanced Bash Scripting guide.

beeblequix

08-10-2006 11:12 AM

Wow. Very nice. I had to 'man test' to understand that "-lt" means less than, and I'll make my variables a little more descript, but thanks a ton. :) :) :)