从脚本输出到环境变量bash

时间:2023-01-26 23:20:55

I have a script which output that :

我有一个脚本输出:

TAGS    Name    i-1c69afe325    instance    INSTANCE_NAME

I want to take "Name" and "INSTANCE_NAME" and make an environment variable with it.

我想使用“Name”和“INSTANCE_NAME”并使用它创建一个环境变量。

I tried a lot of things and the last was :

我尝试了很多东西,最后一个是:

./test2.sh | awk '{system("export "$2"="$5)}'

it did't errored but I don't have any env var.

它没有错,但我没有任何env var。

Any idea ?

任何的想法 ?

PS : if it is important, I try to transform my ec2-tags into environment-variables each time my instance boot.

PS:如果它很重要,我会在每次实例启动时尝试将ec2-tags转换为环境变量。

Thank you a lot for your help

非常感谢你的帮助

1 个解决方案

#1


Using the information you can find on Bash FAQ 001 you come up with something like this:

使用您在Bash FAQ 001上找到的信息,您可以得到如下内容:

while IFS= read -r tags name i_var instance instance_name; do
  declare "$name"="$instance_name"
done < <(./test2.sh)

or with a recent bash

或者是最近的bash

while IFS= read -r tags name i_var instance instance_name; do
  printf -v "$name" %s "$instance_name"
done < <(./test2.sh)

#1


Using the information you can find on Bash FAQ 001 you come up with something like this:

使用您在Bash FAQ 001上找到的信息,您可以得到如下内容:

while IFS= read -r tags name i_var instance instance_name; do
  declare "$name"="$instance_name"
done < <(./test2.sh)

or with a recent bash

或者是最近的bash

while IFS= read -r tags name i_var instance instance_name; do
  printf -v "$name" %s "$instance_name"
done < <(./test2.sh)