Programming and Scripting :: globbing in Bash



I think this is the most annoying thing I've run into with Bash, mainly because I try to use globbing fairly frequently and usually it doesn't work the same way as it does in other applications.

Latest example is this renaming script.  I made this because I often run this manually:
Code Sample
for i in *; do [ -f "$i" ] && o=`echo "$i" | tr '[A-Z]' '[a-z]'` && mv "$i" "$o"; done

That's not fun or convenient, and the renaming scripts I've found so far are either not specific to my needs or they are much too complex for my taste (seems to be that way for an increasing number of applications). It also does not check for an existing file of the same name.

So anyway, what I've had to do to find uppercase or mixed case filenames is *[ABCDEFGHIJKLMNOPQRSTUVWXYZ]* because for some reason Bash sees *[A-Z]* as *, or maybe as *[A-Za-z]*.
Code Sample

PREFIX="000_RENAME_"

[ "${1}" -a -d "${1}" ] && DIR="${1}" || DIR=`pwd`

cd "${DIR}"
echo "You are about to rename all uppercase and mixed case files in
`pwd`
If this results in duplicate filenames, the second one will instead be
prefixed with \"${PREFIX}\" for manual renaming later.

Do you want to continue?"
read anykey
case ${anykey} in
[Yy]|[Yy][Ee][Ss]|[Oo][Kk])
for inpoot in *[ABCDEFGHIJKLMNOPQRSTUVWXYZ]*; do # Find capitals. WHY does *[A-Z]* not work??
#echo $inpoot
outpoot=`echo "${inpoot}" | tr '[A-Z]' '[a-z]'`
if [ -f "${inpoot}" ]; then # rename only regular files
 if [ -e "${outpoot}" ]; then # don't overwrite any existing files
   mv "${inpoot}" "${PREFIX}${outpoot}" && echo "${outpoot} already exists...renaming to ${PREFIX}${inpoot}"
 else
   mv "${inpoot}" "${outpoot}" && echo "lowered ${inpoot}: ${outpoot}"
 fi
fi
done
;;
*) exit 0;;
esac

Hope this helps Mik, might point in the right direction. Apparently, globbing of eg [A-Z] is a locale-dependent thing.  Different locale setting, different interpretation of [A-Z].

Quote
When you set your locale to anything other than 'C', then both [A-Z]
and [a-z] become whatever the locale wants it to mean.  They do not
necessarily mean the same thing as [A-Za-z].  It could be, for
example, that [A-Z] is an invalid regular expression, because 'Z'
comes before 'A' in this locale.

You are supposed to use [[:lower:]] if you want lower case letters in
the current locale.


Perhaps [[:upper:]] will do it.

From:http://mail.nl.linux.org/linux-utf8/2001-05/msg00033.html

Phil

I had tried [:upper:] with results that I couldn't explain...a few files were displayed, but i couldn't see the pattern to it.
Using [[:upper:]] works.  I wonder if I misread the Bash docs?

Thank you!


original here.