LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 07-22-2008, 11:25 AM   #1
wfdeng117
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" ;;

esac
 
Old 07-22-2008, 11:41 AM   #2
matthewg42
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:
Code:
case "$DOWHR" in
[0-9][0-9][0-9])
        # 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"
        fi
esac
 
Old 07-22-2008, 11:48 AM   #3
tronayne
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:

#!/bin/ksh

case ${DOWHR} in
008) echo "Test 1" ;;
009|010|011|012|013|014) echo "Test 2" ;;
esac
 
Old 07-22-2008, 11:51 AM   #4
colucix
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:
Code:
case ${DOWHR} in

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


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:42 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