Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

bash script compare string to asterisk character - how to?

Posted on 2003-10-28
4
Medium Priority
?
5,505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 7

Accepted Solution

by:
glassd earned 900 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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