简体   繁体   English

bash脚本中命令的“源”输出作为多个变量

[英]'Source' output of a command in bash script as multiple variables

I have MSI file, which have inside version.ini file which contains multiple variables I need to take out. 我有MSI文件,里面有version.ini文件,其中包含多个需要取出的变量。

For example: 例如:

[VERSION]
fileid=...
version=...
option=...

So I have simple command: 所以我有简单的命令:

cabextract -p -F "version.ini" installer.msi | awk -F '=' '{if ($1 == "fileid" || $1 == "version" || $1 == "language") print tolower($1)"=\""$2"\"";}' > /tmp/tmpoutput
source /tmp/tmpoutput
echo $version

and with that I achieved extracting version.ini from a msi file and extracting from ini file information I want. 这样,我实现了从msi文件中提取version.ini并从ini文件信息中提取所需的信息。

But just for curiosity is there any prettier version to take out variables without temporary writing it to a file. 但是只是出于好奇,没有任何更漂亮的版本可以删除变量,而无需将其临时写入文件。 It has nosense, but I cannot find another working and simple way to load multiple variables. 它毫无意义,但是我找不到另一种可行的简单方法来加载多个变量。

If I write command like that: 如果我这样写命令:

var=$(cabextract -p -F "version.ini" installer.msi | awk -F '=' '{if ($1 == "fileid" || $1 == "version" || $1 == "language") printtolower($1)"=\""$2"\"";}')

it won't work. 它不会工作。 I can't put command in $() because then it merge results of awk command. 我不能将命令放在$()因为它会合并awk命令的结果。 I've tried to put it into array but that also failed. 我试图将其放入数组,但是也失败了。

Any advice? 有什么建议吗?

You can use process substitution here: 您可以在此处使用process substitution

source <(cabextract -p -F "version.ini" installer.msi | awk -F '=' '{if ($1 == "fileid" || $1 == "version" || $1 == "language") print tolower($1)"=\""$2"\"";}')

Using this directive output of cabextract will be used for source like a file. 使用cabextract此指令输出将用于文件之类的source

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM