Passing value to an existing variable in makefile [duplicate] - makefile

This question already has answers here:
Passing additional variables from command line to make
(8 answers)
Closed 3 years ago.
I found this port = ${PORT} in one makefile. How can I pass value to the variable port using make commands?

for many simple situations, pass the values on the command line using the variable=value grammar. Note that this will (usually) override the value in the Makefile.
make PORT=1234 ...
# OR
make port=1234 ...
This format will also override other variable settings (environment variables, built-in variables, ...). It will NOT replace variables set with the 'override' directive.
#rveerd comment provide a link to more complete answer.

Related

Extract the pointer from a V_${i}_T variable name in a bash loop [duplicate]

This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Dynamic variable names in Bash
(19 answers)
Closed 4 years ago.
Suppose I have several variable names like
V_1_T = a
V_2_T = b
V_3_T = c
...
and I want to extract the pointers a, b , c, ... in a bash loop in order to concatenate the values. My explicit wish is about reconstructing a message separated in several parts as explained in the gammu-smsd documentation.
I've tried the example in the doc but it doesn't work. The reason is that the code never points to the pointer of the variables but to the variables themselves, i.e. I get V_1_T at best and never a as I would.
I've also tried to put
${V_${i}_T} ; ""$"V_${i}_T"
with and without escape symbols for the commas, ..., but nothing worked ...
Any ideas ?
I'm working on the latest version of Raspbian + RaspberryPi.
Use indirect parameter expansion:
for i in 1 2 3; do
t="V_${i}_t"
echo "${!t}"
done
This avoids the use of eval shown in the docs you linked to.

how can I read a value from an environment variable with ':' in its name? [duplicate]

This question already has an answer here:
Accessing environment variables that don't map to valid shell variable names
(1 answer)
Closed 5 years ago.
We are using elastic beanstalk. Some values are environment properties of the environment. When I perform a container_command I'm able to read this properties as environment variables. The problem is the following: a lot of properties are named like this db:user or collector:server and after that the value.
How can I read this values? I can interpret them as environment variables. So the environment properties with 'normal' names I can read. But not those ones who contain a ':' in their name:
To test (+ make it clearer for people who don't know elastic beanstalk) I've created this. The global goal is to read the value of a variable which contains a ':' in its name.
#!/bin/bash
${myvar:test}="hey"
echo ${myvar:test}
$./test.sh
$./test.sh: line 3: =hey: command not found
: isn't an allowed character in shell variables at all, and ${test:foo} has a completely separate meaning (it expands $test with a default value of foo if no variable named test is defined).
If your operating system is Linux, however, you can directly parse your original environment variables from procfs:
#!/usr/bin/env bash
declare -A env=( ) ## <- note that this requires bash 4.0 or newer
while IFS= read -r -d '' envvar; do
[[ $envvar = *:* ]] || continue
varname=${envvar%%=*}
value=${envvar#*=}
env[$varname]=$value
done < /proc/self/environ
echo "The value of the environment variable db:user is: <<${env[db:user]}>>"
Note that to test this, though, you'll need to be able to actually create an environment variable with a literal colon, and your code currently fails at doing so. Consider instead:
env db:user="test value for db:user" ./yourscript

Bash scripting help ENV_NAME=${1:-develop} [duplicate]

This question already has answers here:
Usage of :- (colon dash) in bash
(2 answers)
Closed 6 years ago.
I've inherited some bash scripts and I see this one liner
ENV_NAME=${1:-develop}
Can someone tell me what it's doing? I don't even know how to google this.
Thanks!
The construct is a so called parameter expansion. It expands to a default value if the variable itself is not set or null. The semantic is
${variable:-default value}
$1 is the first parameter passed to the script. If the parameter will be omitted ENV_NAME defaults to "develop".

how to combine multi bash parameter expansion [duplicate]

This question already has answers here:
Can ${var} parameter expansion expressions be nested in bash?
(15 answers)
Closed 5 years ago.
bash parameter expansion has a few useful function, e.g. substring ,replace,upper,lower. how to combine these function without define temporary parameter?
f="abc.txt"
e=${f:4:3} #txt
echo ${e^^} #TXT
I define e to upper the txt. echo ${${f:4:3}^^} can not work. Is it possible omit e. if in java i can write
String f="abc.txt";
System.out.print(f.substring(4,7).toUpperCase());
even, i can
System.out.print("abc.txt".substring(4,7).toUpperCase());
No, this is not possible in bash AFAIK.
To make it possible we would need some sort of prioritization (along-with parsing logic changes) when more than 1 parameter expansion is specified, for which there is no code as of now.

Double expansion of a parameter in Bash script [duplicate]

This question already has answers here:
Lookup shell variables by name, indirectly [duplicate]
(5 answers)
Closed 7 years ago.
I want to expand a parameter twice, and ${$PARAM} doesn't work.
For example, if I set two variables to file names and then want to loop through those variables and expand to their file names:
INPF_1=input1.inp
INPF_2=input2.inp
# copy the input files to the execution directory
for input_param in ${!INPF_*}; do
echo ${$input_param}
done
How can I to access the file names from those parameters in the for loop?
I.e., expand to input1.inp and input2.inp.
You had it almost: just use "${!input_param}" instead of ${$input_param}.
The quoting doesn't do anything in this case, but it's a good habit.
Be aware of the difference to ${!INPF_#}, which – when used between double quotes – expands to a separate word for each variable name, whereas "${!INPF_*}" doesn't. You almost always want "${!INPF_#}". See the difference:
$ for var in "${!INPF_*}"; do echo "$var"; done
INPF_1 INPF_2
$ for var in "${!INPF_#}"; do echo "$var"; done
INPF_1
INPF_2
See the manual for the various parameter expansions.

Resources