bug-fileutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ls: avoiding error msg on non-existant file


From: Bob Proulx
Subject: Re: ls: avoiding error msg on non-existant file
Date: Thu, 21 Nov 2002 23:36:18 -0700
User-agent: Mutt/1.4i

Karl O . Pinc <address@hidden> [2002-11-18 14:33:30 -0600]:
> To keep there error from coming up, I can
> 
> 2>/dev/null
> 
> if [ ! -e . ] ; then ls pattern ; fi
> 
> but niether of these solutions is particularly elegant.
> The first throws all errors away, and I want to know if
> there are any other errors.  The second assumes that
> there will only be files in the directory when the directory
> also contains files matching <pattern>.

I am not sure I understood that.  Won't '.' always exist?

The first is rather like this.

  for file in pattern ; do
    if [ ! -f "$file" ]; then
      echo "$file: no such file" 1>&2
    fi
  done

Since the shell is expanding the wildcard first, or not as in your not
existing case, and then ls is seeing the nonexistent file.  Perhaps
you could just use the shell directly when using a pattern.

> There's also:
> 
> if [ -n "$(ls pattern 2>/dev/null)" ] ; then ls pattern ; fi

Idea:  Perhaps you could use plain old shell file globbing.

  echo pattern

as in

  echo *.html | fmt

It appears that you are only taking benefit from the column forming
feature of ls and nothing else.  Therefore you could possibly avoid it
here.  If pattern does not match then the output is the same as the
input.  Which probably is not what you wanted.

You did not really say what you were trying to do.  Therefore it is
hard to suggest alternatives.

> An 'ignore-non-existant-files' option seems cleanest.
> 
> :-( bloat, bloat, bloat )

Yep.  Sigh.

Bob

-- 
Please follow up to the list and not to me privately unless it is
personal.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]