I have a set of properties, and I want to create env variables in bash. The format of the properties is like this:
CONFIG=$(curl -s "endpoint")
echo $CONFIG
property1: value1 property2: value2 property3: value3...
Then I apply a replace to get '='
CONFIG2=${CONFIG//: /=}
echo $CONFIG2
property1=value1 property2=value2 property3=value3...
And finally, applying a for loop I want to add those properties
for property in $CONFIG2; do env "$property"; done
I need to use env since my properties contains . characters, export doesn't work for me. For example: property.1.thisisanexample=value1
While executing the 'for', I can see that in each iteration it is adding the current property, but after it finish and I run env, they aren't there. Any idea of what is happening and how can this be done?
#1
Assuming your CONFIG variable has or gets the following string value:
CONFIG='property.1.thisisanexample: value1 property.2.thisisanexample: value2 property.3.thisisanexample: value:3'
Then you may create a CONFIG2 parameter like in below:
Note: This will substitute the colon+ space : characters with a =
CONFIG2=$(echo ${CONFIG//:\ /=} )
#!/bin/bash
CONFIG='property.1.thisisanexample: value1 property.2.thisisanexample: value2 property.3.thisisanexample: value:3'
CONFIG2=$(echo ${CONFIG//:\ /=} )
echo "${CONFIG2}"
Output:
property.1.thisisanexample=value1 property.2.thisisanexample=value2 property.3.thisisanexample=value:3
#2
Now if you want to use env with the properties you may do the following:
for property in ${CONFIG2} ; do
env ${property}
done
Related
In Bash, I am trying to create a path with two variables within:
/path/to/my/file/${variable1_-}${variable2}/Still/some/path
My variable2 is always set, but the variable1 might be empty and in that case I don't want to print the "_"
I have tried the line above, but doesn't seem to be correct.
Can someone help in getting the right path printed?
Thanks in advance for your suggestions!
You have a simple typo (the underscore should be after the separator, not part of the variable name) and you want to include the underscore if variable1 is set, not it it's unset (so plus instead of minus in the parameter expansion; and add a colon to also cover the set but empty case). Presumably you also want to include the actual value of variable1 when it's set.
/path/to/my/file/${variable1}${variable1:+_}${variable2}/Still/some/path
or equivalently, nested
/path/to/my/file/${variable1:+${variable1}_}${variable2}/Still/some/path
where the braces before the underscore are necessary to separate the variable name from the literal text.
You can use this.
https://linux.die.net/man/1/bash
${parameter:+word}
set also variable1
variable1=VAR1
variable2=VAR2
variable3=${variable1:+_}
echo /path/to/my/file/${variable1}${variable3}${variable2}/Still/some/path
set only variable2
variable1=
variable2=VAR2
variable3=${variable1:+_}
echo /path/to/my/file/${variable1}${variable3}${variable2}/Still/some/path
With a few more lines of code this could work:
run () {
prefix="" # empty
if [ -n "$variable1" ]; then
prefix="${variable1}_"
fi
echo "/path/to/my/file/${prefix}${variable2}/Still/some/path"
}
# set only variable2
variable2=var2
run
# set also variable1
variable1=var1
run
output:
/path/to/my/file/var2/Still/some/path
/path/to/my/file/var1_var2/Still/some/path
description:
-n tests if the string is not empty, in that case I fill prefix with variable1 and the underscore
I'm unable to substitute the argument value(s) in the bash command as below:
# echo $int1
{"id":"74953939-fd20-4472-8aaa-067e6f4c4106"}
# echo $int2
{"id":"5ef4664d-3600-4df9-a6a9-01ffb0f49422"}
# echo $int3
{"id":"6dc95c01-742e-4225-8298-e5750fe67f27"}
# set -x
# data set net-agent interfaces '["$int1", "$int2", "$int3"]'
+ data set net-agent interfaces '["$int1", "$int2", "$int3"]'
Any idea on why the values are not being substituted?
Thanks!
I'm guessing that the argument to the command should be valid JSON, in which case you should remove the double quotes from around each variable and wrap the entire string in double quotes so variables are expanded:
data set net-agent interfaces "[$int1, $int2, $int3]"
Using set -x, this produces:
$ data set net-agent interfaces "[$int1, $int2, $int3]"
+ data set net-agent interfaces '[{"id":"74953939-fd20-4472-8aaa-067e6f4c4106"}, {"id":"5ef4664d-3600-4df9-a6a9-01ffb0f49422"}, {"id":"6dc95c01-742e-4225-8298-e5750fe67f27"}]'
Is there a way to change the contents of a string with variable expansion and have the contents update if the variable does?
Like so:
var1=0 # set var1
var2="$var1" # set var2 to var1
var1=1 # set var1 to something else and have var2 change as well?
I know this example will not work but it is just to show you guys what I'm trying to do.
Is there any way at all to accomplish this without always setting var1 before var2?
Thanks in advance
EDIT: Also, is it possible to do this with a variable that hasn't been set before the first one?
Like so:
var2=$var1
var1=10
echo $var2
I want the output to be 10 but since var1 is not set when assigned to var2, the var2 will also be empty. Help
Check this: How can I use variable variables (indirect variables, pointers, references) or associative arrays?
One way:
var1=200
var2=var1
echo ${!var2}
200
var1=100
echo ${!var2}
100
I think bash has no pointer concept.
So if I do this, I would write a function, e.g. setVar1, the func has one argument, which is the new value of var1. in this function, the new value would be set to var1 and var2
So you have to call the function when you want to set a value to var1.
Also, if var2 always has same value as var1, why not just use one variable?
I am new to linux shell script. i want that my script read a property file and save the value in any variable , the same which i can pass in same script..
as i wrote script is not fulfilling my requirement:
!/bin/bash
. test1
flat
if [ "$1" == test1 ]; then
flat=$1; /assign value to var flat
echo "flat"
fi
test1 is property file which includes :
la=12
tu=15
now i want when i run:
./myscript la
it read it from property file and store the value in flat variable.
Please help me.
You just need to use indirect referencing, but to do so, you need to store the value of the special parameter $1 in a regular parameter first.
!/bin/bash
. test1
var="$1"
# Only assign to flat if the variable specified in var is defined
if [ -n "${!var:-}" ]; then
flat="${!var}"; # assign value to var flat
echo "flat"
fi
First, ${!var} expands to the value of the variable whose name is in var. If var is "foo", it's the same as $foo. If var is "baz", it's the same as $baz.
${var:-default} expands to the value of var if it is set and has a non-null value. Otherwise, it expands to whatever you have after the ':-', in this case the string default. If there is no string, it uses the null value. So ${var:-} would expand to the null string if var was not set (or was already the null string).
Combining the two, ${!var:-} takes the variable var, and uses its value as a variable name. It then tries to expand that variable, and if it isn't set or is null, expand to the null string. So if var is la, it expands to the value of la. If var is re, and there is no variable re set, it expands to the null string.
Finally, the -n operator tests if its argument is non-zero length. In other words, it checks that the result of trying to expand the variable whose name is in var is not the null string. If that's true, then expand it again (yes, it's a little redundant) and assign its value to flat.
As the answer is written above, the variable flat is undefined if the argument to the script is not the name of a variable set in test1. If you don't mind flat being set regardless (say, flat=""), you don't need the if statement. You can just use one line to set the value of flat:
#!/bin/bash
. test1
var="$1"
flat="${!var:-}"
If I understood correctly you want to achieve an indirect variable dereferencing (see e.g. this example).
The solution is to use eval:
eval flat=\$$1
I have a TARGET variable that can be set to dev, test or prod.
I defined the following lists:
dev=(server1 user1 target1)
test=(server2 user2 target2)
prod=(server3 user3 target3)
Depending on the value of TARGET, I'd like to dynamically associate the variable CONFIG to one of the list.
Let's say TARGET=dev. I then have
eval CONFIG=\$$TARGET # I expect CONFIG to be a list containing (server1 user1 target1)
echo ${CONFIG[*]} # OK, it gives (server1 user1 target1)
echo ${CONFIG[1]} # I would expect to have "server1" but it returns "1", seems like CONFIG is not seen as a list
Any idea ?
eval CONFIG=\$$TARGET sets CONFIG to the string $TARGET. When an array is expanded in a string context, the result is the concatenation of the values in the array, with the first character of IFS inserted as a separator. Thus after the assignment the value of CONFIG is the string server1 user1 target1.
You need to assign to CONFIG as an array. Since you're working in zsh, you don't need to use eval to obtain the value of a variable whose name is in a variable. Use the P parameter expansion flag.
CONFIG=(${(P)TARGET})