英文:
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:
- https://stackoverflow.com/questions/6585064/how-to-pass-command-line-parameters-from-a-file - this question is about position arguments and named arguments
- https://stackoverflow.com/questions/19331497/set-environment-variables-from-file-of-key-value-pairs - this question is about environment variables
答案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+=("--$k" "$v")
done < yourtextfile.txt
./script.sh "${args[@]}"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论