Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents,

I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out:

for filename in $(find /var/log/test -type f -mtime +10)
do
        read -r firstLine < $filename
        echo "$firstLine"
either        echo "${firstLine:0:3}"     # "${firstLine:0:3}": bad substitution
or             echo "${cut -c1-8}"         # "${cut -c1-8}": bad substitution

        if [ "${firstLine:0:3}" = "MSH" ] || [ "${firstLine:0:3}" = "FHS" ] || [ "${firstLine:0:5}" = "<?xml" ]
        then
                echo $filename >> /var/log/test/my2.log     # Just a log file to keep track of which files got deleted
##              rm $filename
        fi
done

*****************************************************************

Please assist.
Many thanks in advance, you guys/gals are awesome!

Just guessing that you are not running a (recent) bash for your "substring expansion" ${firstLine:0:3} as it is the usual syntax and should work. ${cut -c1-8} should be a "command substitution" in the syntax $(cut -c1-8) but needs an input file to work upon.

1 Like

Come on folks, I need to wake up and so should you. The only problem was that I forget to start with #!/bin/bash, it's all. As soon as I added that, it worked like a charm.

Thank you @RudiC for replying. Your verification of the code gave me enough confidence to go and look elsewhere and find it. Thank you very much.

And thank you all for reading, thinking about replying, etc.

YOU ALL HAVE A VERY NICE WEEKEND (especially you @RudiC). YOU GUYS ARE TRULY AWESOME.