Subject: Re: Shell hack -- getting files with dates
To: Jan Danielsson <jan.m.danielsson@gmail.com>
From: Mark E. Perkins <perkinsm@bway.net>
List: netbsd-users
Date: 02/03/2007 18:00:57
On 2007/02/03 17:10, Jan Danielsson wrote:
> Hello,
> 
>    I am processing a list of files using a for loop. However -- I would
> like to perform some special processing if the file names contain a
> date. I wrote this:
> 
> -----------------------------
> #!/bin/sh
> 
> for f in ~/backup/*.gpg
> do
>         if echo "$f" | egrep -q '[0-9]{4}-[0-9]{2}-[0-9]{2}' ; then
>                 echo "$f"
>         fi
> 
> done
> -----------------------------
> 
>    ...and it seems to do what I expect it to. What I am actually using
> is the return code from egrep -- but is it safe to do it like that? Is
> my assumption correct -- that the return code from egrep is simply
> "passed through" echo, or will I be seeing odd side effects five years
> from now?
> 

The reason this "seems to do what [you] expect it to" is not because the
return code from egrep is "passed through" echo (as you put it).  Rather, it
is because the exit status of a pipeline is the exit status of the last
command in the pipe.

Mark