简体   繁体   中英

output from script into environment variables, bash

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.

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.

Any idea ?

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

Thank you a lot for your help

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

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

or with a recent bash

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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