Simple problem using ls output
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > Simple problem using ls output




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Simple problem using ls output  
DrTebi


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-05-04 10: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









[ Post a follow-up to this message ]



    Re: Simple problem using ls output  
Seb


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-05-04 10: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





[ Post a follow-up to this message ]



    Re: Simple problem using ls output  
DrTebi


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-05-04 10: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






[ Post a follow-up to this message ]



    Re: Simple problem using ls output  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
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 ]



    Re: Simple problem using ls output  
Janis Papanagnou


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-06-04 01:47 AM

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





[ Post a follow-up to this message ]



    Re: Simple problem using ls output  
Janis Papanagnou


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-06-04 01:47 AM

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

Ah, just noticed; this can be simplified further to

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


Janis





[ Post a follow-up to this message ]



    Re: Simple problem using ls output  
Juhan Leemet


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-06-04 01:47 AM

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.






[ Post a follow-up to this message ]



    Re: Simple problem using ls output  
Chris F.A. Johnson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-06-04 01:47 AM

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





[ Post a follow-up to this message ]



    Re: Simple problem using ls output  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-06-04 12:50 PM

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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:24 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register