Modify script to generate a log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify script to generate a log file
# 1  
Old 06-13-2006
Question Modify script to generate a log file

I've seen several examples of scripts in thise forum about having a script generate a log file. I have a script that is run from cron and that monitors a file system for a specfic filename(s) and then performs some actions on them. Normally I call this script from another script (which the one actually called from cron) that generates the log file from the output of the first script. I've found that just looking at the output of the first script is too detailed for most purposes so I want to modify it to create its own log file and then just have specific parts of the script create lines for the log. The script looks like:

Code:
#!/bin/ksh
#
# Function : is_file_arrived file
# Arg(s)   : file = file to verify
# Output   : None
# Status   : 0 = yes file arrived, 1 = no
# Env.     : IFA_WAIT : interval (secs) for file size check (def=5)
#
 
is_file_arrived() {
   [ -z "$1" ] && return 1
   local file=$1
   local arrived=1
   local size1 size2
   if [ -f "$file" -a -z "$(fuser $file 2> /dev/null)" ] ; then
      size1=$(ls -l $file 2>/dev/null | awk '{print $5}')
      sleep ${IFA_WAIT:-15}
      size2=$(ls -l $file 2>/dev/null | awk '{print $5}')
      [ ${size1:-1} -eq ${size2:-2} ] && arrived=0
   fi
   return $arrived
}
 
 
processFile ()
{
   local fileName=$1
   local fileExtension=$2
   local fileNewName="/datafiles/str${fileExtension}.asc"
   local filePrintPath="/print"
   local fileTmpPath="/tmp"
   local fileODIName="str${fileExtension}.pos"
   mv -Eignore $fileName $fileNewName      # Add status check
   prepup $fileNewName $fileExtension
   mv -Eignore  $filePrintPath/$fileODIName $fileTmpPath/$fileODIName
   save2tmp $fileExtension
   call_siu $fileExtension
}
 
# Main Processing
 
nsec=1
#while :
while [[ "$(date +%H%M)" -lt 2329 ]]
do
   for fileName in /datafiles/[Uu][Pp][Ll][Oo][Aa][Dd].[0-9][0-9][0-
9][0-9]
   do
      fileExtension=${fileName#*.}
      is_file_arrived "$fileName" && nsec=1 && processFile $fileName $fileExtens
ion
   done
   sleep $nsec
   case $nsec in
      1)   nsec=15;;
      15)  nsec=45;;
      45)  nsec=90;;
      90)  nsec=300;;
      300) nsec=600;;
      600) nsec=900;;
      *)   nsec=1800;;
   esac
done

...I searched for some type of function to call to create the lof and found this:

Code:
[ ! -f "$0.log" ] && > $0.log

function log
{
  echo "$@" >> "$0.log"
}

How would I go about using a function to create and append the log file? I want to be able to place a line like:

Code:
log "File $FILENAME found."
log "File $FILENAME renamed to $FILENEWNAME"
log "File $FILENAME processed successfully"

...etc, etc..
# 2  
Old 06-13-2006
What you have posted will work.
# 3  
Old 06-14-2006
It will work, but not quite how you would expect.

Assume that the script is called script.sh
Quote:
[ ! -f "$0.log" ] && > $0.log
Will create an empty file called script.sh.log in the current working directory as $0 is replaced by the script's file name.

Quote:
function log
{
echo "$@" >> "$0.log"
}
Will write whatever is passed to it in the file log.log in the current directory as $0 is replaced by the function name.

You could add a global variable to hold the log file name
Code:
function log
{
  echo "$@" >> "$0.log"
}
LOGFILE=$0.log
[ ! -f "$0.log" ] && > $0.log
log "something"

or pass the filename to the function
Code:
function log
{
  typeset logfile=$1
  shift
  echo "$@" >> $logfile
}
[ ! -f "$0.log" ] && > $0.log
log "$0" "something"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can i make my cron/script to generate a log filename with timestamp in it ?

