Bash比较while循环内的字符串不起作用

时间:2019-05-15 23:49:39

标签: bash ash

我创建了这个bash脚本,似乎无法弄清楚为什么if块中的代码没有执行。

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

我遵循了How to compare strings in Bash

中的示例

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

1 个答案:

答案 0 :(得分:1)

您要么没有进入if语句,要么变量不包含您认为的内容。

此代码段将帮助调试两个...

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