Im trying to fix my /etc/weekly that rotates various logs however it does them no matter what filesize they are and i want them to only do it if there file size exceeds 2M or something. So I'm playing with a script to get the filesize using a ls -l command which works and puts the value into a variable.
So then i hope to test that variable with if -gt but it doesn't work, maybe because its the wrong data type not sure really. Here is the code I'm playing with
ls -l asl.log| awk '{printf "%s",$5}'
max=300
if [ $5 -gt $max ]; then echo "wohoo";
else echo "grr";
fi
I have checked the result of
ls -l asl.log| awk '{printf "%s",$5}'
It is greater than 300. But when i run this script the condtion is returning false and i get an error here is the output when running the script
$5 is not the value you should use to compare, that is the value in the fifth column from the output of ls -l asl.log . You want to compare the output of the entire command (ls piped through awk). So you need to store that in a variable, and compare that with $max. So you could do something like:
# These are back ticks, not single quotes.
size=`ls -l asl.log| awk '{printf "%s",$5}'`
max=300
if [ $size -gt $max ]; then echo "wohoo";
else echo "grr";
fi