Solved

bash script compare string to asterisk character - how to?

Posted on 2003-10-28
4
5,394 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
linux rpm install 5 202
install command in linux 1 44
RHEL 7 , I have tftp running as a process how can I run it as a service 2 77
Error while installing rpm 1 77
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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