LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 03-21-2008, 11:05 PM   #1
jim.thornton
Member
 
Registered: May 2007
Posts: 430

Rep: Reputation: 19
Scripting question


Is it possible when writing a bash script to have a case statement that can handle a range?? Or would I have to use multiple if statements.

For example, I have to write a script that will count the number of arguments provided. I can do it with if statements but I would like to do it with a case statment and have three conditions:

1. > 4 ---> If more than 4 arguments are provided do...
2. < 4 ---> If less than 4 arguments are provided do...
3. = 4 ---> If 4 arguments are provided do...

Is this possible with case??
 
Old 03-22-2008, 04:09 AM   #2
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
You can do something like:
Code:
case $# in

   [0-3])   echo "less than 4" ;;

   4)   echo  "equal to 4" ;;

   *)   echo  "more than 4" ;;

esac
Here the [0-3] is not really for numerical comparison, but it is a range of digits. It would be a little more complicated if the range was between larger numbers (e.g. 23-47) but in your case it should do the trick.
 
Old 03-22-2008, 07:56 AM   #3
jim.thornton
Member
 
Registered: May 2007
Posts: 430

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by colucix View Post
You can do something like:
Code:
case $# in

   [0-3])   echo "less than 4" ;;

   4)   echo  "equal to 4" ;;

   *)   echo  "more than 4" ;;

esac
Here the [0-3] is not really for numerical comparison, but it is a range of digits. It would be a little more complicated if the range was between larger numbers (e.g. 23-47) but in your case it should do the trick.
Awesome... Thank you.

From your answer it seems as though there is no way to test if there is more than x or less than x. Am I correct. I would always have to improvise like that?
 
Old 03-22-2008, 09:13 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
Quote:
Originally Posted by jim.thornton View Post
From your answer it seems as though there is no way to test if there is more than x or less than x. Am I correct. I would always have to improvise like that?
Correct. The case statement compares the value of the variable with those provided inside the construct. It performs a "string comparison" and as far as I know there is no way to use comparison operators.
 
Old 03-22-2008, 09:25 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Case uses a pattern to determine which section of the case statement to execute. For single digits, it's simple to use patterns like those above which will allow you to effectively select by number range, but for more complex numbers it is not easy. Consider numbers more than 10... then the patterns become much harder to make, at least in a way which is not very long winded.

But you have if...elif... fi which can be used very well for this:
Code:
if [ $number -ge 0 ] && [ $number -lt 10 ]; then
    echo "$number is in range 0..9"
elif [ $number -ge 10 ] && [ $number -lt 20 ]; then
    echo "$number is in range 10..19"
elif [ $number -ge 20 ] && [ $number -lt 30 ]; then
    echo "$number is in range 20..29"
fi
 
Old 03-23-2008, 05:58 PM   #6
jim.thornton
Member
 
Registered: May 2007
Posts: 430

Original Poster
Rep: Reputation: 19
Thanks...

How do I do an OR (||) when scripting for bash? I tried using || and it didn't work
 
Old 03-23-2008, 06:08 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You can use || as or. You have to make sure you put spaces in the right places. What did you try?
 
Old 03-23-2008, 07:24 PM   #8
jim.thornton
Member
 
Registered: May 2007
Posts: 430

Original Poster
Rep: Reputation: 19
Quote:
Originally Posted by matthewg42 View Post
You can use || as or. You have to make sure you put spaces in the right places. What did you try?
I want it to take -i or -I as the argument and this is what I have:

Code:
if [ "$1" != "-i" || "$1" != "-I" ]
The this is the error I'm getting:

./test.sh: line 50: [: missing `]'
./test.sh: line 50: -i: command not found
 
Old 03-24-2008, 04:49 AM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
The syntax would be like this:
Code:
if [ "$1" != "-i" ] || [ "$1" != "-I" ]
If you're parsing command line options, you might want to take a look at the bash internal getopt (edit: and getopts) which can be used to validate options for you.

For example, if you have a script which can take the options -a -b and -C, where -b takes an argument to the option, you might do it like this:

Code:
#!/bin/bash

while getopts "ab:C" option; do
        case $option in
        a|C)
                echo "we have option: $option (no argument for this option)"
                ;;
        b)
                echo "we have option: $option with argument: $OPTARG"
                ;;
        *)
                echo "ERROR - unknown option" >&2
                exit 1
                ;;
        esac
done

# remove options from parameter list, leaving just remaining parameters
shift $(($OPTIND - 1))

for p in "$@"; do
        echo "post-option parameter: $p"
done
An example invocation and output:
Code:
% ./test.sh -a -b "my option argument" -C now the rest of the parameters
we have option: a (no argument for this option)
we have option: b with argument: my option argument
we have option: C (no argument for this option)
post-option parameter: now
post-option parameter: the
post-option parameter: rest
post-option parameter: of
post-option parameter: the
post-option parameter: parameters

Last edited by matthewg42; 03-24-2008 at 04:11 PM.
 
Old 03-24-2008, 08:03 AM   #10
jim.thornton
Member
 
Registered: May 2007
Posts: 430

Original Poster
Rep: Reputation: 19
Wow... That was such a great response.

Thank you.
 
Old 03-24-2008, 08:12 AM   #11
smehra24
Member
 
Registered: Jan 2008
Location: mumbai
Distribution: red hat
Posts: 31

Rep: Reputation: 15
case $# in
[0-3]) echo 'arguments less than 4';;
4) echo 'arguments equal to 4';;
*) echo 'More than 4'
esac
 
  


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
Yet another scripting question kinetik Linux - General 4 04-01-2006 12:30 PM
Scripting question mithereal Linux - General 2 09-21-2005 02:02 AM
scripting question gearoid Programming 3 05-26-2004 02:43 PM
Scripting question glock19 Linux - Software 2 09-05-2003 12:45 AM
Scripting question aikitortoise Linux - Newbie 3 11-14-2002 05:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:00 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
Open Source Consulting | Domain Registration