Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell Script Issues

Status
Not open for further replies.

Tim2525

Technical User
Feb 7, 2005
51
US
Hi All,

Attempting to write my first shell script. Getting an error telling me on line "echo Started back up on `date` >> /db2_backup/timer.txt" my script can't create the file. I'm also getting an unmatched "if". But I think there is something else going on.

Can one of you gurus take a quick look and give any pointers/suggestions. Would be most appreciated.


echo Started back up on `date` >> /db2_backup/timer.txt

#set -x
dbname=campmor5
if [ "$dbname" = "" ] then
echo "Usage : " $0 "<dbname>"
exit
endif

listcmd="db2 list applications for database $dbname"

while true do
$listcmd
if [ $? -eq 2 ] then
db2 deactivate database $dbname
exit
endif
$listcmd | tail +5 | nawk '{print $3}' | while read applid
do
if [ "$applid" != "" ] then
db2 "force application($applid)"
endif
#done

#Snoozing for 5 seconds...
sleep 5

#Backing up DB...
db2 backup database campmor5 to /export/home/db2inst1/db2_backups

##SSH file to George..
rsync -a -e ssh -v /export/home/db2inst1/db2_backup db2inst1@enolagay:/export/home/db2inst1/db2_backup

#Checking for successful file transfer...
if ! rsync -a -e ssh -v /export/home/db2inst1/db2_backup db2inst1@enolagay:/export/home/db2inst1/db2_backup then
echo "CAMPMOR DATABASE BACKUP FAILED" >> /db2_backup/timer.txt
print "CAMPMOR DATABASE BACK UP FAILED"
print "CHECK /EXPORT/HOME/DB2INST1/BACKUPCAMPMOR FOR BACKUP EXISTENCE."
exit 1
endif

echo Back up completed on `date` >> /db2_backup/timer.txt
echo --------------------------------------------------- >> /db2_backup/timer.txt
exit


TIA,
T
 
ksh but thinking of switching it to bin/sh

TIA,
T
 
I think the first line:

echo Started back up on `date` >> /db2_backup/timer.txt

should be:

echo Started back up on `date` > /db2_backup/timer.txt

 
Thanks comtec17.
Still have the same issue. cannot create file...
 
ksh if statements:
Code:
if [ $? -eq 2 ] then
         db2 deactivate database $dbname
         exit
      [b]fi[/b]

not endif... I think that is csh.

You can use >> ... that mean append and it will create the file if it doesn't already exist. > will send stdout to the file and overwrite if it does exist. Does the /db2_backup directory exist? And if so, do you have permission to write to the directory?
 
1) For every [tt]endif[/tt], change it to [tt]fi[/tt]. For example...
Code:
if [ "$dbname" = "" ] then
   echo "Usage : " $0 "<dbname>"
   exit
fi

2) Make sure you have permission to write to the [tt]/db2_backup[/tt] directory.

3) Add this as the first line of your script...
Code:
#!/bin/ksh
4) Every [tt]do[/tt] loop needs to be terminated with a [tt]done[/tt]. That would make this part look like...
Code:
while true
do
   $listcmd
   if [ $? -eq 2 ] then
      db2 deactivate database $dbname
      exit
   endif
   $listcmd | tail +5 | nawk '{print $3}' | while read applid
   do
      if [ "$applid" != "" ]
      then
         db2 "force application($applid)"
      fi
   done
done
There may be other things needed, this was just a quick look at it.

Hope this helps.
 
Thanks much SamBones. Will make the changes.
 
Okay. Made recommended mods. I'm now getting unexpected 'fi at bolded area. Here is the latest...


#!/usr/bin/ksh

#echo Started back up on `date` > /db2_backup/timer.txt

#set -x
dbname=campmor5
if [ "$dbname" = "" ] then
echo "Usage : " $0 "<dbname>"
exit
fi

listcmd="db2 list applications for database $dbname"

while true
do
$listcmd
if [ $? -eq 1 ] then
db2 deactivate database $dbname
exit
fi
$listcmd | tail +5 | nawk '{print $3}' | while read applid
do

if [ "$applid" != "" ]
then
db2 "force application($applid)"
fi

done
done

#Snoozing for 5 seconds...
sleep 5

#Backing up DB...
db2 backup database campmor5 to /export/home/db2inst1/db2_backups

##SSH file to George..
rsync -a -e ssh -v /export/home/db2inst1/db2_backup db2inst1@enolagay:/export/home/db2inst1/db2_backup

#Checking for successful file transfer...
if ! rsync -a -e ssh -v /export/home/db2inst1/db2_backup db2inst1@enolagay:/export/home/db2inst1/db2_backup then
echo "CAMPMOR DATABASE BACKUP FAILED" >> /db2_backup/timer.txt
print "CAMPMOR DATABASE BACK UP FAILED"
print "CHECK /EXPORT/HOME/DB2INST1/BACKUPCAMPMOR FOR BACKUP EXISTENCE."
exit 1
endif

echo Back up completed on `date` >> /db2_backup/timer.txt
echo --------------------------------------------------- >> /db2_backup/timer.txt
exit


TIA,
T
 
I do have a permission issue writing out to timer.txt. Talking with Admin now. Txs.
 
I found the last if/fi statement needing change.
 
You have a couple of ifs and thens on the same line, e.g.

[tt]if [ "$dbname" = "" ] then[/tt]

You need to change that to

[tt]if [ "$dbname" = "" ]; then[/tt]

or

[tt]if [ "$dbname" = "" ]
then[/tt]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top