[英]Passing a parameter's value to shell function prints only the name of the parameter
I need to pass a parameter to my shell function, which looks like this:我需要向我的 shell 函数传递一个参数,它看起来像这样:
function deploy {
docker create \
--name=$1_temp \
-e test_postgres_database=$2 \
-e test_publicAddress="http://${3}:9696"\
# other irrelevant stuff
I am passing the following parameters:我正在传递以下参数:
deploy test_container test_name #1 test_database #2 ip_address #3
So when, I pass those 3 parameters, based on them a new container is created.因此,当我传递这 3 个参数时,基于它们创建了一个新容器。 However, the third parameter is something special.
但是,第三个参数有些特殊。 So there is another function, which gets the ip of the container.
所以还有一个函数,就是获取容器的ip。
function get_container_ip_address {
container_id=($(docker ps --format "{{.ID}} {{.Names}}" | grep $1))
echo $(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${container_id[0]})
So, the execution of the deploy function actually looks like this:所以,deploy 函数的执行实际上是这样的:
ip_address=$(get_container_ip_address test_container)
deploy test_container test_database ip_address
Let's say the IP address of the container is 1.1.1.1, so the ip_address=1.1.1.1
.假设容器的 IP 地址是 1.1.1.1,所以
ip_address=1.1.1.1
。 However, when I execute the script and create the container, its IP address is: "http://ip_address:9696"
and not "http://1.1.1.1:9696"
.但是,当我执行脚本并创建容器时,它的 IP 地址是:
"http://ip_address:9696"
而不是"http://1.1.1.1:9696"
。
I also tried the following:我还尝试了以下方法:
...
-e test_publicAddress="http://$3:9696"\
...
But I still got the same result.但我还是得到了同样的结果。 Is there a way I can get the value of the passed parameter?
有没有办法获取传递参数的值? By the way, I am sure it contains the needed ip address as I use it elsewhere (not in a function) and I printed it for testing.
顺便说一句,我确定它包含所需的 IP 地址,因为我在其他地方(不是在函数中)使用它并打印它进行测试。 Thank you in advance!
先感谢您!
so run this like that:所以像这样运行:
ip_address=$(get_container_ip_address test_container)
deploy test_container test_database $ip_address
when you call it without the $
the script leave it alone like a string ip_address
当你在没有
$
的情况下调用它时,脚本将它像字符串ip_address
一样单独存在
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.