簡體   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