bash script compare string to asterisk character - how to?

Hi all,

I want to test whether a string is the asterisk character:

if [ $thestring == '*' ]
then
  .. do stuff ..
fi

the asterisk is read into the script by redirect ie:

while read thestring anotherstring etc

and i run the script like:

myscript < datafile

i have tried various things like:

if [ $thestring == '\*' ]

if [ $thestring == * ]

if [ $thestring == \* ]

etc, but nothing seems to work.  the comparison never seems to recognise when the string really *is* an asterisk.

hope someone can help.

regards,  Mike.

LVL 37
meverestAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

glassdCommented:
if [[ $thestring = '*' ]]

Single = is the string equality test. Use [[ .. ]] rather than [ .. ] to invoke the internal test function rather than the external test (more efficient). single quotes around the asterisk prevent it from being expanded by the shell.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
meverestAuthor Commented:
what a shame - that doesn't work either.

here's my full script:

#! /bin/bash

CHAIN=""

while read TARGET PROT OPT SOURCE DESTINATION SRCPORT DIR DESTPORT
do
        echo "$TARGET $PROT $SOURCE $DESTINATION $SRCPORT $DESTPORT"
        LINE=""
        if [ $TARGET = "Chain" ]
        then
                CHAIN=$PROT
                echo "got chain $PROT"
        else
                if [ $TARGET = "target" ]
                then
                else
                        LINE="ipchains -A $CHAIN"
                        if [ $PROT = "all" ]
                        then
                        else
                                LINE="$LINE -p $PROT"
                        fi
                        LINE="$LINE -s $SOURCE"

                        if [ $PROT = "all" ]
                        then
                        else
                                if [[ $SRCPORT = '*' ]]
                                then
                                else
                                        LINE="$LINE $SRCPORT"
                                fi
                        fi
                        LINE="$LINE -d $DESTINATION"

                        if [ $PROT = "all" ]
                        then
                        else
                                if [[ $DESTPORT = '*' ]]
                                then
                                else
                                        LINE="$LINE $DESTPORT"
                                fi
                        fi
                        LINE="$LINE -j $TARGET"
                        echo "$LINE"
                        $LINE
                fi
        fi
done

and here is a sample of the input file:

ACCEPT     tcp  ------  0.0.0.0/0             209.23.178.0/24       * ->   80
ACCEPT     tcp  ------  0.0.0.0/0             209.23.178.0/24       * ->   443
ACCEPT     tcp  ------  0.0.0.0/0             209.23.178.11         * ->   25
ACCEPT     tcp  ------  0.0.0.0/0             209.23.178.13         * ->   110
ACCEPT     all  ------  202.0.85.34           209.23.178.16         n/a
DENY       tcp  ------  0.0.0.0/0             209.23.178.0/24       * ->   389
DENY       tcp  ------  0.0.0.0/0             209.23.178.0/24       * ->   1433
ACCEPT     all  ------  0.0.0.0/0             209.23.178.10         n/a
ACCEPT     all  ------  0.0.0.0/0             209.23.178.18         n/a
ACCEPT     all  ------  0.0.0.0/0             0.0.0.0/0             n/a
ACCEPT     all  ------  202.0.85.34           209.23.178.16         n/a
DENY       all  ------  0.0.0.0/0             202.99.250.207         n/a
ACCEPT     all  ------  203.202.41.80         202.99.250.207         n/a
DENY       tcp  ------  0.0.0.0/0             202.99.248.211         443 ->   *
DENY       tcp  ------  0.0.0.0/0             202.99.250.211         443 ->   *
DENY       tcp  ------  0.0.0.0/0             202.99.250.211         80 ->   *
DENY       tcp  ------  0.0.0.0/0             202.99.248.211         80 ->   *
DENY       all  ------  0.0.0.0/0             202.99.250.204         n/a
DENY       all  ------  0.0.0.0/0             202.99.248.204         n/a
DENY       all  ------  0.0.0.0/0             202.99.248.207         n/a
DENY       all  ------  0.0.0.0/0             202.99.248.205         n/a

i wonder if you get the same problem, eg:

DENY all 0.0.0.0/0 202.7.248.206 n/a
ipchains -A VicOne -s 0.0.0.0/0 -d 202.7.248.206 -j DENY
DENY tcp 0.0.0.0/0 202.7.250.235 80 *
[[: not found
[[: not found
ipchains -A VicOne -p tcp -s 0.0.0.0/0 80 -d 202.7.250.235 * -j DENY
Try `ipchains -h' or 'ipchains --help' for more information.
DENY tcp 0.0.0.0/0 202.7.250.235 443 *
[[: not found
[[: not found
ipchains -A VicOne -p tcp -s 0.0.0.0/0 443 -d 202.99.250.235 * -j DENY
Try `ipchains -h' or 'ipchains --help' for more information.
DENY tcp 0.0.0.0/0 202.99.248.235 443 *
[[: not found
[[: not found
ipchains -A VicOne -p tcp -s 0.0.0.0/0 443 -d 202.99.248.235 * -j DENY
Try `ipchains -h' or 'ipchains --help' for more information.
DENY tcp 0.0.0.0/0 202.99.248.235 80 *
[[: not found
[[: not found
ipchains -A VicOne -p tcp -s 0.0.0.0/0 80 -d 202.99.248.235 * -j DENY
Try `ipchains -h' or 'ipchains --help' for more information.
ACCEPT all 0.0.0.0/0 202.99.250.254 n/a
ipchains -A VicOne -s 0.0.0.0/0 -d 202.99.250.254 -j ACCEPT
ACCEPT all 0.0.0.0/0 202.99.250.253 n/a
ipchains -A VicOne -s 0.0.0.0/0 -d 202.99.250.253 -j ACCEPT
ACCEPT all 0.0.0.0/0 202.99.250.252 n/a
ipchains -A VicOne -s 0.0.0.0/0 -d 202.99.250.252 -j ACCEPT
ACCEPT all 0.0.0.0/0 202.99.250.251 n/a

btw - this is LRP linux:

bean# cat /proc/version
Linux version 2.2.16 (root@Zen-Machine) (gcc version 2.7.2.3) #1 Sun Jul 16 18:29:35 EDT 2000

not even sure if it is a real bash(?)  come to think of it, the only reason i started writing this script is because someone elses ipchains-restore didn;t work (maybe for the same reason?)

thanks for the response, upped points for your trouble.

cheers!
0
meverestAuthor Commented:
hey - i got the answer:

if [ "$DESTPORT" = '*' ]

i guess when $DESTPORT is an asterisk, the interpreter evaluated it out to the end.

it's just too smart!

;-)

all i ask is a comment for the points.

cheers!
0
meverestAuthor Commented:
oh well,

it wasn't the solution, but at least you responded ;-)

thanks!  Mike.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.