check input arguments is upper or lower case

Reply

Join Date: Nov 2007
Posts: 45
Reputation: picass0 is an unknown quantity at this point 
Solved Threads: 0
picass0 picass0 is offline Offline
Light Poster

check input arguments is upper or lower case

 
0
  #1
Jul 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: check input arguments is upper or lower case

 
0
  #2
Jul 23rd, 2008
Perhaps use case/esac with a pattern of [a-z]+
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 58
Reputation: omrsafetyo is an unknown quantity at this point 
Solved Threads: 9
omrsafetyo omrsafetyo is offline Offline
Junior Poster in Training

Re: check input arguments is upper or lower case

 
0
  #3
Jul 23rd, 2008
Try this:
Shell Scripting Syntax (Toggle Plain Text)
  1. function test_upper {
  2. test=`echo $1 | grep [A-Z]`
  3.  
  4. if [ "$?" = "0" ]; then
  5. #Uppercase letters found.
  6. #translate to lower case:
  7. echo $test | tr '[A-Z]' '[a-z]
  8. else
  9. #letters are all lowercase, return them.
  10. echo $1
  11. fi
  12. }
  13.  
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 45
Reputation: picass0 is an unknown quantity at this point 
Solved Threads: 0
picass0 picass0 is offline Offline
Light Poster

Re: check input arguments is upper or lower case

 
0
  #4
Jul 24th, 2008
thanks omrsafetyo!!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 45
Reputation: picass0 is an unknown quantity at this point 
Solved Threads: 0
picass0 picass0 is offline Offline
Light Poster

Re: check input arguments is upper or lower case

 
0
  #5
Jul 24th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 58
Reputation: omrsafetyo is an unknown quantity at this point 
Solved Threads: 9
omrsafetyo omrsafetyo is offline Offline
Junior Poster in Training

Re: check input arguments is upper or lower case

 
0
  #6
Jul 24th, 2008
with my above function:

Shell Scripting Syntax (Toggle Plain Text)
  1. while [ "$*" != "" ]
  2. do
  3. working_arg=`test_upper $1`
  4. if [ "x${working_arg}x" != "xx" ]; then
  5. echo "$working_arg is all lowercase."
  6. fi
  7. shift
  8. done
... or

Shell Scripting Syntax (Toggle Plain Text)
  1. for arg in "$*"
  2. do
  3. test_upper $arg
  4. done
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)
  1. CAPS=0
  2. for arg in "$*"
  3. do
  4. if [ `test_upper $arg` -eq 0 ]
  5. then
  6. CAPS=1
  7. fi
  8. done
  9.  
  10. if [ $CAPS -eq 1 ]
  11. then
  12. #blah blah blah
  13. fi
Last edited by omrsafetyo; Jul 24th, 2008 at 3:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: check input arguments is upper or lower case

 
0
  #7
Jul 25th, 2008
Look at the bash guide here. It has examples on checking for upper case.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC