Bash compare strings not working inside of while loop

by James   Last Updated May 16, 2019 00:26 AM

I created this bash script and can't seem to figure out why my code inside of the if block isn't executing.

db_instances_status="creating" 
db_instances_status=$(makes api request to get value)

COUNTER=0
while [ $COUNTER -lt 1 ]; do
  db_instances_status=$(makes api request to get value)

  echo "$db_instances_status" # echos available

  if [ "available" = "$db_instances_status" ]; then
     # code never makes it here
     dosomething()
     break;
  fi
  sleep 30
done

I followed examples from this How to compare strings in Bash

and here https://tecadmin.net/tutorial/bash/examples/check-if-two-strings-are-equal/

Tags : bash ash


Answers 2


[deleted as requested by another user]

user11506911
user11506911
May 16, 2019 00:02 AM

You either are not getting to the if statement or else the variable doesn't contain what you think it does.

This snippet will help debug both...

while [ $COUNTER -lt 1 ]; do
  echo "[DEBUG] getting status"
  db_instances_status=$(makes api request to get value)
  echo "[DEBUG] X${db_instances_status}X"
  echo "$db_instances_status" # echos available

  if [ "available" = "$db_instances_status" ]; then
John3136
John3136
May 16, 2019 00:10 AM

Related Questions


Updated May 29, 2015 07:11 AM

Updated March 08, 2017 22:26 PM

Updated April 08, 2017 14:26 PM

Updated February 09, 2018 05:26 AM

Updated April 04, 2015 02:11 AM