Assigning one variable the value of another in bash - 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.

Related

Export array of JSON objects as environment variable

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}]'

BASH : Problem regarding hash table/dictionary and bad substitution

Context:
I started writing this script for easily changing connections for my raspberry pi zero (Raspibian Lite as OS), this is because I always needed to edit the wpa-supplicant config file and decided to do something about it as it is a really portable pc.
How it works:
The core of the program is to create profiles in format of dictionaries to store the name and passwd and apply that profile when needed. The profiles are added to the script code itself. I made it like this, every time a new profile is created this 2 lines are generated with the profile name corresponded. For example:
declare -A profile1
profile1=( ["name"]="name" ["pass"]="pass")
Problem:
To apply that profile I put at terminal prompt "./script --use profile1" so my goal is that it gets the details of the profile desired.
When I write that by :
echo "${$2[name]}" it outputs me a " bad substitution" error.
Things I tried and checked:
Shebang is #!/bin/bash
I tried substituting the $2 in a string and trying to execute it but I dont get anything good.
Things to consider:
Here is the link to the script so you can test it yourself, there are some things are a bit more complex than the thing indicated in the post but I just simplified it.
https://github.com/gugeldot23/wpa_scrip
You need nameref variables if you want to address the profile array name by reference:
declare -n profile # nameref variable profile
profile="$2" # Fills-in the nameref from argument 2
# Address the nameref instead of echo "${$2[name]}"
echo "${profile[name]}"
See: gnu.org Bash Manual / Bash Builtins / declare:
-n
Give each name the nameref attribute, making it a name reference to another variable. That other variable is defined by the value of name. All references, assignments, and attribute modifications to name, except for those using or changing the -n attribute itself, are performed on the variable referenced by name’s value. The nameref attribute cannot be applied to array variables.

Build the name of Bamboo variable in script

Is it possible to concatenate a string to call a Bamboo variable.
Using a script task in Bamboo, I want to generalize the following:
python my.py moon ${bamboo.mynamespace.moon}
to
SET planet=MOON
python my.py %planet% ${bamboo.mynamespace.%planet%}
But doing it like the second example above results in my python script receiving
${bamboo.mynamespace.%planet%}
as a string and not the value of
${bamboo.mynamespace.moon}
I know... moon is not a planet
I don't think it's going to be possible in the way how you're using it. Because once you use ${bamboo.variableName} Bamboo tries to resolve the variable and substitute it with a variable value. Since there's no variable%planet% Bamboo can't reference it.
But I think you could reorganise your solution a bit and make use environment variables (all Bamboo variables are passed to process as environment variables). So e.g. if Bamboo variable's name is variable.name you're allowed to reference to it via ${bamboo_variable_name} (bamboo prefix + all dots are replaced with underscore)
Then I can imagine you could get variable which interests you via print os.environ['bamboo_mynamespace_' + 'planet'] (more info on env variables in python here)

Location of environment variables, where `_dupenv_s` function searches for the value?

Where does the _dupenv_s function searches for environment variables for varname argument? If I wanted to change/add/remove environment variables the function look for where would I go?
They are stored in the _environ global variable. Use _putenv_s to add/modify/remove (empty "" string value to remove).
Windows processes also have a separate environment that is used by the native Windows API functions.

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

Resources