LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to Go Illini in Bash (https://www.linuxquestions.org/questions/programming-9/how-to-go-illini-in-bash-306018/)

lbauer 03-25-2005 02:51 PM

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?

saravkrish 03-25-2005 03:05 PM

Use perl. It's a whole lot easier for such regular expression comparisons.

-Sarav

TheLinuxDuck 03-25-2005 03:15 PM

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!

TheLinuxDuck 03-25-2005 03:30 PM

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.

lbauer 03-25-2005 04:46 PM

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


All times are GMT -5. The time now is 11:57 AM.