Need help with Uni Coursework PLEASeEEEE

Associate
Joined
11 Jul 2005
Posts
788
Location
London
Pretty much struggling with this one question and wondering if you guys can help me :(


Basically this question has me stumped, i dont even know how i should document it :(

maybe you guys can help me figure it out... Im pretty good on Linux but with it being almost 12 and me not sleeping for a few days i cant get my head round it!!!!

Question 3: UNIX Programming (This must be implemented using a UNIX System)

A Generational Log File System. Many UNIX servers maintain log files in which they write data about operations they perform and their success or failure. Log Data can then be used later on to analyse security problems, but these log files tend to grow and unless regularly truncated, can fill up the disc.

The aim of this coursework is to create a generational log (e.g. daily log) for say three days, with suffix .1, .2, .3 for example. The last copy of the log is then erased automatically.

Write a function named ‘rotate’ that takes a file name and a number of old copies to maintain as its parameters. This function will then carry out the appropriate file renaming. Log files will then be written to the logs directory.

(Suggestion: use a shorter time period than one day for demonstration purposes). Hand in the script and sample log files.
 
As far as im aware i cannot use an external program such as apache i have to "Write a function named ‘rotate’ that takes a file name and a number of old copies to maintain as its parameters. This function will then carry out the appropriate file renaming. Log files will then be written to the logs directory." So i have to write it myself, but really have no clue where to begin!!
 
Hey all, ive made some progress but im getting stuck with syntax errors at "if text.3" what i want is it to make sure the file is there before changing it. anyone got any advise??

#!/bin/bash

echo "Starting To Rotate Log Files"
cd /var/log
mkdir textlog
mv text /var/log/textlog
cd /var/log/textlog
for text in /var/log/textlog
if text.3
then mv text.3 text.4
fi
if text.2
then mv text.2 text.3
fi
if text.1
then mv text.1 text.2
fi
if text
then mv text text.1 && rm text.4
fi
done
echo "Log Files Rotated"
 
Thanks for all the advise, i finally managed to do it and get it working with yum.log, for those who may get a question like this in the future for UNI see below for my answer which i got the marks for :)

#!/bin/bash

# To use this Rotate Function i recommend running it under a daily crontab
# enter this command into /etc/crontab
# 0 0 * * * /root/rotate.sh

echo "Starting To Rotate Log Files"
cd /var/log
if [ ! -d yumlog ]
then mkdir yumlog
fi
cp yum.log /var/log/yumlog
cd /var/log/yumlog
if [ -f yum.log.3 ]
then cp yum.log.3 yum.log.4
fi
if [ -f yum.log.2 ]
then cp yum.log.2 yum.log.3
fi
if [ -f yum.log.1 ]
then cp yum.log.1 yum.log.2
fi
if [ -f yum.log ]
then mv yum.log yum.log.1
fi
if [ -f yum.log.4 ]
then rm yum.log.4
else [ ! –f yum.log.4 ]
fi
echo "Log Files Rotated"
 
Back
Top Bottom