in a Vagrantfile, I define a local variable. How can pass it to a bash script as a parameter? $var, #var, #{var}?

huangapple go评论49阅读模式
英文:

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 = &quot;true&quot; (or false) depending of the value of the Vagrant command.

A puts shows me that it is valued by &quot;false&quot;, 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 &quot;3.4.0&quot; &quot;2.12&quot; $update

Part of the Vagrantfile:

if ARGV[0] == &quot;provision&quot;
  update = &quot;true&quot;
else
  update = &quot;false&quot;
end

puts &quot;Vagrant box. D&#233;marrage en mode mise &#224; jour : &quot; + update

config.vm.provision &quot;file&quot;, source: &quot;../common&quot;, destination: &quot;common&quot;

config.vm.provision &quot;shell&quot;, privileged: false, inline: &lt;&lt;-SHELL
  bash common/050_Certificats.sh
  bash common/070_Utilitaires.sh
  bash common/100_JDK.sh &quot;17&quot;
  bash common/150_Postgresql-Postgis.sh &quot;15&quot; &quot;3&quot; &quot;postgres&quot;
  bash common/200_NodeJS-Angular.sh &quot;16&quot;
  bash common/250_ELK.sh &quot;7.x&quot;
  bash common/300_Kafka.sh &quot;3.4.0&quot; &quot;2.12&quot; $update
SHELL

Message on console:

Vagrant box. D&#233;marrage en mode mise &#224; jour : false

default: Indiquez si Kafka est en installation initiale (false) ou update (true).  n&#39;est pas accept&#233;.

The bash script called, testing the values:

#!/bin/bash
# $1: Version de Kafka [3.4.0]
# $2: Version de Scala associ&#233;e [2.12]
# $3: Mode update (true) ou installation initiale (false)
echo &quot;300 : Installation de Kafka $1 bas&#233; sur Scala $2&quot;
export DEBIAN_FRONTEND=noninteractive

if [ -z &quot;$1&quot; ]; then
  echo &quot;Pr&#233;cisez la version de Kafka &#224; installer. [Recommand&#233;e : 3.4.0]&quot; &gt;&amp;2;
  exit 1;
fi

if [ -z &quot;$2&quot; ]; then
  echo &quot;Pr&#233;cisez la version de Scala li&#233;e. [Recommand&#233;e : 2.12]&quot; &gt;&amp;2;
  exit 1;
fi

if [ &quot;$3&quot; != &quot;true&quot; ] &amp;&amp; [ &quot;$3&quot; != &quot;false&quot; ]; then
  echo &quot;Indiquez si Kafka est en installation initiale (false) ou update (true). $3 n&#39;est pas accept&#233;.&quot; &gt;&amp;2;
  exit 1;
fi

update=$3

if [ &quot;$update&quot; == &quot;false&quot; ]; then
   cd /tmp &amp;&amp; wget -q &quot;https://downloads.apache.org/kafka/$1/kafka_$2-$1.tgz&quot;
   sudo tar -xf &quot;kafka_$2-$1.tgz&quot; -C /opt
   echo &quot;export KAFKA_HOME=/opt/kafka_$2-$1&quot; &gt;&gt; ~/.profile
else
   echo &quot;En mode update, Kafka ne se r&#233;installera pas.&quot;
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 &lt;&lt;-SHELL.

I found myself successful with that, instead:

if ARGV[0] == &quot;provision&quot;
  update = &quot;true&quot;
else
  update = &quot;false&quot;
end

puts &quot;Vagrant box. D&#233;marrage en mode mise &#224; jour : &quot; + update

config.vm.provision &quot;file&quot;, source: &quot;../common&quot;, destination: &quot;common&quot;

config.vm.provision &quot;shell&quot;, privileged: false, path: &quot;../common/050_Certificats.sh&quot;
config.vm.provision &quot;shell&quot;, privileged: false, path: &quot;../common/070_Utilitaires.sh&quot;
config.vm.provision &quot;shell&quot;, privileged: false, path: &quot;../common/100_JDK.sh&quot;, args: [&quot;17&quot;]
config.vm.provision &quot;shell&quot;, privileged: false, path: &quot;../common/150_Postgresql-Postgis.sh&quot;, args: [&quot;15&quot;, &quot;3&quot;, &quot;postgres&quot;]
config.vm.provision &quot;shell&quot;, privileged: false, path: &quot;../common/200_NodeJS-Angular.sh&quot;, args: [&quot;16&quot;]
config.vm.provision &quot;shell&quot;, privileged: false, path: &quot;../common/250_ELK.sh&quot;, args: [&quot;7.x&quot;]
config.vm.provision &quot;shell&quot;, privileged: false, path: &quot;../common/300_Kafka.sh&quot;, args: [&quot;3.4.0&quot;, &quot;2.12&quot;, &quot;#{update}&quot;]

答案2

得分: 1

我会尝试执行以下命令:

因为根据错误信息,它似乎明确期望字符串 &quot;true&quot;&quot;false&quot;

英文:

I would try

bash common/300_Kafka.sh &quot;3.4.0&quot; &quot;2.12&quot; &quot;#{update}&quot;

because from the error message it seems like it explicitly expects the string &quot;true&quot; or &quot;false&quot;.

huangapple
  • 本文由 发表于 2023年6月2日 13:58:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定