943,768 Members | Top Members by Rank

Ad:
Jul 23rd, 2008
0

check input arguments is upper or lower case

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
picass0 is offline Offline
45 posts
since Nov 2007
Jul 23rd, 2008
0

Re: check input arguments is upper or lower case

Perhaps use case/esac with a pattern of [a-z]+
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 23rd, 2008
0

Re: check input arguments is upper or lower case

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.
Reputation Points: 13
Solved Threads: 9
Junior Poster in Training
omrsafetyo is offline Offline
58 posts
since Apr 2008
Jul 24th, 2008
0

Re: check input arguments is upper or lower case

thanks omrsafetyo!!!
Reputation Points: 10
Solved Threads: 0
Light Poster
picass0 is offline Offline
45 posts
since Nov 2007
Jul 24th, 2008
0

Re: check input arguments is upper or lower case

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
picass0 is offline Offline
45 posts
since Nov 2007
Jul 24th, 2008
0

Re: check input arguments is upper or lower case

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.
Reputation Points: 13
Solved Threads: 9
Junior Poster in Training
omrsafetyo is offline Offline
58 posts
since Apr 2008
Jul 25th, 2008
0

Re: check input arguments is upper or lower case

Look at the bash guide here. It has examples on checking for upper case.
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: print only the last entry of the file
Next Thread in Shell Scripting Forum Timeline: Help!!!!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC