Is there any function in shell that enable to check for upper or lower case? i would like to check the input argumnets for upper or lower case.

Recommended Answers

All 6 Replies

Perhaps use case/esac with a pattern of [a-z]+

Try this:

function test_upper {
test=`echo $1 | grep [A-Z]`

if [ "$?" = "0" ]; then
  #Uppercase letters found.
  #translate to lower case:
  echo $test | tr '[A-Z]' '[a-z]
   #letters are all lowercase, return them.
   echo $1

Not exactly sure what you want your end result to be - but this for instance should find uppercase letters - if they are there, convert them to lowercase.
Basically, doing the grep for [A-Z] returns the argument provided if it has capital letters. If this happens, the return code ($?) is 0. If the return code is a 1, no capital letters were in the argument.
You can then run through all arguments.

You can apply this anyway you want though. Grep for [a-z] or [A-Z]. If the return code is 0, then the caps or lowercase were found and you can act accordingly.

thanks omrsafetyo!!!

i would like to know wheather is there a way thats i can check multiple arguments at the same time? bcos i got 3 args to input then i would like to check this 3 args for uppercase.

with my above function:

while [ "$*" != "" ]
   working_arg=`test_upper $1`
   if [ "x${working_arg}x" != "xx" ]; then
      echo "$working_arg is all lowercase."

... or

for arg in "$*"
   test_upper $arg

if you want something simpler.

You could, for instance, rewrite the test_upper function to return a 1 or a 0, depending on whether or not it found uppercase. (You could simple use "echo $?" to "return" this value). Then, when you loop the arguments, you can have a variable that holds whether or not the case you are testing for was found (globally). e.g.

for arg in "$*"
   if [ `test_upper $arg` -eq 0 ]

if [ $CAPS -eq 1 ]
  #blah blah blah

Look at the bash guide here. It has examples on checking for upper case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.