1834009 Members
4366 Online
110063 Solutions
New Discussion

script question

 
SOLVED
Go to solution
Jon Rios
New Member

script question

Hello to everyone. This is my first time on this site and I am also very new to Unix. Here is my first question. I am writing my first script and am getting a error message for line 17. My script is suppossed to copy the logfile to .date. It will the zero the file. It will then gzip the .date file. Last thing it will do is remove .date files more than 30 days old. Please let meknow what I'm doing wrong here.

OLD="14"
TODAY=$(date +%Y%m%d)

DIR="/test/test"
LOGFILE="test.log"

log_file() {

if [ -f ${DIR}/${LOGFILE} ]
then
cp -f ${DIR}/${LOGFILE} ${DIR}/${LOGFILE}.${TODAY}
cp /dev/null ${DIR/${LOGFILE}
gzip -f ${DIR}/${LOGFILE}.${TODAY}

done

print "Checking for files older than $OLD old days old."

print "Removing Files ..."

find ${DIR} -name "${LOGFILE}.*" -mtime +$OLD -exec rm -f {} \;

}
Thanks for the time and help.

10 REPLIES 10
Patrick Wallek
Honored Contributor

Re: script question

Try using echo instead of print.
Jon Rios
New Member

Re: script question

Its complaining about THEN statement, right after the if statement. Please help.
harry d brown jr
Honored Contributor

Re: script question

Jon,

Which line is line 17? And what was the error message?


live free or die
harry
Live Free or Die
Steven Sim Kok Leong
Honored Contributor
Solution

Re: script question

Hi,

Replace "done" with "fi".

if [ -f ${DIR}/${LOGFILE} ]
then
cp -f ${DIR}/${LOGFILE} ${DIR}/${LOGFILE}.${TODAY}
cp /dev/null ${DIR/${LOGFILE}
gzip -f ${DIR}/${LOGFILE}.${TODAY}
fi

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: script question

Hi,

In case you are wondering what "fi" is, the loop structure for if is:

if ... # your condition here
then
# your code here
fi

You use done in a while loop:

while ... # your condition here
do
# your code here
done

Hope this explains it out for you. Regards.

Steven Sim Kok Leong
Jon Rios
New Member

Re: script question

Now I'm more confused then before. My syntax error is fixed now. I am able to execute my script. Bu t when I go and look in my directory for a test.log.(date)O file, its not there. I even just tried the cp -p line in my script but its not doing anything. What is it that I have done wrong in my script. Thanks for your help and sorry for bugging anyone.
Steven Sim Kok Leong
Honored Contributor

Re: script question

Hi,

There are a few issues with this script:

1) log_file function is not called anywhere in the script. Preferably, it should be called after the function definition.

2) There is a typo in your cp statement which explains why /test/test/test.log was not created. There is a missing closing parenthesis.

>> cp /dev/null ${DIR/${LOGFILE}

It should be

cp /dev/null ${DIR}/${LOGFILE}

3) The gzip parameters are not correct. If you want your script to backup directories and files and keep the output log and errors during the archiving,

Instead of

>> gzip -f ${DIR}/${LOGFILE}.${TODAY}

It should be

gzip -f backup.gz list_of_dirs_and_files > ${DIR}/${LOGFILE} 2>&1

The backup log will be /test/test/test.log

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: script question

Hi,

Btw, to call your log_file function, simply insert at the bottom of your script:

log_file

Hope this helps. Regards.

Steven Sim Kok Leong
Darrell Allen
Honored Contributor

Re: script question

Hi Jon,

You don't have to define your code as a function. As Steven points out, a function must be called, it is not executed simply because it is defined.

So, if this is simply a script you want to run from time to time, keep it simple by deleting the funciton definition lines:
log_file{} {
}

You wouldn't have the need for a statement to execute the code then.

If you do intend to add more code to the script and want to call log_file multiple times, then you have done well to code the function.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Steven Sim Kok Leong
Honored Contributor

Re: script question

Hi,

I need to make one correction to my posting above regarding your gzip statement. gzip is used for compressing individual files and not for archiving. If you are specifically talking about archiving (tar) and compressing the tar file (gzip) later, then you should use a combination of tar and gzip:

tar cvf - /home/* | gzip > /home/home.tar.gz > ${DIR}/${LOGFILE} 2>&1

Hope this helps. Regards.

Steven Sim Kok Leong