tags:

views:

218

answers:

1

Hi

I'm writing a bash script which needs to convert a string to lowercase. The problem is I'm doing it on a mac so 'tr' is not available. How can I go about doing this on a mac?

The problem I'm trying to tackle is that my script needs to recognize an if an extension is a .gif or a .jpg - and I don't want to have to check for .jpeg, .jPeg, .JPEG, .JPeg, etc etc etc... if there's a smarter way of doing this than just converting to lowercase and testing for gif, jpg and jpeg, i'm all ears :)

UPDATE:
I am an idiot.
The reason this mac "doesn't have" these basic text-conversion programs is because I overwrote PATH with "hello" when doing some testing >_<

+2  A: 

in bash, you can use nocaseglob

shopt -s nocaseglob
for file in *.jpg *.jpeg *.gif
do
  echo "$file"
done
#turn off
shopt -u nocaseglob

in general to convert cases, various ways

echo "stRING" | awk '{print toupper($0)}'

echo "STRING" | tr "[A-Z]" "[a-z]" # upper to lower

echo "StrinNG" | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' #lower to upper
ghostdog74
what side-effects will this have? will it just make bash match in a case-insensitive way until I turn it back off?
Mala
Oh I also need to get the extension in lowercase as I'm writing it back... will this do that for me?
Mala
ah nevermind, I'm going with this and nested for loops: for extension in gif jpg jpeg, for file in *.$extension, [...] done done;Thanks!
Mala
oddly enough, this mac (10.4.8) does not have awk, tr OR sed... or maybe they just aren't in the default path?
Mala