| | |
check input arguments is upper or lower case
![]() |
•
•
Join Date: Apr 2008
Posts: 58
Reputation:
Solved Threads: 9
Try this:
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.
Shell Scripting Syntax (Toggle Plain Text)
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] else #letters are all lowercase, return them. echo $1 fi }
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.
•
•
Join Date: Apr 2008
Posts: 58
Reputation:
Solved Threads: 9
with my above function:
... or
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.
Shell Scripting Syntax (Toggle Plain Text)
while [ "$*" != "" ] do working_arg=`test_upper $1` if [ "x${working_arg}x" != "xx" ]; then echo "$working_arg is all lowercase." fi shift done
Shell Scripting Syntax (Toggle Plain Text)
for arg in "$*" do test_upper $arg done
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.
Shell Scripting Syntax (Toggle Plain Text)
CAPS=0 for arg in "$*" do if [ `test_upper $arg` -eq 0 ] then CAPS=1 fi done if [ $CAPS -eq 1 ] then #blah blah blah fi
Last edited by omrsafetyo; Jul 24th, 2008 at 3:55 pm.
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
Look at the bash guide here. It has examples on checking for upper case.
![]() |
Similar Threads
Other Threads in the Shell Scripting Forum
- Previous Thread: print only the last entry of the file
- Next Thread: Help!!!!!!
| Thread Tools | Search this Thread |






