vendredi 2 octobre 2015

How do you compare the output of a command with a string variable?

[root@ip-10-10-61-178 ~]# cat if.sh 
#!/bin/bash

VPCCIDR=`curl -s http://ift.tt/1mDJdOy | cut -c 1-5`

I am trying to achieve the following with bash but am having trouble:

  1. curl the AWS instance data URL and return the beginning of the local IP. e.g 10.10 for test or 10.20 for prod
  2. If the output of the curl equals to the variable called $TESTENV then print "This is Test" else if it is equal to the variable called $PRODENV then print "This is Prod. If it matches none then echo "no match".

The problem I have is that my script just echoes "This is Test" regardless of what values I set for TESTENV and PRODENV.

#!/bin/bash

#Find out what VPC we are in. e.g 10.10 or 10.50
VPCCIDR=`curl -s http://ift.tt/1mDJdOy | cut -c 1-5`

TESTENV="10.10"
PRODENV="10.50"

if [ "$VPCCIDDR" != "$TESTENV" ]; then  
echo "This is Test"
elif [ "$VPCCIDDR" != "$PRODENV" ]; then
echo "This is Prod"
else
echo "no match"
fi




Aucun commentaire:

Enregistrer un commentaire