From: laxman on
Hi everyone,
I am writing a small script for log files
whenever the size of a file exceeds it has to compress the file or
split it into to file
i want to know how to to define the size of file in scripting
i am tring in the following way

logtest=/var/log/testmessages
4 logsize="ls -lh /var/log/testmessages"
5 if [ $logsize -le 10K ]
6then
7 compress $logtest
8 else
9 exit
10 fi
here it is throughing an error message test.sh: line 5: [: too many
arguments
finally i requirment is i have a 1 gb of log file which increasing
everytime and all the log files are generating in the file for example
file is stdout.log i want rotationally remove the old logs from the
file how can i do it please help and i have gone through man page of
logrotate i am unable to understand it .....please help in this it is
very important for me......if any mistakes are in the above script
please forgive me

Thanks in Advance
From: steven_nospam at Yahoo! Canada on
On May 7, 11:12 am, laxman <alakshman...(a)gmail.com> wrote:
> Hi everyone,
> I am writing a small script for log files
> whenever the size of a file exceeds it has to compress the file or
> split it into to file
> i want to know how to to define the size of file in scripting
> i am tring in the following way
>
> logtest=/var/log/testmessages
> 4 logsize="ls -lh /var/log/testmessages"
> 5 if [ $logsize -le 10K ]
> 6then
> 7 compress $logtest
> 8 else
> 9 exit
> 10 fi
> here it is throughing an error message test.sh: line 5: [: too many
> arguments
> finally i requirment is i have a 1 gb of log file which increasing
> everytime and all the log files are generating in the file for example
> file is stdout.log i want rotationally remove the old logs from the
> file how can i do it please help and i have gone through man page of
> logrotate i am unable to understand it .....please help in this it is
> very important for me......if any mistakes are in the above script
> please forgive me
>
> Thanks in Advance

Your "ls" command is getting more than just the logsize, it is
porbably getting the filename as well. Try it like this:

#!/bin/ksh
LOGFNAME=/var/log/testmessages
integer FSIZE=$(ls -l ${LOGFNAME}|awk '{ print $5 }')
if test ${FSIZE} -gt 10000
then
echo " File is over 10K bytes"
else
echo "File is less than 10K"
fi




From: steven_nospam at Yahoo! Canada on
On May 7, 11:12 am, laxman <alakshman...(a)gmail.com> wrote:

> finally i requirment is i have a 1 gb of log file which increasing
> everytime and all the log files are generating in the file for example
> file is stdout.log i want rotationally remove the old logs from the
> file how can i do it please help and i have gone through man page of
> logrotate i am unable to understand it .....

For your second issue, if I understand correctly, you have one big log
file and want to keep just the most recent entries in the log, and
drop the beginning of the log. What I normally do here is just rename
the old file, for example:

/bin/mv /var/log/testmessages.log /var/log/testmessages.log.old
touch /var/log/testmessages.log
chmod a+rw /var/log/testmessages.log

But that does not do exactly what you want. Instead, something you can
do is the following:

#!/bin/ksh
#
# This will keep the last 4096 bytes of a log file by using the dd
command to skip
# all the characters until you get to (size of file) less the value in
keepbytes
#
integer KEEPBYTES=4096
LOGFNAME=/var/log/testmessages.log
dd if=${LOGFNAME} of=${LOGFNAME}.tmp bs=1 skip=$(expr $(cat
license.log|wc -c) - ${KEEPBYTES})
/bin/mv ${LOGFNAME}.tmp ${LOGFNAME}
From: Stephane CHAZELAS on
2008-05-7, 08:12(-07), laxman:
> Hi everyone,
> I am writing a small script for log files
> whenever the size of a file exceeds it has to compress the file or
> split it into to file
> i want to know how to to define the size of file in scripting
> i am tring in the following way
>
> logtest=/var/log/testmessages
> 4 logsize="ls -lh /var/log/testmessages"

"h" is for "human", not machine.

logsize=$(wc -c < /var/log/testmessages)

--
St�phane
From: mo on
On Wed, 07 May 2008 12:12:41 -0300, laxman <alakshmanrao(a)gmail.com> wrote:

> Hi everyone,
> I am writing a small script for log files
> whenever the size of a file exceeds it has to compress the file or
> split it into to file
> i want to know how to to define the size of file in scripting
> i am tring in the following way
>
> logtest=/var/log/testmessages
> 4 logsize="ls -lh /var/log/testmessages"
> 5 if [ $logsize -le 10K ]
> 6then
> 7 compress $logtest
> 8 else
> 9 exit
> 10 fi
> here it is throughing an error message test.sh: line 5: [: too many
> arguments
> finally i requirment is i have a 1 gb of log file which increasing
> everytime and all the log files are generating in the file for example
> file is stdout.log i want rotationally remove the old logs from the
> file how can i do it please help and i have gone through man page of
> logrotate i am unable to understand it .....please help in this it is
> very important for me......if any mistakes are in the above script
> please forgive me
>
> Thanks in Advance


I tink this is ok with linux.

For file size (don't use human formats, use size in bytes):
ls -l file|tr -s ' ' |cut -d ' ' -f 5
set -- `ls -l file`;echo $5
find file -printf "%s\n"
ls -l file|awk '{print $5}'
du -hb file
ls -hs file
stat -c %s file

I don't know about logrotate.
Some simple ideas to mantain a log (txt file) with lines or date limit and
daily, weekly or manual execution.
In an apropriate (static) moment:
tail -n99999 file >log.tmp # or
grep -m1 -A99999 "some pre`date -d '90days ago' +%x`suf" file >log.tmp
#or ...
After,
mv log.tmp file