Export array of JSON objects as environment variable - bash

How can I export an environment variable like this?
USERS=[{name:a,surname:b,age:c},{name:d,surname:e,age:f}]
What I've tried so far unsuccessfully
[{"name":"a","surname":"b","age":"c"},{"name":"d","surname":"e","age":"f"}]
[{'name':'a','surname':'b','age':'c'},{'name':'d','surname':'e','age':'f'}]
[{\'name\':\'a\',\'surname\':\'b\',\'age\':\'c\'},{\'name\':\'d\',\'surname\':\'e\',\'age\':\'f\'}]
[{\"name\":\"a\",\"surname\":\"b\",\"age\":\"c\"},{\"name\":\"d\",\"surname\":\"e\",\"age\":\"f\"}]
"[{"name":"a","surname":"b","age":"c"},{"name":"d","surname":"e","age":"f"}]"
'[{"name":"a","surname":"b","age":"c"},{"name":"d","surname":"e","age":"f"}]'
'[{'name':'a','surname':'b','age':'c'},{'name':'d','surname':'e','age':'f'}]'
I know that with docker-compose and terraform this can easily be done but I have to define a single env var here
Something very important that I forgot to mentioned:
I want this variable to be read as a LIST since it's part of a configuration file. Not as a string. Since I want to map it to a User object.
User {
name,
surname,
age
}

Put it in quotes and use the export command to put it in the environment. Also, make it valid JSON by quoting all the strings
export USERS='[{"name":"a","surname":"b","age":10},{"name":"d","surname":"e","age":35}]'

Related

Zsh: Creating associative array from yaml file using yq

The real case:
Every time I set up my environment, I'd like to check and – create if are not existing – certain environment variables. So, instead of doing it manually all the time, I thought it would be great if I can have a file which stores the environment name env_N and environment value env_V pairs. Amongst all text file formats, the yaml looks the simplest and the more natural to store that info.
So what I thought it would be great if I suck in the yaml file with my environmental variables using yq and create associative array ready to be iterated over by zsh foreach loop:
foreach entry in my_assoc_arr
do
check_and_create(entry.env_N, entry.env_V)
done
with the final result of:
$ echo $env_N1
env_V1
$ echo $env_N2
env_V2
$ echo $env_N3
env_V3
...
The problem I'm having is to get my yaml to associative array using yq in shell zsh script.
After applying each suggestion from the comments, I was unable to create my associative array from yaml with yq. I had errors from yq like bad syntax or script worked or not depends on whether I have #!/bin/zsh switched on or commented out.
I got impression that my task is simple, but somehow I cant achieve this.
What I'm doing wrong here?
PS: I'm using zsh on macOS
Why would you want to use Yaml for this, when it’s much easier to write a simple shell script for it?
Simply create a text file with the following line for each env var:
# Set $FOO to 'bar' if $FOO does not yet exist.
export ${FOO=bar}
Then source this file from your shell (or another script).
See https://zsh.sourceforge.io/Doc/Release/Expansion.html#Parameter-Expansion

Terraform template variables from other Terraform resources

I have Terraform that is using a templated bash script to set my user data section for an AWS launch configuration.
data "template_file" "user_data" {
template = "${file("${path.module}/user-data.tpl")}"
vars {
file_system_id = "${aws_efs_mount_target.my_efs_alpha.dns_name}"
}
}
The file_system_id variable then needs to be used in my template:
sudo mount -t nfs -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 $$$${file_system_id}:/ /mnt/efs
Bash will interpret a single dollar sign as a bash variable. As I understand it, Terraform will interpret a double-dollar-sign as a Terraform variable. For added fun, the dollar signs in the template need to be escaped with another dollar sign -- hence the 4 dollar signs in front of file_system_id.
Looking at the user data in my Launch Config over in AWS Console, Terraform does not appear to be making any effort to replace my $$$${file_system_id) with the variable value from my template_file definition. Rather, it just shows up in the user data section as literally $${file_system_id}.
So, the question is, how do I get my EFS DNS name (or whatever other value I want) to replace the file_system_id variable in my template? What have I missed?
As BMW mentioned, you don't need to escape the dollar signs. ${file_system_id} works just fine.
Terraform's variable-replacement in templates will run first so you don't need to worry about how Bash will parse it until after the variables are replaced.

Assigning one variable the value of another in bash

I am using a switcher that exports environmental variables based upon which machine is being used. One of the variables that gets exported is ems_1 .
Now, in my start.bash script, I am trying to assign a variable called provider with the value ems_1 has.
export provider = ems_1
Doesn't work . Any suggestions ?
export provider=$ems_1
You need to reference variables using the $ sign.
variable=value
cannot have spaces in-between.

Variable PATH in shell using cron

I've read that while using a cron you define variables like always:
var = <value>
But you can't use variable values on < value > such as:
PATH=$PATH
So how could I introduce the PATH inside PATH plus HOME/FOLDER for instance? Normally I would do...
PATH=$HOME/FOLDER:$PATH
But if what I've read is correct, that isn't available...right?
my crontab(5) page agrees with you:
The value string is not parsed for environmental substitutions or replacement of variables, thus lines like
PATH = $HOME/bin:$PATH
will not work as you might expect.
However, if you're specifically interested in $HOME, you can use this:
An alternative for setting up the commands path is using the fact that many shells will treat the tilde(~) as substitution of $HOME, so if you use bash for your tasks you can use this:
SHELL=/bin/bash
PATH=~/bin:/usr/bin/:/bin

Shell Scripting: Using a variable to define a path

My problem lies with my confusion with shell variables.
To my understanding, variables allow me to store a value (String in this case) and to call it later in my code. So if I wanted to have a variable that holds the path to some set of scripts, I could ideally just store it like this:
SPTH = '/home/Foo/Documents/Programs/ShellScripts/Butler'
//Later on in the script//
cd $SPTH
./script1
What I'm trying to do, with probably the wrong syntax, is to set the path to variable SPTH.
Then I use cd with argument $SPTH.
Ideally this would allow me to run the file there without typing in the path. However it doesn't work. The $SPTH is ignored and the result is as if cd was used alone.
So what am I doing wrong? And what would be a way to do this?
Don't use spaces...
(Incorrect)
SPTH = '/home/Foo/Documents/Programs/ShellScripts/Butler'
(Correct)
SPTH='/home/Foo/Documents/Programs/ShellScripts/Butler'
To add to the above correct answer :-
For my case in shell, this code worked (working on sqoop)
ROOT_PATH="path/to/the/folder"
--options-file $ROOT_PATH/query.txt

Resources