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).

hpmistry19
  • 33
  • 7
  • Try using regex matching. Use the pattern to check only for 100/172 in the starting. Something like `^[100|172].[0-9]+.[0-9]+`. https://stackoverflow.com/questions/21112707/check-if-a-string-matches-a-regex-in-bash-script/21112809 – Ankush Oct 02 '20 at 17:45
  • That's the really good example @Ankush – hpmistry19 Oct 02 '20 at 18:24

2 Answers2

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.

User123
  • 1,498
  • 2
  • 12
  • 26
  • 1
    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 Oct 02 '20 at 17:52
  • 1
    @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: http://mywiki.wooledge.org/BashFAQ/031 . OP has tagged `bash`, so i believe it should work in this case – User123 Oct 02 '20 at 18:00
  • 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 Oct 02 '20 at 18:23
  • @hpmistry19: Glad it worked! You can upvote and accept the answer if you found it useful... – User123 Oct 02 '20 at 18:27
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
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • 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. – hpmistry19 Oct 02 '20 at 18:49