Unix Shell - Simple problem using ls output

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2004 > Simple problem using ls output





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Simple problem using ls output
DrTebi

2004-10-05, 5:58 pm

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

Any ideas? I also tried awk, but still, I cannot seem to make the
result a valid input for cat.

Thanks,
DrTebi




Seb

2004-10-05, 5:58 pm

On 2004-10-05, DrTebi <DrTebi@yahoo.com> wrote:
> 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


Your 'ls' is probably an alias or a function; try:

cat `/bin/ls -t "error_log.*" | line`

--Seb
DrTebi

2004-10-05, 5:58 pm

On Tue, 05 Oct 2004 18:29:40 +0000, Seb wrote:

> On 2004-10-05, DrTebi <DrTebi@yahoo.com> wrote:
>
> Your 'ls' is probably an alias or a function; try:
>
> cat `/bin/ls -t "error_log.*" | line`
>
> --Seb


That was exactly right, thank you. My ls was aliased as ls --color.
DrTebi

Chris F.A. Johnson

2004-10-05, 5: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
Janis Papanagnou

2004-10-05, 8:47 pm

DrTebi wrote:
>
> 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?
>
> Any ideas? I also tried awk, but still, I cannot seem to make the
> result a valid input for cat.


Assuming the latest file is determined by the time stamp xxxx-xx-xx

set -- error_log.*
: "$@"
mail -s "$_" < "$_"


You don't need the contents in a variable, do you? But if so use
var=$(< "$_") or var=`cat "$_"` before you mail the contents
using "$var".

Janis
Janis Papanagnou

2004-10-05, 8:47 pm

Janis Papanagnou wrote:
>
> set -- error_log.*
> : "$@"
> mail -s "$_" < "$_"


Ah, just noticed; this can be simplified further to

: error_log.*
mail -s "$_" < "$_"


Janis
Juhan Leemet

2004-10-05, 8:47 pm

On Tue, 05 Oct 2004 18:43:36 +0000, Chris F.A. Johnson wrote:
[snippage]
> Always break a problem into its constituent parts.


Always good advice.

> 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`


er, surely that should have been "head -1"?
ls -t gives most recent files first.

or I guess "ls -rt error_log.*|tail -1"
but I find that less straightforward.

--
Juhan Leemet
Logicognosis, Inc.

Chris F.A. Johnson

2004-10-05, 8:47 pm

On 2004-10-06, Juhan Leemet wrote:
> On Tue, 05 Oct 2004 18:43:36 +0000, Chris F.A. Johnson wrote:
> [snippage]
>
> Always good advice.
>
>
> er, surely that should have been "head -1"?
> ls -t gives most recent files first.


Oops! You're right. It's not the method I'd recommend. so I guess
I didn't pay close enough attention to it.

Perhaps I meant to use "ls error_log.* | tail -1".

> or I guess "ls -rt error_log.*|tail -1"
> but I find that less straightforward.


--
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
Stephane CHAZELAS

2004-10-06, 7:50 am

2004-10-05, 20:09(+02), DrTebi:
[...]
> 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?

[...]

Note that in zsh you could do:

mailx -s ... address < error_log.*(om[1])

--
Stephane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com