0
CIDR=$(echo "$DESCRIBE_VPC" | $JQ -r '.Vpcs[0].CidrBlock')
DEN_PARAM=$(aws ssm get-parameters --names "$DEN" --region $REGION)
GET_PARAM_VALUE=$(echo $PARAM | jq -r '.Parameters[].Value' | tr '[:upper:]' '[:lower:]')

DS_HOST=$(nslookup dsaws.com)
DS_STATUS=$?

if [ "$CIDR" == *"100."* ] && [ "$DS_STATUS" == 0 ] ## Ex: 100.1.172.1 /172.1.100.1
then
    retrieveAccInfo
elif [ "$CIDR" == *"172."* ] && [ "$DS_STATUS" != 0 ]
then
    retrieveAccInfo
fi

In Above example, I am trying to match substring "100." and "172." with a retrieved IP address. The above condition matches but if I get an IP: 172.1.1.100 that matches both conditions. What if I want IP address that exactly starts with 100 and 172 to match with the IPs, but not anywhere else in the string(IP).

2
  • Try using regex matching. Use the pattern to check only for 100/172 in the starting. Something like ^[100|172].[0-9]+.[0-9]+. stackoverflow.com/questions/21112707/… Commented Oct 2, 2020 at 17:45
  • That's the really good example @Ankush Commented Oct 2, 2020 at 18:24

2 Answers 2

1

Try below:

for matching the IPs starting with 100: [[ "$CDR" =~ ^100.* ]] , and for matching the IPs starting with 172: [[ "$CDR" =~ ^172.* ]]

It's advisable to use if [[ condition ]] ,i.e double square brackets for if condition while using bash.

Sign up to request clarification or add additional context in comments.

4 Comments

Actually - unsure that single brackets would even work. Double brackets are a bash / zsh exclusive feature. The regex comparison operator (=~) won't work in POSIX compliant shells (not-bash). You can get around this by requesting a bash or bash-compliant interpreter with a shebang.
@franklin: completely agree with you, the regex matching won't work with the single bracket , [[ is more modern and powerful. good details can be found on link: mywiki.wooledge.org/BashFAQ/031 . OP has tagged bash, so i believe it should work in this case
Awesome that worked. Yes, I just forgot to add [[]] in the example here which I always write in the regex. However, your suggested way worked for me. Much appreciated. @User123
@hpmistry19: Glad it worked! You can upvote and accept the answer if you found it useful...
0

case is often a good option.

Corrected to only match 172 on nonzero DS_STATUS:

for DS_STATUS in 0 1
do for CIDR in 100.2.3.4 172.2.3.4 1.2.3.100 1.2.3.172
do echo "CIDR=$CIDR, DS_STATUS=$DS_STATUS"
   case "$DS_STATUS/$CIDR" in
   0/100[.]*)  echo "match"                   ;;
   0/172[.]*)  echo "no match, DS_STATUS = 0" ;;
   */172[.]*)  echo "alternate match"         ;;
   *) echo "no match"                         ;;
   esac
done
done

CIDR=100.2.3.4, DS_STATUS=0
match
CIDR=172.2.3.4, DS_STATUS=0
no match, DS_STATUS = 0
CIDR=1.2.3.100, DS_STATUS=0
no match
CIDR=1.2.3.172, DS_STATUS=0
no match
CIDR=100.2.3.4, DS_STATUS=1
no match
CIDR=172.2.3.4, DS_STATUS=1
alternate match
CIDR=1.2.3.100, DS_STATUS=1
no match
CIDR=1.2.3.172, DS_STATUS=1
no match

So,

case "$DS_STATUS/$CIDR" in
0/100[.]*) retrieveAccInfo            ;; # primary match
0/172[.]*) : no match, DS_STATUS is 0 ;; 
*/172[.]*) retrieveAccInfo            ;; # alternate match
        *) : no match at all          ;; 
esac

1 Comment

Case seems a really nice option as well here also looks more mature. I was observing your code but couldn't figure out the second condition if "DS_STATUS" != 0. In your code it shows it's equal to zero but how do we write if it's not equal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.