String with variable expansion changes if variable changes - bash

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?

Related

Create env variables in a loop in bash not working

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

Variable in variablenames

i have a a couple of variables with a number in its names. e.g
SERVER_IP48_SUBNET
..
SERVER_IP60_SUBNET
And an additional variable
SERVER_IP
Im trying to expand/concatenate them in the following way:
ALLIPs=${SERVER_IP}
for i in {48..64}; do
ALLIPs=${ALLIPs},${SERVER_IP${i}_SUBNET}
done
as you can imagine this script fails saying:
Wrong substitution
Does anybody of you know a good solution for this problem?
Thanks so far
Use a nameref with bash version 4.3 +
ALLIPs=${SERVER_IP}
for i in {48..64}; do
declare -n tmp="SERVER_IP${i}_SUBNET"
ALLIPs+=",$tmp"
done
But you should really be using an array in the first place:
server_ip=0.0.0.0
subnet_ip=(
[48]=1.1.1.1
[49]=2.2.2.2
# ...
[64]=16.16.16.16
)
all_ips=( "$server_ip" )
for i in {48..64}; do
all_ips+=( "${subnet_ip[i]}" )
done
(
IFS=,
echo "ALLIPs = ${all_ips[*]}"
)
Get out of the habit of using ALLCAPS variable names, leave those as
reserved by the shell. One day you'll write PATH=something and then
wonder why
your script is broken.
I just noticed, if you just want a to join the IP addresses with commas, and you're using an array, you don't need a loop at all:
all_ips=$(
IFS=,
set -- "$server_ip" "${subnet_ip[#]}"
echo "$*"
)
You can use ${!varprefix#} or ${!varprefix*} to expand to all variables with that common prefix (the difference is the same as $# and $*):
SERVER_IP48_SUBNET=48sub
SERVER_IP49_SUBNET=49sub
SERVER_IP50_SUBNET=50sub
SERVER_IP=1.2.3.4
# set this as empty since !SERVER_IP# also matches SERVER_IP
ALLIPS=""
for var in "${!SERVER_IP#}"; do
ALLIPS=$ALLIPS,${!var}
done
This would probably be more practical if you could invert the names like this, since we can only match prefixes:
SERVER_IP_SUBNET_48=48sub
SERVER_IP_SUBNET_49=49sub
SERVER_IP_SUBNET_50=50sub
SERVER_IP=1.2.3.4
ALLIPS=$SERVER_IP
for var in "${!SERVER_IP_SUBNET_#}"; do
ALLIPS=$ALLIPS,${!var}
done
More info on this feature in the bash manual.
One idea:
SERVER_IP48_SUBNET=48sub
SERVER_IP49_SUBNET=49sub
SERVER_IP50_SUBNET=50sub
SERVER_IP=1.2.3.4
ALLIPs=${SERVER_IP}
for i in {48..50}
do
tmpvar="SERVER_IP${i}_SUBNET" # build the variable name
ALLIPs="${ALLIPs},${!tmpvar}" # indirect variable reference via tmpvar
done
echo "ALLIPs = $ALLIPs}"
This generates:
ALLIPs = 1.2.3.4,48sub,49sub,50sub

Bash create a path with variables divided by _ and check if exist

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

get a default value when variable is unset in make

(edit: question more accurate based on #Michael feedback)
In bash, I often use parameter expansion: the following commands print "default value" when $VARNAME is unset, otherwise it prints the VARNAME content.
echo ${VARNAME:-default value} #if VARNAME empty => print "default value"
echo ${VARNAME-default value} #if VARNAME empty => print "" (VARNAME string)
I did not find a similar feature on GNU make.
I finally wrote in my Makefile:
VARNAME ?= "default value"
all:
echo ${VARNAME}
But I am not happy with this solution: it always creates the variable VARNAME and this may change the behavior on some makefiles.
Is there a simpler way to get a default value on unset variable?
If you want to use the expansion of a GNU make variable if it is non-empty and a default value if it is empty, but not set the variable, you can do something like this:
all:
echo $(or $(VARNAME),default value)
If you want to test if a variable has a non-empty value, you can use:
ifeq ($(VARNAME),)
VARNAME="default value"
else
do_something_else
endif
For checking if a variable has been defined or not, use ifdef.
Refer to Syntax of Conditionals in the manual for more.
I have a similar case where the result of filtering a shell command could be a single word or empty string. When empty, it should fallback to the default word. In the example below APPLE_LINUX will be 'apple' on macOS or 'linux' on other platforms. MSG will be set to the message for the appropriate platform. The example intentionality avoids using ifeq.
MACHINE := $(shell $(COMPILE.cpp) -dumpmachine)
MACHINE_APPLE := $(findstring apple,$(MACHINE))
APPLE_LINUX := $(firstword $(MACHINE_APPLE) linux)
apple.MSG := You are building on macOS
linux.MSG := You are building on Linux or another OS
MSG := $($(APPLE_LINUX).MSG)
Just remove the colon. If you use :- in your substitution the default value will be used if the variable is null, an empty string or it does not exist, but just using - on its own will only substitute the default value if the variable has not been defined.
# var1=default
# var2=
# echo var2 is ${var2:-$var1}
var2 is something
# echo var3 is ${var3:-$var1}
var3 is something
# echo var2 is ${var2-$var1}
var2 is
# echo var3 is ${var3-$var1}
var3 is something

How to make a script read a value from a property file and pass it to the same script?

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

Resources