10-05-04 10:58 PM
On 2004-10-05, DrTebi wrote:
> Hello,
> I am sure there is a simple solution to this, but so far I couldn't figure
> it out:
>
> I have something like this in my /var/log/apache2 directory:
> domainx.com_log.2004-09-11
> domainx.com_log.2004-09-18
> domainx.com_log.2004-09-25
> error_log.2004-09-11
> error_log.2004-09-18
> error_log.2004-09-25
> error_log-ssl.2004-09-11
> error_log-ssl.2004-09-18
> error_log-ssl.2004-09-25
> example2.com_log.2004-09-11
> example2.com_log.2004-09-18
> example2.com_log.2004-09-25
>
> Now, inside a bash script, I want to get the contents of the latest
> error_log.xxxx-xx-xx file into a variable and mail it out.
> How would I go about it?
>
> I have tried
> ls -t -1 | grep "error_log\." | line
> ... and got
> error_log.2004-09-30
> ... but I cannot use it for e.g. cat. I believe it has
> to do the the end-of-line character:
> cat `ls -t -1 | grep "error_log\." | line`
> ... outputs
> cat: error_log.2004-09-25: No such file or directory
Always break a problem into its constituent parts.
Your first task is to get the name of the most recent error_log
file. You can either use:
file=`ls -t error_log.* | tail -1`
or, since the files are sensibly datestamped, just use the shell:
set -- error_log.*
while [ $# -gt 9 ] ## this loop is only necessary in a Bourne shell
do ## Newer shells can reference more than
shift 9 ## 9 positional parameters.
done
eval "file=\"\${$#}\""
Then if you want to store the contents of the file in a variable:
log=`cat "$file"`
But you don't need to do that to mail it:
mail -s "$file" xxx@yyy.xxx < "$file"
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
[ Post a follow-up to this message ]
|