How to use Perl in a Shell Script

Any sample shell script with perl?

Solution:

Here is a shell script written to archive my log files older than 90-days which calls up perl.

Code :

#!/bin/sh
LOG=command_tool.log

# Script will compress and archive log files older than 90-days
  cd /usr/local/apache/logs

# Perl script to identify the date 90 days prior from today
  date=`perl <<'EOF'
  @T=localtime(time-86400 * 89);printf("%02d-%02d-%02d\n",$T[4]+1,$T[3],$T[5]+1900);
  EOF`

# Next statement will add logs 90-days old in command_archive.gz file

# To view .gz file use gunzip -c command_archive.gz
  nawk 'BEGIN {FS="\n"}{RS="#"}{ORS="#"}{if ($2 ~ /^'$date'/) print $0;}' $LOG > archive.temp
  gzip -c archive.temp >> command_archive.gz
  rm archive.temp

# Next statement will remove logs 90-days old from the command_tool.log file
  nawk 'BEGIN {FS="\n"}{RS="#"}{ORS="#"}{if ($2 !~ /^'$date'/) print $0;}' $LOG > tmp_tool.log
  perl -plne "s/LogFile.*/LogFile\n#/g" tmp_tool.log > tmp1_tool.log
  cp tmp1_tool.log $LOG
  rm tmp_tool.log
  rm tmp1_tool.log

Have a Unix Problem
Unix Forum - Do you have a UNIX Question?

Unix Books :-
UNIX Programming, Certification, System Administration, Performance Tuning Reference Books

Return to : - Unix System Administration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.
The site www.gotothings.com is in no way affiliated with or endorsed by any company listed at this site.
Any unauthorised copying or mirroring is prohibited.