bug-textutils
[Top][All Lists]
Advanced

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

Re: Is this a bug in tr?


From: Jim Meyering
Subject: Re: Is this a bug in tr?
Date: Sun, 28 Oct 2001 11:25:19 +0100
User-agent: Gnus/5.090004 (Oort Gnus v0.04) Emacs/21.0.107

Godwin Stewart <address@hidden> wrote:
> I wanted a shell script to convert all the filenames in a given directory so
> that any uppercase letters are made lowercase and so that any spaces are
> replaced by an underscore.
>
> Great, I thought. This is just what tr was made for. Then I had a
> surprise...
>
> This is the script itself, with most of the "works" commented out while I
> was trying to narrow down the source of the problem I was having:
>
> #!/bin/bash
> #
> #
> # Script to convert all filenames to lower case, and to replace spaces with
> underscores
> #
> #
>
> ls | while read f; do
>   g=`echo "$f" | tr A-Z a-z | tr [:blank:] _`

Here's the problem:

You didn't quote the `[:blank:]' argument, and since
`[...]' ranges are interpreted by the shell and since
you had a file named `n' that matched that pattern, your
command became this:

   g=`echo "$f" | tr A-Z a-z | tr n _`

To avoid that, you could have written it like this:

   g=`echo "$f" | tr A-Z a-z | tr '[:blank:]' _`

Or better still (since the meaning of A-Z and a-z can depend
on your locale settings) like this:

   g=`echo "$f" | tr '[:upper:]' '[:lower:]' | tr '[:blank:]' _`

[ ... ]



reply via email to

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