Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-22-2008, 10:25 AM   #1
LQ Newbie
Registered: Jul 2008
Posts: 1

Rep: Reputation: 0
Korn Shell Scripting case statement help!!

I am testing the below case statement and is not working.

case ${DOWHR} in
008) << This works
echo "Test 1" ;;
[009-014]) << This does not work
echo "Test 1" ;;

Old 07-22-2008, 10:41 AM   #2
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Please post program code in [code] tags to aid readability.

What do you expect [009-014] to match? If you expect it to match three digit numbers in the range of 9 to 14, you are mis-understanding shall pattern matching. It does not to integer ranges like that.

Probably what you want is something like this:
case "$DOWHR" in
        # ugly way to strip off leading 0's
        tmp="${DOWHR#0}" ; tmp="${tmp#0}"
        if [ "$tmp" -ge 9 ] && [ "$tmp" -le 14 ]; then
                echo "we have a match"
Old 07-22-2008, 10:48 AM   #3
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060Reputation: 1060
Give this a try:


case ${DOWHR} in
008) echo "Test 1" ;;
009|010|011|012|013|014) echo "Test 2" ;;
Old 07-22-2008, 10:51 AM   #4
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
In a case/esac construct you can also specify multiple patterns to execute the same block of code, using the logical OR:
case ${DOWHR} in

   009|010|011|012|013|014) echo something ;;
Edit: oops... too late!


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
korn shell: scripting an or for if..then Harry Seldon Linux - General 2 01-10-2008 04:21 PM
shell scripting question - IF statement copperfox Linux - Software 1 09-28-2007 09:22 AM
How do I use an If statement within a case in a Shell script?? crazygyhrous Programming 7 01-03-2006 06:41 AM
Case Statement With Aix Ksh Scripting ']['HeBroken AIX 2 02-09-2005 12:44 PM
KSH Scripting case statement..... ']['HeBroken Programming 1 12-10-2004 10:38 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:07 AM.

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