Hello Friends, I would like my script to display date timestamps in the file name for every script execution. Below is the scenario: (just for testing purpose) I scheduled a cron job, lets say it runs every 5 min and record/logs output in to a log file. 0,5,10,15,20,25,30,35,40,45,50,55 *... (5 Replies)
Discussion started by: System Admin 77
5 Replies

2. UNIX for Dummies Questions & Answers

Need script to generate file.

Hi I have a file "test" with data as below 1,APRIL,NEW,"New market,delhi,pune,India",RECENT, 254664 2,MARCH,OLD,"New Area,Mumbai,UP,India",CURRENT, 152483 So I want a script which provides output as below 1,APRIL,RECENT,254664 2,MARCH,CURRENT,152483 I am aware we can use awk/sed... (9 Replies)
Discussion started by: sv0081493
9 Replies

3. Shell Programming and Scripting

How to tail -f logfile. if log file is generate every 1 HR.?

Hello, How to tail -f logfile. if log file is gennerate every 1 HR. I want it works automatically all the time. never changes it by manual. Thank ls -trl CybertonTransaction.* -rw-r--r-- 1 autobot robot 617071 Jul 9 00:02 CybertonTransaction.20130709-00.log -rw-r--r-- 1 autobot ... (12 Replies)
Discussion started by: ooilinlove
12 Replies

4. Shell Programming and Scripting

modify the test file by any script

Hi All the Helpers! I have a text file which looks like input.txt.I would request to please suggest me how can I make this file look like output.txt input.txt VOP 111 0 1 2 DEM 111 0 222 333 444 555 DEM 879 888 987 888 989 DEM 879 888 987 888 989 VOP 118 0 12 3 6... (7 Replies)
Discussion started by: Indra2011
7 Replies

5. Shell Programming and Scripting

Modify the file by script

Hi All, I have an input file like below, 6984 1225 6989 1220 6994 1214 ... (3 Replies)
Discussion started by: Indra2011
3 Replies

6. Shell Programming and Scripting

How to monitor log file for a Error and generate the e-mail ( Please help)

This is my log file and this is live log. Any abnormal error other than following I need to generate the email. Log path : /DER/app/admin/ABC/bdump/erg.log Current log# 2 seq# 103046 mem# 0: /ora2/oradata/ABC/redo02a.log Current log# 2 seq# 103046 mem# 1:... (7 Replies)
Discussion started by: hishanms
7 Replies

7. Shell Programming and Scripting

Monitor log file for a Error and generate the e-mail.

This is my log file and this is live log. Any abnormal error other than following I need to generate the email. Log path : /DER/app/admin/ABC/bdump/erg.log Current log# 2 seq# 103046 mem# 0: /ora2/oradata/ABC/redo02a.log Current log# 2 seq# 103046 mem# 1:... (1 Reply)
Discussion started by: hishanms
1 Replies

8. Shell Programming and Scripting

Generate script perl mail log not work

Dear unix forum members, I'm working on a script that will parse a mail machine's logs and print a list of email addresses in this format: authen@domain.com | sender@domain | recipient@domain exam account1@domain1.com | sender2@domain2.com |... (3 Replies)
Discussion started by: puka
3 Replies

9. Shell Programming and Scripting

Perl : how to modify a file without generate a temporary file

Hi All, I have a file like below, how can i insert one line after line 1 without using a temporary file in perl? line 1 line 2 line 3 expected result line 1 new line <---insert here line 2 line 3 (2 Replies)
Discussion started by: summer_cherry
2 Replies

10. Shell Programming and Scripting

How to modify the contents of file using script

Hi, Can anyone pls let me know how can i modify the file contents thru script. Eg. I have file abc.dat that contains below lines Merge.resync.cycleFlag Merge.resync.logFlag Merge.resync.maxByteRate Merge.resync.maxSearch Merge.resync.rate Merge.resync.tickLog ... (2 Replies)
Discussion started by: sdosanjh
2 Replies
Login or Register to Ask a Question