Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Bash函数中使用局部变量_Bash - Fatal编程技术网

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

如何在Bash函数中使用局部变量,bash,Bash,我正在尝试编写一个Bash脚本,它将停止Docker容器的存储库,重新构建它们,并对它们运行一些测试(使用Pytest)。为了使代码干燥,我尝试定义一个函数wait\u for\u container,如下所示: docker stop $(docker ps -a -q) docker-compose build docker-compose up -d function wait_for_container { local CONTAINER=$1 local PORT

我正在尝试编写一个Bash脚本,它将停止Docker容器的存储库,重新构建它们,并对它们运行一些测试(使用Pytest)。为了使代码干燥,我尝试定义一个函数
wait\u for\u 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...
另一方面,以下非干式脚本确实有效:

和回声

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.
然后是测试结果。如何改进
wait\u for\u container
功能以达到相同的效果?

以下是和的评论,下面是经过修改的脚本:

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
变量设置为局部变量)。

粗略地看,我没有发现任何错误(您可能应该引用变量展开式,但您的示例数据在未引用展开式时似乎没有问题),奇怪的是,问题似乎出在调用
nc
实用程序上。能否将日志记录添加到脚本中,以便查看整个
nc
命令在执行时的实际外观?顺便说一句,使用函数避免代码重复是一种方法,没有理由不起作用,因此请花点时间解决这个问题(这可能只是一些小的、难以发现的问题,可能与使用函数无关).
直到nc-z$CONTAINER$PORT
===>
直到nc-z$ADDR$PORT
GrishaLevit是正确的。顺便说一句,您还应该将
ADDR
变量设置为本地变量,因为它当前正在泄漏到调用上下文中。作为一般代码实践,大写变量仅用于环境变量,而不是较低的CA为提高可读性,请编辑本地脚本变量。
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