Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression for case insensitivity

Status
Not open for further replies.

blainepruitt

Technical User
Apr 18, 2002
105
US
Greetings all,

I figure this will be a somewhat easy answer for those more familiar with regular expressions than myself, but how would I take a value and either make it all upper case or all lower case before running it through a test statement. I have some clients using my script that regularly type in the wrong case which causes the test statements to fail. Any help you can provide would be extremely valuable. Thanks in advance.

-bp
 
If your script is ksh you can set the variable to upper or lower before the test with typeset

VARIABLE=$1
-typeset -l $VARIABLE (lower case)
or
-typeset -u $VARIABLE (upper case)

-jim
 
J1mbo,

Thanks for your help, but unfortunately this script was written in the bourne shell. I tried the typeset code, but was unsuccessful. Thank you for you help.

-bp
 
If you have ksh on your system and it's your script,
change it to ksh. ksh should handle the bourne shell
code. Otherwise, try something like this

#! /bin/sh

echo "Enter something: "
read VAR
# user enters 'test', for example

case $VAR in
test) do stuff ;;
TEST) do stuff;;
*) error message;;
esac

----

Depending on what is being input, this can be shortened to
case $VAR in
[Tt][Ee][Ss][Tt]) do stuff ;;
*) error message;;
esac

--- Or with an 'if' statement:

if [ $VAR -eq "test" || $VAR -eq "TEST"] ; do

...do stuff

fi


Hope this helps,

jim



 
The following seems to work for me:

var2=`echo $var1 | tr a-z A-Z`

Good luck "Code what you mean,
and mean what you code!
But by all means post your code!"

Razalas
 
You may want to use case statements instead of test:
case $var in
a|A) echo "a or A was entered";
b|B) echo "b or B was entered";
*) echo "something else was entered";
esac
 
The tr command worked like a champ! Thanks to all for their assistance with this.

-bp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top