英文:
in a Vagrantfile, I define a local variable. How can pass it to a bash script as a parameter? $var, #var, #{var}?
问题
在一个 Vagrantfile 中,我想要运行脚本。
但其中一部分脚本不应在 provision 模式下运行。
我设置了一个本地变量 update = "true"
(或 false),取决于 Vagrant 命令的值。
一个 puts
显示它的值是 false
,例如,
但然后,我无法将其内容发送到一个 bash 脚本中作为参数。
我尝试过 $update
、#update
、#{update}
、update
,但都没有成功
在这一行上 bash common/300_Kafka.sh "3.4.0" "2.12" $update
Vagrantfile 的一部分:
if ARGV[0] == "provision"
update = "true"
else
update = "false"
end
puts "Vagrant box. Démarrage en mode mise à jour : " + update
config.vm.provision "file", source: "../common", destination: "common"
config.vm.provision "shell", privileged: false, inline: <<-SHELL
bash common/050_Certificats.sh
bash common/070_Utilitaires.sh
bash common/100_JDK.sh "17"
bash common/150_Postgresql-Postgis.sh "15" "3" "postgres"
bash common/200_NodeJS-Angular.sh "16"
bash common/250_ELK.sh "7.x"
bash common/300_Kafka.sh "3.4.0" "2.12" $update
SHELL
控制台上的消息:
Vagrant box. Démarrage en mode mise à jour : false
default: Indiquez si Kafka est en installation initiale (false) ou update (true). n'est pas accepté.
调用的 bash 脚本,测试值:
#!/bin/bash
# $1: Version de Kafka [3.4.0]
# $2: Version de Scala associée [2.12]
# $3: Mode update (true) ou installation initiale (false)
echo "300 : Installation de Kafka $1 basée sur Scala $2"
export DEBIAN_FRONTEND=noninteractive
if [ -z "$1" ]; then
echo "Précisez la version de Kafka à installer. [Recommandée : 3.4.0]" >&2;
exit 1;
fi
if [ -z "$2" ]; then
echo "Précisez la version de Scala liée. [Recommandée : 2.12]" >&2;
exit 1;
fi
if [ "$3" != "true" ] && [ "$3" != "false" ]; then
echo "Indiquez si Kafka est en installation initiale (false) ou update (true). $3 n'est pas accepté." >&2;
exit 1;
fi
update=$3
if [ "$update" == "false" ]; then
cd /tmp && wget -q "https://downloads.apache.org/kafka/$1/kafka_$2-$1.tgz"
sudo tar -xf "kafka_$2-$1.tgz" -C /opt
echo "export KAFKA_HOME=/opt/kafka_$2-$1" >> ~/.profile
else
echo "En mode update, Kafka ne se réinstallera pas."
fi
英文:
In a Vagrantfile, I want to run scripts.
But some part of them shouldn't run in provision mode.
I've sat a local variable update = "true"
(or false) depending of the value of the Vagrant command.
A puts
shows me that it is valued by "false"
, by example,
but then, I'm unable to send its content to a bash script, for parameter.
I've tried $update
, #update
, #{update}
, update
without success
on the line bash common/300_Kafka.sh "3.4.0" "2.12" $update
Part of the Vagrantfile:
if ARGV[0] == "provision"
update = "true"
else
update = "false"
end
puts "Vagrant box. Démarrage en mode mise à jour : " + update
config.vm.provision "file", source: "../common", destination: "common"
config.vm.provision "shell", privileged: false, inline: <<-SHELL
bash common/050_Certificats.sh
bash common/070_Utilitaires.sh
bash common/100_JDK.sh "17"
bash common/150_Postgresql-Postgis.sh "15" "3" "postgres"
bash common/200_NodeJS-Angular.sh "16"
bash common/250_ELK.sh "7.x"
bash common/300_Kafka.sh "3.4.0" "2.12" $update
SHELL
Message on console:
Vagrant box. Démarrage en mode mise à jour : false
default: Indiquez si Kafka est en installation initiale (false) ou update (true). n'est pas accepté.
The bash script called, testing the values:
#!/bin/bash
# $1: Version de Kafka [3.4.0]
# $2: Version de Scala associée [2.12]
# $3: Mode update (true) ou installation initiale (false)
echo "300 : Installation de Kafka $1 basé sur Scala $2"
export DEBIAN_FRONTEND=noninteractive
if [ -z "$1" ]; then
echo "Précisez la version de Kafka à installer. [Recommandée : 3.4.0]" >&2;
exit 1;
fi
if [ -z "$2" ]; then
echo "Précisez la version de Scala liée. [Recommandée : 2.12]" >&2;
exit 1;
fi
if [ "$3" != "true" ] && [ "$3" != "false" ]; then
echo "Indiquez si Kafka est en installation initiale (false) ou update (true). $3 n'est pas accepté." >&2;
exit 1;
fi
update=$3
if [ "$update" == "false" ]; then
cd /tmp && wget -q "https://downloads.apache.org/kafka/$1/kafka_$2-$1.tgz"
sudo tar -xf "kafka_$2-$1.tgz" -C /opt
echo "export KAFKA_HOME=/opt/kafka_$2-$1" >> ~/.profile
else
echo "En mode update, Kafka ne se réinstallera pas."
fi
答案1
得分: 2
我的发送参数到shell脚本的方式不正确。它似乎可以工作,但不完美,出现在inline << -SHELL
中。
相反,我发现自己在这方面成功:
if ARGV[0] == "provision"
update = "true"
else
update = "false"
end
puts "Vagrant box. Démarrage en mode mise à jour : " + update
config.vm.provision "file", source: "../common", destination: "common"
config.vm.provision "shell", privileged: false, path: "../common/050_Certificats.sh"
config.vm.provision "shell", privileged: false, path: "../common/070_Utilitaires.sh"
config.vm.provision "shell", privileged: false, path: "../common/100_JDK.sh", args: ["17"]
config.vm.provision "shell", privileged: false, path: "../common/150_Postgresql-Postgis.sh", args: ["15", "3", "postgres"]
config.vm.provision "shell", privileged: false, path: "../common/200_NodeJS-Angular.sh", args: ["16"]
config.vm.provision "shell", privileged: false, path: "../common/250_ELK.sh", args: ["7.x"]
config.vm.provision "shell", privileged: false, path: "../common/300_Kafka.sh", args: ["3.4.0", "2.12", "#{update}"]
英文:
My way of sending parameters to shell script isn't correct.
It looks working but doesn't perfectly, inside of an inline <<-SHELL
.
I found myself successful with that, instead:
if ARGV[0] == "provision"
update = "true"
else
update = "false"
end
puts "Vagrant box. Démarrage en mode mise à jour : " + update
config.vm.provision "file", source: "../common", destination: "common"
config.vm.provision "shell", privileged: false, path: "../common/050_Certificats.sh"
config.vm.provision "shell", privileged: false, path: "../common/070_Utilitaires.sh"
config.vm.provision "shell", privileged: false, path: "../common/100_JDK.sh", args: ["17"]
config.vm.provision "shell", privileged: false, path: "../common/150_Postgresql-Postgis.sh", args: ["15", "3", "postgres"]
config.vm.provision "shell", privileged: false, path: "../common/200_NodeJS-Angular.sh", args: ["16"]
config.vm.provision "shell", privileged: false, path: "../common/250_ELK.sh", args: ["7.x"]
config.vm.provision "shell", privileged: false, path: "../common/300_Kafka.sh", args: ["3.4.0", "2.12", "#{update}"]
答案2
得分: 1
我会尝试执行以下命令:
因为根据错误信息,它似乎明确期望字符串 "true"
或 "false"
。
英文:
I would try
bash common/300_Kafka.sh "3.4.0" "2.12" "#{update}"
because from the error message it seems like it explicitly expects the string "true"
or "false"
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论