Shell script IF condition equality check not working for string returned from function

Viewed 28

So I am facing a rather annoying problem. I have two files: check_ongoing_deployment.sh

#!/usr/bin/env bash

check_ongoing_deployment() {
  deploy_status=$(get_deploy_state "node-server")
  echo "${deploy_status}"
}

main.sh

source "$(dirname "$0")/check_ongoing_deployment.sh"
    deployment_status=$(check_ongoing_deployment)
    echo "Deployment status: $deployment_status"
    if [[ ${deployment_status} == "PRIMARY" ]]; then
      echo "No deployment in progress for ${service_to_wait}."
    else
      echo "Deployment on going for ${service_to_wait}. Waiting for deployment to complete."
    fi

The problem is the IF condition in main.sh is not working. The echo shows deployment_status is in fact "PRIMARY", but its not getting equated to the string "PRIMARY".

However, when I change check_ongoing_deployment.sh to just return a string like:

 #!/usr/bin/env bash
    
    check_ongoing_deployment() {
      deploy_status=$(get_deploy_state "node-server")

      # echoing String
      echo "PRIMARY"
    }

The if condition in main.sh is working.

So it looks like it has something to with the getting the returned value from the function, but I cannot figure out what the if condition is missing, as the returned value seems identical string.

EDIT: I have tried the following and still having the issue:

deployment_status=$(check_ongoing_deployment)
status=$(echo $deployment_status | tr -d '\r')
#status=$(echo $deployment_status | sed -i 's/\r$//')

if [[ $status == "PRIMARY" ]]; then

PS: Why are people is such a hurry to close questions? Did you check the script yourself and confirmed that it is indeed a duplicate and the solution works? This is more annoying than the issue.

0 Answers
Related