1

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/

5
  • Add a line above your if statement that says db_instances_status='available'. Now does your code enter the if section? If it does then db_instances_status does NOT just contain available in your surrounding code. Commented May 15, 2019 at 23:53
  • Try echo "X${db_instances_status}X" in case the status has leading/trailing spaces. Commented May 15, 2019 at 23:53
  • I get X"available"X Commented May 16, 2019 at 0:06
  • The issue was the actual value is quoted. "available" Commented May 16, 2019 at 0:11
  • Smh, it's always something minor. Commented May 16, 2019 at 0:12

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for helping me debug. The actual value was being returned in quotes. it didn't execute because "available" doesn't equal available

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.