如何将文件中的变量作为参数(选项)传递给脚本?

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

How to pass variables from a file as arguments (options) to a script?

问题

I have a text file with variables in format VARIABLE=value like:

PID=123123
WID=569569
TOKEN=456456456

Where VARIABLE has only uppercase letters and value contains only alphanumeric characters ([a-zA-Z0-9])

Now I want to pass these variables to a script as named arguments (options):

./script.sh --PID 123123 --WID 569569 --TOKEN 456456456

Is there a simple way to do this in bash?

英文:

I have a text file with variables in format VARIABLE=value like:

PID=123123
WID=569569
TOKEN=456456456

Where VARIABLE has only uppercase letters and value contains only alphanumeric characters ([a-zA-Z0-9])

Now I want to pass these variables to a script as named arguments (options):

./script.sh --PID 123123 --WID 569569 --TOKEN 456456456

Is there a simple way to do this in bash?

I have read:

答案1

得分: 3

读取文件,以 = 分隔元素,并构建参数数组。然后调用脚本。

args=()
while IFS== read -r k v; do
   args+=("--$k" "$v")
done < yourtextfile.txt
./script.sh "${args[@]}"
英文:

Read the file line by line as = separated elements and construct the array of arguments. Then call the script.

args=()
while IFS== read -r k v; do
   args+=(&quot;--$k&quot; &quot;$v&quot;)
done &lt; yourtextfile.txt
./script.sh &quot;${args[@]}&quot;

huangapple
  • 本文由 发表于 2023年4月11日 15:51:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983591.html
匿名

发表评论

匿名网友

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

确定