The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
tcpdump and connection troubleshooting melanie_pfefer Linux 4 04-10-2008 06:17 AM
tftp troubleshooting Bobby76 UNIX for Dummies Questions & Answers 0 02-29-2008 05:44 AM
Troubleshooting Samba on Solaris 8 davisr65 SUN Solaris 0 03-06-2005 09:13 AM
Networking troubleshooting manwithaphone UNIX for Advanced & Expert Users 1 05-01-2004 05:12 AM
Ultra10 ethernet troubleshooting xyyz UNIX for Dummies Questions & Answers 5 03-11-2002 03:25 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-04-2005
Registered User
 

Join Date: Mar 2005
Posts: 14
Stumble this Post!
Troubleshooting Script

I am fairly new to Unix (Sun OS) scripting with Ksh, and I could use a little help troubleshooting my script. When I run it, I get the error "if unexpected" on the function setYear. If I comment out the setYear function, I get the error "for unexpected" on the function sendEmails. This leads me to believe that the error lies above the two functions, but I cannot find it. I am hoping that one of you might be able to point me in the right direction. Thanks in advance for the help.

Code:
#!/bin/ksh

# Author:    Mark Harley, ISDC Halifax
# Contact:   m.harley@cgi.com
# Created:   01 March 2005
# Updated:   04 March 2005
# Purpose:   This is a test of the automated monthly eBCCS log file backup.
#            The actual copies of this file are on the following servers:  

dmocnu.on.bell.ca, dmocnt.on.bell.ca, dc1c3m.qc.bell.ca

# The following files need to be backed up each month (usually on the 7th):
# Nohup.out
# PHLog.txt
# PSQTLog.txt
# Response-Log.txt
# Access.log
# Weblogic.log
# ServiceAddressQueryResponseLog.txt

# this sets the email addresses that are sent the temp file containing the command outputs
email_addresses="tina.naugler@cgi.com m.harley@cgi.com pamela.green@cgi.com"

# this puts the instance names in an array
set -A instance
instance[1]="ont0"
instance[2]="qbc1"
instance[3]="jdbc0"
instance[4]="admin1"

# this puts the file names in an array
set -A fName
fName[1]="nohup.out"
fName[2]="PHLog.txt"
fName[3]="PSQTlog.txt"
fName[4]="response-Log.txt"
fName[5]="access.log"
fName[6]="wl-domain.log"
fName[7]="weblogic.log"
fName[8]="ServiceAddressQueryResponse.Log.txt"

# these variables will contain the system date to append to the file name
# the DATEday variable can be altered for mid-month backups (but it MUST be reset to the 7th 

after backup completed)
integer DATEday=7
integer DATEmonth=$(( \( "`date +%m`" + 12 - 1 \) % 12 ))
integer DATEyear="`date +%y`"

# filler variables
zero="0"
star="*"
underscore="_"

# if the current month is January, then the backup will be for December of the previous year

function setYear{
  if [ ${DATEmonth} == 1 ]
  then
    DATEyear=${DATEyear}-1
  fi
}

# send the temp file in the body of an email to the email addresses specified above

function sendEmails{
  for email_address in $email_addresses
  do
    /bin/mail -s "${DATEday}-${DATEmonth}-${DATEyear} Log File Backup" $email_address < 

/tmp/ebccsbkup.tmp
  done
}

# backup files and compress them, then move them to the backup directory for the instance
# the results of the executed commands is written to a temp file

