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 03-25-2005, 02:51 PM   #1
lbauer
LQ Newbie
 
Registered: Mar 2005
Distribution: Mac with X11
Posts: 3

Rep: Reputation: 0
How to Go Illini in Bash


I'm a newbie, and am just learning about scripts. Plus I like Illinois basketball.

So I'm writing this fun little script with Bash with an If - Then control structure.
I ask who the best team is. If the user types the right team name they get one response. If they type a different team they get another response.

I can get this to work:

if
test $team = Illinois
then . . .


But I want it to work whether they type Illini or illini or Illinois or illinois.

I've tried all sorts of variations of quoting, brackets, escaping, etc
with this:

if
test $team = [Ii]llinois -o [Ii]llini

But haven't found what works.
What characters do I need to use to make this work?
 
Old 03-25-2005, 03:05 PM   #2
saravkrish
Member
 
Registered: Mar 2004
Location: KY, USA
Distribution: Fedora Core 1
Posts: 190

Rep: Reputation: 30
Use perl. It's a whole lot easier for such regular expression comparisons.

-Sarav
 
Old 03-25-2005, 03:15 PM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
BAH! I scoff at your suggestion of using perl!!

(= Just kidding.. I love perl.. but then again, I'm stubborn and like to stretch bash.... actually, lbauer, this is really easy to do. in your test, you'll need to use the [[ ]] construct with an ==. You can see more about double brackets here.

Here's an example of use to show you how it can be done.
Code:
#!/bin/bash

teamname="Illinois"

if [[ $teamname == Illi* ]]; then
  echo "Match"
else
  echo "No match"
fi

if [[ $teamname == Illio* ]]; then
  echo "Match"
else
  echo "No match"
fi
When run:
Code:
~/bash> ./pmatch.sh
Match
No match
Because the second contains an o that should not be.

Happy bashing!
 
Old 03-25-2005, 03:30 PM   #4
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
BTW, here's a few links for you, of some things I reference:

http://itb.biologie.hu-berlin.de/~be...ware/bash.html
http://www.tldp.org/LDP/abs/html/

and, regexp substitution, etc:
http://www.tldp.org/LDP/abs/html/par...stitution.html
This last one is a great reference to show how powerful bash scripting can really be.
 
Old 03-25-2005, 04:46 PM   #5
lbauer
LQ Newbie
 
Registered: Mar 2005
Distribution: Mac with X11
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for the bracket suggestion.

This works:

echo -n "Who's the best College basketball team in the nation? "
read team

if [[ $team == *llin* ]]
then
echo "Go Illini, #1!!"
else
echo "Boo! That's not the best team"
echo "Go Illini!"
fi
 
  


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
$LINENO can't be modified in bash 3.0, while it can be in bash 2.05b Darwish Linux - Software 1 11-07-2005 02:57 PM
Why can't I use bash to do this ? naihe2010 Programming 5 10-30-2005 02:31 PM
bash help brianbek Linux - General 2 04-03-2005 06:03 PM
bash + html + javascript or just bash ? rblampain Programming 4 12-01-2004 07:53 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

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

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