[opensuse] BASH: Shorter way to do [[ $line == QUIT ]] || [[ $line == Quit ]] || [[ $line == quit ]] && break ?
Bash gurus, What is a shorter way to do multiple case insensitive tests on a string or variable that would accomplish this: [[ $line == QUIT ]] || [[ $line == Quit ]] || [[ $line == quit ]] && break Basically what I am looking for is some case insensitive way to test for quit or exit without having to define a test for each possible permutation without bringing in a big hammer like sed or the like. I am a bit confused on the test expansion but know things like $line == [Qq]uit won't work. I can do it in case: case $line in [Qq][Uu][Ii][Tt] ) break;; esac But that seems like a clunky use of case. Is there a better way? -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 28 July 2009 17:26:30 David C. Rankin wrote:
Bash gurus,
What is a shorter way to do multiple case insensitive tests on a string or variable that would accomplish this:
[[ $line == QUIT ]] || [[ $line == Quit ]] || [[ $line == quit ]] && break
Basically what I am looking for is some case insensitive way to test for quit or exit without having to define a test for each possible permutation without bringing in a big hammer like sed or the like. I am a bit confused on the test expansion but know things like $line == [Qq]uit won't work.
I can do it in case:
case $line in [Qq][Uu][Ii][Tt] ) break;; esac
But that seems like a clunky use of case. Is there a better way?
~> cat test.sh #!/bin/bash compare() { mytext=$(echo $1|tr [:upper:] [:lower:]) if [ "quit" = "$mytext" ]; then echo "yes"; else echo "no" fi } compare "QuIt" compare "QUIT" compare "quit" compare "foo" ~> ./test.sh yes yes yes no Anders -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Anders Johansson wrote:
On Tuesday 28 July 2009 17:26:30 David C. Rankin wrote:
Bash gurus,
What is a shorter way to do multiple case insensitive tests on a string or variable that would accomplish this:
[[ $line == QUIT ]] || [[ $line == Quit ]] || [[ $line == quit ]] && break
Basically what I am looking for is some case insensitive way to test for quit or exit without having to define a test for each possible permutation without bringing in a big hammer like sed or the like. I am a bit confused on the test expansion but know things like $line == [Qq]uit won't work.
I can do it in case:
case $line in [Qq][Uu][Ii][Tt] ) break;; esac
But that seems like a clunky use of case. Is there a better way?
~> cat test.sh #!/bin/bash
compare() { mytext=$(echo $1|tr [:upper:] [:lower:]) if [ "quit" = "$mytext" ]; then echo "yes"; else echo "no" fi }
compare "QuIt" compare "QUIT" compare "quit" compare "foo"
~> ./test.sh yes yes yes no
Anders
The case statement is a lot cheaper than spawning a subshell! You can write the whole case all on one line too for something like that to reduce clutter. ksh has to-upper/lower built in as a typeset option, I forget if bash does but you can check it as easy as I can. -- bkw -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday July 28 2009, David C. Rankin wrote:
Bash gurus,
What is a shorter way to do multiple case insensitive tests on a string or variable that would accomplish this:
[[ $line == QUIT ]] || [[ $line == Quit ]] || [[ $line == quit ]] && break
...
I can do it in case:
case $line in [Qq][Uu][Ii][Tt] ) break;; esac
% line=QuIt % [[ $line == [Qq][Uu][Ii][Tt] ]] && echo 'Quit, already!' Quit, already! % line=GoOn % [[ $line == [Qq][Uu][Ii][Tt] ]] && echo 'Quit, already!' || echo 'More is better...' More is better...
... -- David C. Rankin
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
David C. Rankin wrote:
Bash gurus,
What is a shorter way to do multiple case insensitive tests on a string or variable that would accomplish this:
[[ $line == QUIT ]] || [[ $line == Quit ]] || [[ $line == quit ]] && [[ break
You could translate $line to upper or lower case first (using tr), then do your tests. /Per -- Per Jessen, Zürich (24.2°C) -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 28 July 2009 11:23:38 am Per Jessen wrote:
David C. Rankin wrote:
Bash gurus,
What is a shorter way to do multiple case insensitive tests on a string or variable that would accomplish this:
[[ $line == QUIT ]] || [[ $line == Quit ]] || [[ $line == quit ]] && [[ break
You could translate $line to upper or lower case first (using tr), then do your tests.
/Per
Anders, Randall, Per, Thanks. As in Anders and Pers responses, the translate to upper/lower before testing is the bit that hadn't flipped in my mind. Makes it a lot easier, thanks again. -- David C. Rankin, J.D.,P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
David C. Rankin wrote:
But that seems like a clunky use of case. Is there a better way?
Yes: $ shopt -s nocasematch $ [[ "foo" == "FoO" ]] && echo bar bar $ That's it. Regards nordi -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
David C. Rankin wrote:
But that seems like a clunky use of case. Is there a better way?
$ shopt -s nocasematch $ [[ "foo" == "FoO" ]] && echo bar bar $
That's it.
Regards nordi
I think that one wins the prize for elegance and simplicity. Personally, I'd probably have broken out grep -i -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday July 28 2009, Philip Dowie wrote:
... $ shopt -s nocasematch $ [[ "foo" == "FoO" ]] && echo bar bar $
...
I think that one wins the prize for elegance and simplicity. Personally, I'd probably have broken out grep -i
Don't forget to reset the option if you don't want case-insensitive matching subsequently. Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Here is the simplest: IF UPPER(LINE) = "QUIT" THEN EXIT/ /But this is in REXX -- Duaine Hechler Piano, Player Piano, Pump Organ Tuning, Servicing & Rebuilding Reed Organ Society Member Florissant, MO 63034 (314) 838-5587 dahechler@att.net www.hechlerpianoandorgan.com -- Home & Business user of Linux - 10 years -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Duaine & Laura Hechler wrote:
Here is the simplest:
IF UPPER(LINE) = "QUIT" THEN EXIT/
/But this is in REXX
Which, given the subject, isn't applicable :-) /Per -- Per Jessen, Zürich (17.7°C) -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Per Jessen - 8:59 29.07.09 wrote:
Duaine & Laura Hechler wrote:
Here is the simplest:
IF UPPER(LINE) = "QUIT" THEN EXIT/
/But this is in REXX
Which, given the subject, isn't applicable :-)
Rewritten to shell it would be something like [ "`echo "$line" | tr [a-z] [A-Z]`" = "QUIT" ] && break -- Michal Hrusecky Package Maintainer SUSE LINUX, s.r.o e-mail: mhrusecky@suse.cz
Michal Hrusecky wrote:
Per Jessen - 8:59 29.07.09 wrote:
Duaine & Laura Hechler wrote:
Here is the simplest:
IF UPPER(LINE) = "QUIT" THEN EXIT/
/But this is in REXX
Which, given the subject, isn't applicable :-)
Rewritten to shell it would be something like
[ "`echo "$line" | tr [a-z] [A-Z]`" = "QUIT" ] && break
Good grief read the prior messages in the thread. There is no need and no excuse to spawn child shells and other processes for this. -- bkw -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Rewritten to shell it would be something like
[ "`echo "$line" | tr [a-z] [A-Z]`" = "QUIT" ] && break
Good grief read the prior messages in the thread. There is no need and no excuse to spawn child shells and other processes for this.
which, IMHO is the *entire* point. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Philip Dowie wrote:
Rewritten to shell it would be something like
[ "`echo "$line" | tr [a-z] [A-Z]`" = "QUIT" ] && break
Good grief read the prior messages in the thread. There is no need and no excuse to spawn child shells and other processes for this.
which, IMHO is the *entire* point.
I know - someday - I'm going to have to learn BASH -- but -- this is why I, personally, still use REXX - it makes things - way - so - much - simpler. Plus 30+ years background in programming in REXX. Aside - I can't even imagine trying this one in bash -- years ago, I wrote a - REXX ReAssembler - reading mainframe 370 objects decks to the mainframe 370 assembler source. (In case anyone wants to look at it - I still have the script) Duaine -- Duaine Hechler Piano, Player Piano, Pump Organ Tuning, Servicing & Rebuilding Reed Organ Society Member Florissant, MO 63034 (314) 838-5587 dahechler@att.net www.hechlerpianoandorgan.com -- Home & Business user of Linux - 10 years -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Tue, 28 Jul 2009, David C. Rankin wrote:
What is a shorter way to do multiple case insensitive tests on a string or variable that would accomplish this:
[[ $line == QUIT ]] || [[ $line == Quit ]] || [[ $line == quit ]] && break
Basically what I am looking for is some case insensitive way to test for quit or exit without having to define a test for each possible permutation without bringing in a big hammer like sed or the like. I am a bit confused on the test expansion but know things like $line == [Qq]uit won't work.
I can do it in case:
case $line in [Qq][Uu][Ii][Tt] ) break;; esac
But that seems like a clunky use of case. Is there a better way?
Using case is the canonical way. But you could also use: test -z "${line#[Qq][Uu][Ii][Tt]}" && break HTH, -dnh -- Every feature is a bug, unless it can be turned off. -- Karl Heuer -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (10)
-
Anders Johansson
-
Brian K. White
-
David C. Rankin
-
David Haller
-
Duaine & Laura Hechler
-
Michal Hrusecky
-
nordi
-
Per Jessen
-
Philip Dowie
-
Randall R Schulz