繁体   English   中英

在shell函数中传递参数的问题

[英]Problem with passing parameters in a shell function

我有以下 shell 脚本。 有一个部署函数,稍后在脚本中用于部署容器。

function deploy {

    if [[ $4 -eq QA ]]; then
        echo Building a QA Test container...
        docker create \
        --name=$1_temp \
        -e DATABASE=$3 \
        -e SPECIAL_ENV_VARIABLE \
        -p $2:9696 \ 
        ... # skipping other project specific settings
    else
        docker create \
        --name=$1_temp \
        -e DATABASE=$3 \
        -p $2:9696 \
       ... # skipping some project specific stuff
    fi

在部署期间,我必须对应用程序(在容器中)进行一些测试。 我为此使用了不同的容器,但是我需要将一个附加参数传递给我的 QA_test 容器的部署函数,因为我需要在 docker docker create中进行不同的设置。 因此,为什么我将 if 语句放在开头,它检查第 4 个参数是否等于“QA”,如果是,它会创建一个带有特殊环境变量的特定容器,否则如果它只有 3 个参数,它会创建一个“正常的'一个。 我能够使用两个单独的部署功能运行代码,但我想让我的代码更好地阅读。 无论如何,它应该是这样的:

Step 1: Normal tests: 
deploy container_test 9696 test_database # 3 parameters
run tests... (this is not relevant to the question)

Step 2: QA testing: 
deploy container_qa_test 9696 test_database QA # 4 parameters, so I can create a 
                                               # a special container
run tests... (again, not relevant to the question)

Step 3: If they are successful, deploy a production-ready container:
deploy production_container 9696 production_database # 3 parameters again

但是根据日志会发生什么:

Step 1: test_container is created. However its created with the upper if, but there is not a 4th parameter that equals QA, however it executes it.
Step 2: this runs normal.
Step 3: production container is built as a QA container

即使条件不满足,它也永远不会到达else部分。 谁能给我一些提示?

只需将[[ $4 -eq QA ]]更改为:

if [[ "$4" == "QA" ]]; then

-eq用于比较数字....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM