Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
 

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: globbing in Bash, some works, some does not?< Next Oldest | Next Newest >
mikshaw Offline





Group: Members
Posts: 4856
Joined: July 2004
Posted: Sep. 15 2006,18:00 QUOTE

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


--------------
http://www.tldp.org/LDP/intro-linux/html/index.html
Back to top
Profile PM WEB 
WDef Offline





Group: Members
Posts: 798
Joined: Sep. 2005
Posted: Sep. 15 2006,19:20 QUOTE

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
Back to top
Profile PM 
mikshaw Offline





Group: Members
Posts: 4856
Joined: July 2004
Posted: Sep. 15 2006,23:02 QUOTE

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!


--------------
http://www.tldp.org/LDP/intro-linux/html/index.html
Back to top
Profile PM WEB 
2 replies since Sep. 15 2006,18:00 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

 
reply to topic new topic new poll
Quick Reply: globbing in Bash

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code