LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   where to place certiain commands inside shell script (http://www.linuxquestions.org/questions/linux-newbie-8/where-to-place-certiain-commands-inside-shell-script-463078/)

beeblequix 07-11-2006 05:06 PM

where to place certiain commands inside shell script
 
here's part of the script:

#!/bin/sh
NOW='date %Y%m%d%H%M%s'
...
# ******************************************************
# dropping a table from database
# ******************************************************
db2 drop table blah_blah_1
if { "$?" = "0" )
then echo "Dropped blah_blah_1 table"
elif then echo "Could not drop...retrying..."
sleep 1
db2 drop table blah_blah_1
if ( "$?" = "0" )
then echo "Dropped blah_blah_1 on 2nd try"
elif then echo "Could not drop blah_blah_1. Please rerun script."
echo "section failure, retry script" | mail -s "section failure" my@mycompany.com other_people@mycompany.com


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 ]]

beeblequix 07-12-2006 09:45 AM

#!/bin/sh
NOW='date %Y%m%d%H%M%s'
RC=$?
PATH= /some/explicit/path
EMAIL1= me@mycompany.com
EMAIL2= otherperson@mycompany.com
# ******************************************************
# dropping a table from database
# ******************************************************
db2 drop table blah_blah_1
if [[ "$?" = "0" ]]
then echo "Dropped blah_blah_1 table"
elif then echo "Could not drop...retrying..."
sleep 1
db2 drop table blah_blah_1
if [[ "$?" = "0" ]]
then echo "Dropped blah_blah_1 on 2nd try"
elif then echo "Could not drop blah_blah_1. Please rerun script."
echo "section failure, retry script" | mail -s "section failure" $EMAIL1 $EMAIL2
echo $RC
cp blah_blah_1.log blah_blah.$NOW.log
exit $RC
fi


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.

beeblequix 07-12-2006 10:02 AM

so how about this?
(remove the earlier declaration)
...
elif then echo "Could not drop blah_blah_1. Please rerun script."
echo "section failure, retry script" | mail -s "section failure" $EMAIL1 $EMAIL2
RC=$?
echo $RC
cp blah_blah_1.log blah_blah.$NOW.log
exit $RC
fi

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

#*******************************
# connect to database
#*******************************
db2 connect to $DB_NAME user $USRNM1 using $PZWD1
if [ "$?" = "0" ] then echo "Connected to database"
elif [ "$?" != "0" ] then echo "retrying connection..."
if [ "$?" = "0" ] then echo "Connect to database on 2nd try"
elif [ "$?" != "0" ] then echo "Could not connect to $DB_NAME"|mail -s "non-critical section failure CONNECT TO DATABASE" $EMAIL1 -c$EMAIL2
NOW=`date %Y%m%d%H%M%s` #note those are 'ticks' or shift-tilde, not single quotes
cp output.file output.{$NOW}.file
exit
fi
fi

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:
Code:

doConnect() { ((e++)); echo "retry $e"; /bin/false || { [ "$e" -lt "$max" ] && doConnect; }; }
e=0; max=10; doConnect

or in less terse format and expanded a bit:
Code:

function doConnect() {
 e=$(expr $e + 1)
 echo "retry $e"
 /bin/false
 if [ "$?" != "0" ]; then
  if [ "$e" -lt "$max" ]; then
    doConnect
  else
    echo reached $max
  fi
 else
  echo OK
 fi
}


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. :) :) :)



All times are GMT -5. The time now is 03:48 AM.