如何在Bash函数中使用局部变量

库尔特·皮克

我正在尝试编写一个Bash脚本,该脚本将停止Docker容器的存储库,重新构建它们,并对它们运行一些测试(使用Pytest)。为了使代码干燥,我尝试定义一个函数wait_for_container,如下所示:

docker stop $(docker ps -a -q)

docker-compose build

docker-compose up -d

function wait_for_container {
    local CONTAINER=$1
    local PORT=$2

    ADDR=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER)

    until nc -z $CONTAINER $PORT
    do
        echo "Waiting for the $CONTAINER container..."
        sleep 0.5
    done
    echo "$CONTAINER listening at $ADDR:$PORT"
}

RETHINKDB_CONTAINER=ipercroncompose_rethinkdb_1
RETHINKDB_PORT=28015
wait_for_container $RETHINKDB_CONTAINER $RETHINKDB_PORT

RABBITMQ_CONTAINER=ipercroncompose_rabbitmq_1
RABBITMQ_PORT=5672
wait_for_container $RABBITMQ_CONTAINER $RABBITMQ_PORT

cd test
pytest

但是,我发现这行不通:我反复得到

nc: getaddrinfo: Temporary failure in name resolution
Waiting for the ipercroncompose_rethinkdb_1 container...

另一方面,以下非DRY脚本可以正常工作:

docker stop $(docker ps -a -q)

docker-compose build

docker-compose up -d

RETHINKDB_CONTAINER=ipercroncompose_rethinkdb_1
RETHINKDB_ADDR=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $RETHINKDB_CONTAINER)
RETHINKDB_PORT=28015

until nc -z $RETHINKDB_ADDR $RETHINKDB_PORT
do
    echo "Waiting for the RethinkDB container..."
    sleep 0.5
done
echo "RethinkDB listening at ${RETHINKDB_ADDR}:${RETHINKDB_PORT}."

RABBITMQ_CONTAINER=ipercroncompose_rabbitmq_1
RABBITMQ_ADDR=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $RABBITMQ_CONTAINER)
RABBITMQ_PORT=5672

until nc -z $RABBITMQ_ADDR $RABBITMQ_PORT
do
    echo "Waiting for the RabbitMQ container..."
    sleep 0.5
done
echo "RabbitMQ listening at ${RABBITMQ_ADDR}:${RABBITMQ_PORT}."

cd test
pytest

和回声

RethinkDB listening at 172.18.0.2:28015.
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
Waiting for the RabbitMQ container...
RabbitMQ listening at 172.19.0.2:5672.

其次是Pytests的结果。如何改善wait_for_container功能以达到相同的效果?

库尔特·皮克

Grisha LevitFred的评论之后,以下是改编的脚本:

docker stop $(docker ps -a -q)
docker-compose build
docker-compose up -d

function wait_for_container {
    local CONTAINER=$1
    local PORT=$2

    local ADDR=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER)
    echo $ADDR

    until nc -z $ADDR $PORT
    do
        echo "Waiting for the $CONTAINER container..."
        sleep 0.5
    done
    echo "$CONTAINER listening at $ADDR:$PORT"
}

wait_for_container ipercroncompose_rethinkdb_1 28015
wait_for_container ipercroncompose_rabbitmq_1 5672

cd test
pytest

问题的确确实是netcat需要IP地址作为其第一个输入,而不是Docker容器名称。(我也将ADDR变量设置为局部变量)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在不经常声明的情况下在经常使用的函数中使用局部变量?

如何在C中的另一个函数中使用局部变量的值

Lua - 如何在函数中使用局部变量(图像 URL)来打印图像 (loadRemoteImage)?

CompletableFuture - 如何在未来结果中使用局部变量

如何在SELECT FROM中使用局部变量?

如何在 JSX 中的 HTML 中使用局部变量?

如何在Oracle SQL中使用局部变量?

在使用haxe宏构建的函数中使用局部变量

在包装函数和局部变量破坏中使用setjmp

在回调函数中使用局部变量

如何在Bash中增加局部变量?

如何在主函数内部创建局部变量?

如何在函数外部访问局部变量

如何在Python中的函数中修改局部变量?

node.js 如何在 for 循环中使用局部变量实现承诺感知?

AWS CloudFormation UserData:如何在脚本中使用局部变量

如何在其他功能烧瓶中使用局部变量?

如何在循环中使用 Func 或 Action 而不是局部变量?

如何在SQL小提琴查询中使用局部变量

MySQL:如何在存储过程中使用select设置局部变量?

如何使用内置的局部变量参数编写函数?

如何在闭包中使用var局部变量,以防止编译器对此变量进行智能广播?

如何更改返回局部变量的函数

函数局部变量的返回如何工作?

使用局部变量更新函数

在函数之外使用局部变量

当函数具有同义局部变量时,如何在函数中获取全局变量?

在 angular 中,我们如何在 ngFor 循环中定义的 ngIf 中使用局部变量?

在主函数的一个函数中使用局部变量