function backupFiles{
  for ((y=0; y<=${#fName[*]}; y++))
  do
    if [ ${fName[y]} = "access.log" ] || [ ${fName[y]} = "weblogic.log" ]; then
      if [ -f "access.log" ] || [ -f "weblogic.log" ]; then
       tar cvf bk${fName[y]}.${DATEyear}${DATEmonth}.tar ${fName[y]}.${zero}${star} >> 

/tmp/ebccsbkup.tmp
#      rm ${fName[y]}${zero}${star}
       mv bk${fName[y]}.${DATEyear}$DATEmonth.tar 

$fName[y].${DATEyear}${DATEmonth}${underscore}${DATEday}.tar >> /tmp/ebccsbkup.tmp
       gzip ${fName[y]}.${DATEyear}${DATEmonth}${underscore}${DATEday}.tar >> 

/tmp/ebccsbkup.tmp
      else
        echo "${fName[y]} does not exist in ${instance[x]}" >> /tmp/ebccsbkup.tmp
      fi
    elif [${fName[y]} = "wl-domain.log"]; then
      if [ -f "wl-domain.log" ]; then
        tar cvf ${fName[y]}.${DATEyear}${DATEmonth}${underscore}${DATEday}.tar ${fName[y]} 

>> /tmp/ebccsbkup.tmp
#       cp /dev/null/$fName[y] >> /tmp/ebccsbkup.tmp
        gzip ${fName[y]}.${DATEyear}${DATEmonth}${underscore}${DATEday}.tar >> 

/tmp/ebccsbkup.tmp
      else
        echo "${fName[y]} does not exist in ${instance[x]}" >> /tmp/ebccsbkup.tmp
      fi
    else
      if [ -f $fName[y]} ]; then
        tar cvf bk${fName[y]}.${DATEyear}$DATEmonth.tar 

${fName[y]}.${DATEyear}$DATEmonth${star} >> /tmp/ebccsbkup.tmp
#       rm ${fName[y]}.${DATEyear}${DATEmonth}${star}
       mv bk${fName[y]}.${DATEyear}$DATEmonth.tar 

${fName[y]}.${DATEyear}${DATEmonth}${underscore}${DATEday}.tar >> /tmp/ebccsbkup.
tmp
        gzip ${fName[y]}.${DATEyear}${DATEmonth}${underscore}${DATEday}.tar >> 

/tmp/ebccsbkup.tmp
      else
        echo "${fName[y]} does not exist in ${instance[x]}" >> /tmp/ebccsbkup.tmp
      fi
    fi
    echo "\n" >> /tmp/ebccsbkup.tmp
  done
  mv *.tar.gz /u01/${instance[x]}/ >> /tmp/ebccsbkup.tmp
}


# loop through the instances and call function backupFiles

function setInstance{

  # this outputs an initial statement to the temp file allowing all subsequent outputs to be 

rewrite statements
  echo "eBCCS Monthly Log Backup ${DATEday}-${DATEmonth}-${DATEyear}" > /tmp/ebccsbkup.temp

  for ((x=0; x<=${#instance[*]}; x++))
  do
    echo "${instance[x]} testing..."
    echo "\n" >> /tmp/ebccsbkup.tmp
    echo "${instance[x]} Log Files" >> /tmp/ebccsbkup.tmp
    cd /opt/bea/${instance[x]}/wls6.1/config/bles/logs
    backupFiles
  done

  sendEmails
}

setYear
setInstance
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-04-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
Stumble this Post!
a few comments:

1. this
Code:
set -A instance
instance[1]="ont0"
instance[2]="qbc1"
instance[3]="jdbc0"
instance[4]="admin1"
can be made with that:
Code:
set -A instance 'ont0' 'qbc1' 'jdbc0' 'admin1'
the same goes for the OTHER array declaration/initialization.
btw, arrays are ZERO-based - not ONE-based

2.
Code:
function setYear{
  if [ ${DATEmonth} == 1 ]
  then
    DATEyear=${DATEyear}-1
  fi
}
DATEyear is declared as integer and should be compared as such.
Code:
if (( DATEmonth == 1 ))
and substraction should be performed as such:
Code:
(( DATEyear = DATEyear -1 ))
3. for your actual error.... - you need either a SPACE between the function name the trailing '{' OR you need to put '{' on a new line:
Code:
function setYear {

OR

function setYear
{
there are other potential problems with the script, but we'll go slow - fix the ones above first and start debugging the output next.

good luck.
Reply With Quote
  #3 (permalink)  
Old 03-04-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,444
Stumble this Post!
There is another problem that needs to be addressed now, and that is code like:
for ((y=0; y<=${#fName[*]}; y++))
You don't say which version of Solaris you are using. As of Solaris 9, /bin/ksh is based on ksh88 and it does not support the arithmetic for statement.

You could just change that to a arithmetic while. This means moving the "y=0" to just before the loop and embedding ((y=y+1)) inside the loop. (no y++ either) This is what I would do.

There is a second option. Solaris comes with dtksh which is in /usr/dt/bin/dtksh. You could switch to that. dtksh is based on ksh93 and it has the more advanced arithemetic constructs. It is heavily extended from ksh since you can write x clients in dtksh. This slows it down somewhat.

But you need to do something here. You are trying to run ksh93 code on a ksh88 intrepreter.
Reply With Quote
  #4 (permalink)  
Old 03-04-2005
Registered User
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
Stumble this Post!
To simply start to debug then execute that ksh script in debug mode as,

ksh -x <script.ksh>

it will give appropriate location where it is getting problem.

You can not work for loop as lik fore ((y=0; y<=${#fName[*]}; y++)) in shell script so that you can use while instead of it as,

y=0
while [[ $y -le ${#fName[*]} ]]
do
action;;
let y=y+1
done

It will do that trick.

HTH.
Reply With Quote
  #5 (permalink)  
Old 03-04-2005
Registered User
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
Stumble this Post!
I tried to edit the script (without testing) as,
Code:
#!/bin/ksh

# Variables
email_addresses="tina.naugler@cgi.com m.harley@cgi.com pamela.green@cgi.com"
set -A instance "ont0" "qbc1" "jdbc0" "admin1"
set -A fName "nohup.out" "PHLog.txt" "PSQTlog.txt" "response-Log.txt" "access.log" "wl-domain.log" "weblogic.log" "ServiceAddressQueryResponse.Log.txt"

# these variables will contain the system date to append to the file name
# the DATEday variable can be altered for mid-month backups (but it MUST be reset to the 7th 
after backup completed)
DATEday=7
DATEmonth=$(( \( "`date +%m`" + 12 - 1 \) % 12 ))
DATEyear="`date +%y`"

# filler variables
zero="0"
star="*"
underscore="_"

# if the current month is January, then the backup will be for December of the previous year
function setYear {
  if [ ${DATEmonth} -eq 1 ]
  then
    let DATEyear=DATEyear-1
  fi
}

# send the temp file in the body of an email to the email addresses specified above
function sendEmails {
  
  index=0
  while [[ $index -lt ${#email_addresses[*]} ]]
  do
  
    let email_address=${email_addresses[$index]}
    /bin/mail -s "${DATEday}-${DATEmonth}-${DATEyear} Log File Backup" $email_address < /tmp/ebccsbkup.tmp
    let index=index+1
    
  done
}
# backup files and compress them, then move them to the backup directory for the instance
# the results of the executed commands is written to a temp file
function backupFiles {
	
  let y=0;
  while [[ $y -lt ${#fName[*] ]] 
  do
    if [ ${fName[y]} = "access.log" ] || [ ${fName[y]} = "weblogic.log" ]; then
      if [ -f "access.log" ] || [ -f "weblogic.log" ]; then
        tar cvf bk${fName[y]}.${DATEyear}${DATEmonth}.tar ${fName[y]}.${zero}${star} >> /tmp/ebccsbkup.tmp
        mv bk${fName[y]}.${DATEyear}$DATEmonth.tar $fName[y].${DATEyear}${DATEmonth}${underscore}${DATEday}.tar >> /tmp/ebccsbkup.tmp
        gzip ${fName[y]}.${DATEyear}${DATEmonth}${underscore}${DATEday}.tar >> /tmp/ebccsbkup.tmp
      else
        echo "${fName[y]} does not exist in ${instance[x]}" >> /tmp/ebccsbkup.tmp
      fi
    elif [${fName[y]} = "wl-domain.log"]; then
      if [ -f "wl-domain.log" ]; then
        tar cvf ${fName[y]}.${DATEyear}${DATEmonth}${underscore}${DATEday}.tar ${fName[y]} >> /tmp/ebccsbkup.tmp
        gzip ${fName[y]}.${DATEyear}${DATEmonth}${underscore}${DATEday}.tar >> /tmp/ebccsbkup.tmp
      else
        echo "${fName[y]} does not exist in ${instance[x]}" >> /tmp/ebccsbkup.tmp
      fi
    else
      if [ -f $fName[y]} ]; then
        tar cvf bk${fName[y]}.${DATEyear}$DATEmonth.tar ${fName[y]}.${DATEyear}$DATEmonth${star} >> /tmp/ebccsbkup.tmp
        mv bk${fName[y]}.${DATEyear}$DATEmonth.tar ${fName[y]}.${DATEyear}${DATEmonth}${underscore}${DATEday}.tar >> /tmp/ebccsbkup.tmp
        gzip ${fName[y]}.${DATEyear}${DATEmonth}${underscore}${DATEday}.tar >> /tmp/ebccsbkup.tmp
      else
        echo "${fName[y]} does not exist in ${instance[x]}" >> /tmp/ebccsbkup.tmp
      fi
    fi
    echo "\n" >> /tmp/ebccsbkup.tmp
    let y=y+1
    
  done

  mv *.tar.gz /u01/${instance[x]}/ >> /tmp/ebccsbkup.tmp
}

# loop through the instances and call function backupFiles
function setInstance {
  echo "eBCCS Monthly Log Backup ${DATEday}-${DATEmonth}-${DATEyear}" > /tmp/ebccsbkup.temp
  let x=0
  while [[ $x -lt ${#instance[*]} ]]
  do
    echo "${instance[x]} testing..."
    echo "\n" >> /tmp/ebccsbkup.tmp
    echo "${instance[x]} Log Files" >> /tmp/ebccsbkup.tmp
    cd /opt/bea/${instance[x]}/wls6.1/config/bles/logs
    backupFiles
    let x=x+1
  done
  
  sendEmails
}

setYear
setInstance

exit 0
# END ###
try this. let us know if u have problem.
Reply With Quote
  #6 (permalink)  
Old 03-07-2005
Registered User
 

Join Date: Mar 2005
Posts: 14
Stumble this Post!
Update

Ok, thanks to all of your help, I have solved many of the problems I was having. I have encountered a new problem, however, that needs dealing with. Is it possible to verify the existance of files using a wildcard? For example, if [ -f "access.log${zero}${star}" ] (where zero="0" and star="*")? In this example, I want to find all files that bear the name "access.log0...". The original file, "access.log" needs to be ignored by the script, and only the files with the numeric suffix need to be tar'd and moved. I have attempted to run it with this syntax, and it appears that none of the files are detected. Is this another syntax problem? Thanks for all the help.
Reply With Quote
  #7 (permalink)  
Old 03-07-2005
Registered User
 

Join Date: Feb 2005
Location: Coimbatore, Tamilnadu, India
Posts: 119
Stumble this Post!
-f test option is used to check only the existance of one file. Not more file as, access.log0* where start is used to 0-n. But you have to do as,

Code:
for file in `ls access.logo*`
do
  action;
done
It will work now.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0