Search code examples
pythonshelldockerentry-point

Shell scripting - how to call python shell commands in if statement


I'm currently writing on my docker-entrypoint.sh script. The script determines a service_type which is a env. variable depending on the returned value the actual application start will differ.

within the if statement I have several commands to check if the landscape is ready for useage. I'm also using Python here but always running into the follwoing issue:

/usr/local/bin/docker-entrypoint.sh: line 128: warning: here-document at line 30 delimited by end-of-file (wanted `EOF')

/usr/local/bin/docker-entrypoint.sh: line 129: syntax error: unexpected end of file

docker-entrypoint.sh:

#!/usr/bin/env bash

############### App ###############

if [ "$SERVICE_TYPE" = "app" ]
then
  echo "I'm a Application instance, Hello World!"

  ...

  echo "Checking if System User is setup"
  {
  python manage.py shell <<-EOF
  from django.contrib.auth import get_user_model

  User = get_user_model()  # get the currently active user model
  User.objects.filter(user='$SYS_USER').exists() or User.objects.create_superuser('$SYS_USER', '$SYS_USER')
  EOF
  }

  ...

############### Celery Worker ###############

elif [ "$SERVICE_TYPE" = "celery-worker" ]
then
  echo "I'm a Celery Worker instance, Hello World!"

  ...

############### Celery Beat ##################

elif [ "$SERVICE_TYPE" = "celery-beat" ]
then
  echo "I'm a Celery Beat instance, Hello World!"
  ...


fi

How can I execute my python shell cmd within the if statement so that I basically have the same result as If i wouldn't use it in a if statement like so:

echo "Checking if System User is setup"
{
cat <<EOF | python manage.py shell
from django.contrib.auth import get_user_model

User = get_user_model()  # get the currently active user model
User.objects.filter(user='$SYS_USER').exists() or User.objects.create_superuser('$SYS_USER', '$SYS_USER')
EOF
}

Solution

  • Here-doc end-tokens can't have leading white space. Also your script inside the heredoc shouldn't have any leading whitespace

    #!/usr/bin/env bash
    
    ############### App ###############
    
    if [ "$SERVICE_TYPE" = "app" ]
    then
      echo "I'm a Application instance, Hello World!"
    
      echo "Checking if System User is setup"
      {
      python manage.py shell <<-EOF
    from django.contrib.auth import get_user_model
    
    User = get_user_model()  # get the currently active user model
    User.objects.filter(user='$SYS_USER').exists() or User.objects.create_superuser('$SYS_USER', '$SYS_USER')
    EOF
      }
    
    ############### Celery Worker ###############
    
    elif [ "$SERVICE_TYPE" = "celery-worker" ]
    then
      echo "I'm a Celery Worker instance, Hello World!"
    
    ############### Celery Beat ##################
    
    elif [ "$SERVICE_TYPE" = "celery-beat" ]
    then
      echo "I'm a Celery Beat instance, Hello World!"
    fi