0

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.

madu
  • 5,232
  • 14
  • 56
  • 96
  • 1
    `echo -n "$deployment_status" | hexdump -v -e '/1 "%02X"'`? – Darkman Sep 20 '22 at 06:27
  • @Darkman Thank you. This is the `hexdump`: `225052494D41525922` – madu Sep 20 '22 at 06:32
  • 1
    `echo -e '\x22\x50\x52\x49\x4D\x41\x52\x59\x22'` gives you `"PRIMARY"`. See? there are double quotes in that variable, but you only check it without the quotes. Thus `'"PRIMARY"'` is the one you're looking for. – Darkman Sep 20 '22 at 06:35
  • @Darkman Thank you so much for taking the time to find the issue. After removing the double quotes it worked. Thanks for saving my day. – madu Sep 20 '22 at 06:43
  • While the duplicate is apparently incorrect, we don't reopen unclear or unreproducible questions. Please review the [help] and in particular [How to ask](/help/how-to-ask) as well as the guidance for providing a [mre]. – tripleee Sep 20 '22 at 06:43
  • I was not the user who closed your question. (Also, neither I nor the closer is a moderator on this site.) Who's hurrying to conclusions here? – tripleee Sep 20 '22 at 06:50

0 Answers0