On Mon, Dec 22, 2014 at 10:51:26AM -0800, pe...@easthope.ca wrote:
> This command in a shell script removes unwanted log files.
> 
> for i in $( echo *.Log ); do 
>   /bin/rm $i; 
>   echo "Removed $i." 
> done
> 
> In the edge case of no matching files, rm complains.
> /bin/rm: cannot remove `*.Log': No such file or directory
> 
> If echo is replaced with ls, it complains when there 
> is no match.
> 
> Does anyone have a tidy solution for this task?

First of all, you have an unnecessary use of "echo". Although
seemingly innocuous, if you have file names with spaces, the spaces
will mess things up as things will be going through the shell
twice. Which is once too many.

Second, remember that when the shell performs pathname expansion, it
will leave the original pattern in there if nothing matches.  Hence
the error message you see.

This should do the trick:

  for i in *.Log ; do
    if test -f "$i"; then
      rm "$i"
      echo Removed $i.
    fi
  done

or if you want to make things *really* simple:

  rm --force --verbose *.Log

:-)

-- 
Karl E. Jorgensen


-- 
To UNSUBSCRIBE, email to debian-user-requ...@lists.debian.org 
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org
Archive: https://lists.debian.org/20141222201542.GB5035@hawking

Reply via email to