LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 07-11-2006, 06:06 PM   #1
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 196

Rep: Reputation: 30
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?
 
Old 07-11-2006, 06:10 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
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.
 
Old 07-12-2006, 02:40 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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 ]]
 
Old 07-12-2006, 10:45 AM   #4
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 196

Original Poster
Rep: Reputation: 30
#!/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.

Last edited by beeblequix; 07-12-2006 at 10:49 AM.
 
Old 07-12-2006, 10:53 AM   #5
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
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.
 
Old 07-12-2006, 11:02 AM   #6
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 196

Original Poster
Rep: Reputation: 30
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?

Last edited by beeblequix; 07-12-2006 at 11:05 AM.
 
Old 08-09-2006, 07:11 PM   #7
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 196

Original Poster
Rep: Reputation: 30
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.

 
Old 08-09-2006, 07:50 PM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,679
Blog Entries: 54

Rep: Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955Reputation: 2955
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.
 
Old 08-10-2006, 12:12 PM   #9
beeblequix
Member
 
Registered: Oct 2005
Location: Tierra Firma, Earth
Distribution: Debian of course...
Posts: 196

Original Poster
Rep: Reputation: 30
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.

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem executing mv command inside shell script pablogosse Linux - General 7 12-16-2009 01:21 PM
Can I execute a shell script from inside a php one. prabhatsoni Linux - Software 2 05-24-2006 06:40 AM
embedded shell script from inside fortran TheBrick Programming 2 05-22-2006 09:23 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM
changing the user inside a shell script sanjith11 Programming 2 04-22-2004 08:44 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration