Godwin Stewart <[EMAIL PROTECTED]> 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:]' _`

[ ... ]

_______________________________________________
Bug-textutils mailing list
[EMAIL PROTECTED]
http://mail.gnu.org/mailman/listinfo/bug-textutils

Reply via email to