LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-17-2009, 04:19 PM   #1
laki47
Member
 
Registered: Feb 2009
Posts: 33

Rep: Reputation: 15
Case insensitve compare?


Hi!

I want to compare some variable, but case-insensitive:

i.e.

for i in *
do
if [ $i = "<some_text_here>" ]; then
...
fi
done

<some_text_here> can be: "test", "TeSt", "TEST", ...

How can I compare that?

Thank you!
 
Old 10-17-2009, 04:50 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Move the line into a second variable and use tr to lowercase it. Then test that second variable.

Code:
for i in *
do

j="$(echo $i|tr [:upper:] [:lower:])"

if [ $j = "<some_text_here>" ]; then
...
fi
done
If you're using bash version 4, there are some new parameter substitutions that make it easier.
Code:
if [ ${i,,} = "<some_text_here>" ]; then
${var,,} prints the variable in lowercase. ${var^^} uppercases it.
 
Old 10-17-2009, 04:53 PM   #3
laki47
Member
 
Registered: Feb 2009
Posts: 33

Original Poster
Rep: Reputation: 15
Unfortunately, I'm using ksh.
 
Old 10-17-2009, 04:57 PM   #4
Erik_FL
Member
 
Registered: Sep 2005
Location: Boynton Beach, FL
Distribution: Slackware
Posts: 815

Rep: Reputation: 254Reputation: 254Reputation: 254
Quote:
Originally Posted by laki47 View Post
Hi!

I want to compare some variable, but case-insensitive:

i.e.

for i in *
do
if [ $i = "<some_text_here>" ]; then
...
fi
done

<some_text_here> can be: "test", "TeSt", "TEST", ...

How can I compare that?

Thank you!
if [ "`echo $i | tr '[:upper:]' '[:lower:]'`" = "<lower-case-text>" ]; then

NOTE: Before the "echo" is a backward tick character and after '[:lower:]' is also a backward tick character.

You can also make this a function.

toLower() {
echo $1 | tr "[:upper:]" "[:lower:]"
}

if [ toLower($i) = "<lower-case-text>" ]; then
 
Old 10-17-2009, 05:12 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by laki47 View Post
<some_text_here> can be: "test", "TeSt", "TEST", ...

How can I compare that?
Use
Code:
shopt -s nocasematch
before doing the test and use [[ <comparison expression> ]] rather than [ <comparison expression> ]
 
Old 10-17-2009, 05:14 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by laki47 View Post
Unfortunately, I'm using ksh.
Would have been useful to know that up front
 
Old 10-17-2009, 05:33 PM   #7
laki47
Member
 
Registered: Feb 2009
Posts: 33

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by catkin View Post
Would have been useful to know that up front
I apologize...
 
Old 10-17-2009, 05:44 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by laki47 View Post
I apologize...
No worries, it's easy to overlook these things. Purely out of curiosity, why are you using ksh?
 
Old 10-17-2009, 05:45 PM   #9
laki47
Member
 
Registered: Feb 2009
Posts: 33

Original Poster
Rep: Reputation: 15
Is this ok?:

for i in *
do
if [ `echo $i |awk '{ print tolower($0) }'` = "<some_text_here>" ]; then
...
fi
done

THX

Last edited by laki47; 10-17-2009 at 05:51 PM.
 
Old 10-17-2009, 05:48 PM   #10
laki47
Member
 
Registered: Feb 2009
Posts: 33

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by catkin View Post
No worries, it's easy to overlook these things. Purely out of curiosity, why are you using ksh?
Well, actually it's not up to me, I also prefer bash... Rather say some job principles.

THX
 
Old 10-17-2009, 06:08 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
ksh has typeset -l and typeset -u that declare variables as only able to hold lowercase or uppercase values.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying files from case-sensitive Linux to case-insensitive Windows via CIFS? SlowCoder Linux - General 4 05-07-2008 08:03 PM
renaming directories from upper case to lower case, help!! linux_teller Linux - Newbie 3 03-07-2008 06:15 AM
mesa-6.x claims to support OpenGL 1.5, but in my case does not in my case. qwijibow Programming 2 11-23-2006 08:14 AM
Converting sLoPPy cASE to Pretty Case with tr lowpro2k3 Programming 4 04-13-2005 09:13 PM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 05:09 PM


All times are GMT -5. The time now is 06:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration