Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5537
  • Last Modified:

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.

0
meverest
Asked:
meverest
  • 3
1 Solution
 
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
 
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

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now