LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 10: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, 10:41 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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, 10: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: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
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, 10:51 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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



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

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

All times are GMT -5. The time now is 06:15 AM.

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
Open Source Consulting | Domain Registration