Shell 脚本 - 如何在 if 语句中调用 python shell 命令

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

我目前正在编写 docker-entrypoint.sh 脚本。该脚本确定一个 service_type 是一个环境。取决于返回值的变量实际应用程序启动会有所不同。

在 if 语句中,我有几个命令来检查景观是否已准备好使用。 我在这里也使用 Python 但总是 运行 进入以下问题:

/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

如何在 if 语句中执行我的 python shell cmd,以便我基本上得到与如果我不会在 if 语句中使用它相同的结果:

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
}

Here-doc 结束标记不能有前导白色 space。此外,您在 heredoc 中的脚本不应有任何前导 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