Unix Shell - How to silence selected errors?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > May 2004 > How to silence selected errors?





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 How to silence selected errors?
bill

2004-05-23, 12:09 pm




Is there any way to silence errors selectively?

One error that is bugging me right now is

cp: cannot stat `foo/*': No such file or directory

I don't want to add '2>/dev/null' to the command, because I don't
want to miss any other errors. I just want to get rid of these
"cannot stat" errors.

Is there any standard way to filter out selected errors?

Shell is bash.

Thanks!

-bill

Chris F.A. Johnson

2004-05-23, 4:30 pm

On 2004-05-23, bill wrote:
>
>
>
> Is there any way to silence errors selectively?
>
> One error that is bugging me right now is
>
> cp: cannot stat `foo/*': No such file or directory
>
> I don't want to add '2>/dev/null' to the command, because I don't
> want to miss any other errors. I just want to get rid of these
> "cannot stat" errors.
>
> Is there any standard way to filter out selected errors?


Fix the problem that is causing those errors.

> Shell is bash.


Untested:

cp ...... 2> >(grep -v "cannot stat")

--
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
Alan Connor

2004-05-23, 4:30 pm

On 23 May 2004 17:31:18 GMT, Chris F.A. Johnson <c.fa.johnson@rogers.com> wrote:
>
>
> On 2004-05-23, bill wrote:
>
> Fix the problem that is causing those errors.
>
>
> Untested:
>
> cp ...... 2> >(grep -v "cannot stat")
>


ROTFL !!

AC

Stephane CHAZELAS

2004-05-24, 7:32 am

2004-05-23, 15:48(+00), bill:
> Is there any way to silence errors selectively?
>
> One error that is bugging me right now is
>
> cp: cannot stat `foo/*': No such file or directory


That's a bug (mis-feature) in most Bourne-like shells. When a
pattern doesn't match, it expands to itself, you may end-up
cp'ing the wrong files this way:

rm -- [A-Z]*

removes every file whose name starts with a capital letter. If
there's no such file, it may remove a file named '[A-Z]*'.

If there's no matching file, an error should be reported and the
command should not be executed.

zsh fixed that bug.

If there's a chance there's no matching file, then you should
test for it:

bash:

shopt -s nullglob
file=(foo/*)
# don't use file=(*) but file=(./*) as bash has a bug with that
if (( ${#files[@]} == 0 )); then
perform whatever recovery action
else
cp -- "${files[@]}" /dest
fi


--
Stephane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com