How to create a while loop with if else statement inside local-exec inline command

Hi,
I am trying to run bash script from inside local-exec. but I have noticed that terraform will stuck forever in the loop, and if I add if statement to check the status it will always return true!
Here is an example of what I am trying to achieve :

,,,,,
resource "null_resource" "runjob" {
  provisioner "local-exec" {
    #command = "chmod +x ${path.module}/runjob.sh ; ${path.module}/runjob.sh" 
    
    command = <<EOT
    
    #!/bin/bash
    set -ev
## Code removed #########
while [ $RUN_STATE=="Run" -o $RUN_STATE=="Pending" ]
    do
        sleep 30
        RUN_STATE=$(bedo get job status| jq -r ".state.life_cycle_state")
        echo "Waiting for Job to complete... Current state is: $RUN_STATE"
    done
   
    # Check if job succeeded
    RESULT_STATE=$(bedo get job status | jq -r ".state.result_state")
    (echo "Result state of Job: $RESULT_STATE")

    if [ $RESULT_STATE=="SUCCESS" -o $RESULT_STATE=="TERMINATED" ]; then
        (echo "Job succeeded!"); exit 0;
    else
        (echo "Job failed!"); exit 0; 
    fi
 
    EOT
  }
}

so basically I want to run while loop & if statement
thank you in advance for your support.

command is supposed to be just one command, not a script.

Save the script to a separate file, and make command just a command running it by name.

This syntax is invalid, it is mandatory to use spaces around operators in this kind of expression.

It is unnecessary (and inefficient) to wrap simple echo statements in a subshell.

Thanks @maxb for your reply, i tried to run it from .sh file, but it gave me the same result
also using spaces between the operator will lead to this error “[: Pending unexpected operator” hence I have removed the spaces

so, is there any specific syntax for the while loop & IF statement

No, the spaces are definitely required. [ is an odd command with some difficult syntax quirks, and it sounds like that’s where the problem is.

1 Like

ok, noted

could you please share any sample code for the while loop & IF statement

I would simply post your own code back to you with the spaces added around each == …

Please show what exactly you changed it to, and confirm the error you see seeing.

I thought of another thing that could trip you up, as well…

As previously mentioned, the local-exec provisioner’s command argument is built to take a single command, not a script.

As a result, your line:

has no function, being read as just a simple comment. In particular, it does not cause the use of bash - instead you get whatever the system default /bin/sh is, which might be bash, or it might not.

The use of the == operator with [ is a bash extension, so may be read as an error. (The correct standard equality operator in [ syntax is just =.)

You may also experience other problems, if you have bash-specific code in the part of your script that you did not post.

Here is what i have tried

# While job is not finished keep checking run state
        while :
            do
                RUN_STATE=$(bedo get job status | jq -r ".state.life_cycle_state")
                echo "in loop"
                
                if  [ $RUN_STATE="RUNNING" -o $RUN_STATE="PENDING" ]; then
                    echo "Waiting for Job to complete... Current state is: $RUN_STATE"
                else
                    echo 'Done!'
                    break
                fi
                sleep 20
        done

Output - it will not exit from the loop

image

meanwhile, if I add space like this

        while :
            do
                RUN_STATE=$(bedo get job status | jq -r ".state.life_cycle_state")
                echo "in loop"
                
                if  [ $RUN_STATE == "RUNNING" -o $RUN_STATE == "PENDING" ]; then
                    echo "Waiting for Job to complete... Current state is: $RUN_STATE"
                else
                    echo 'Done!'
                    break
                fi
                sleep 20
        done

Output:
image

The next problem to address is this one:

Change the == for =. As per my previous post, it seems you are indeed on a system where /bin/sh is not bash - the observed behaviour matches that from the shell dash commonly found on Debian/Ubuntu and similar.

1 Like

thanks @maxb

issue solved by using this syntax

if  [ "$RUN_STATE" = "RUNNING" -o "$RUN_STATE" = "PENDING" ]; then

Hi, I also have a problem with that loop, but when I run it in an ec2 manually, the loop works fine but when I implement it in terraform in a .sh file and deploy it, the ec2 does not let me enter the console. I do not know if it has to do with that loop or something else.

#!/bin/bash

while true; do
    container_id=$(docker ps -aqf 'name=frdb')

    if [ -n "$container_id" ]; then
        if [ "$(docker inspect -f '{{.State.Health.Status}}' "$container_id")" = 'healthy' ]; then
            echo 'El contenedor está listo'
            sudo mkdir -p /tmp/holamundo
            break
        else
            echo 'El contenedor no está listo, por favor espere...'
        fi
    else
        echo 'Esperando que el contenedor esté disponible...'
    fi
    sleep 30
done

docker

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.