Solved

bash script compare string to asterisk character - how to?

Posted on 2003-10-28
4
5,375 Views
Last Modified: 2012-06-27
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.

0
Comment
Question by:meverest
  • 3
4 Comments
 
LVL 7

Accepted Solution

by:
glassd earned 300 total points
ID: 9632663
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
 
LVL 37

Author Comment

by:meverest
ID: 9633322
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
 
LVL 37

Author Comment

by:meverest
ID: 9633387
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
 
LVL 37

Author Comment

by:meverest
ID: 9792266
oh well,

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

thanks!  Mike.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now