簡體   English   中英

Shell 腳本 - 如何在 if 語句中調用 python shell 命令

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

我目前正在寫我的 docker-entrypoint.sh 腳本。 該腳本確定一個服務類型,它是一個環境。 變量取決於返回的值,實際應用程序啟動會有所不同。

在 if 語句中,我有幾個命令來檢查景觀是否可以使用。 我也在這里使用 Python 但總是遇到以下問題:

/usr/local/bin/docker-entrypoint.sh:第 128 行:警告:第 30 行的此處文檔由文件結尾分隔(需要“EOF”)

/usr/local/bin/docker-entrypoint.sh:第 129 行:語法錯誤:文件意外結束

碼頭入口點.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 結束標記不能有前導空格。 此外,heredoc 中的腳本不應有任何前導空格

#!/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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM