This question already has answers here:
Makefile - Get arguments passed to make
(2 answers)
Closed 4 months ago.
Is there any way to get key word args in Makefile ?
i have tried giving args and accessing using $1, $2 etc, but how can we give it with a keywords.
You can directly call just like env vars:
file syntax:
build:
cat $(file)
command to run:
make file=my_file.txt
Related
This question already has answers here:
How to get a variable value if variable name is stored as string?
(10 answers)
Closed 1 year ago.
I want to be able to take the values of two variables and concatenate them together to form the identifier for another variable in a bash script.
final_answer="we did it"
one="final"
two="answer"
t="${one}_${two}"
echo ${$t} # would like this to echo we did it; currently give "${$t}: bad substitution"
Not sure this is possible but it seems like bash would have this capacity somehow.
Thank you!
$ echo "${!t}"
we did it
See http://mywiki.wooledge.org/BashFAQ/006#Indirection for details.
This question already has answers here:
Bash indirect variable referencing
(1 answer)
Bash indirect array addressing?
(3 answers)
Closed 2 years ago.
Here is an expected behavior for associated array in bash
$ declare -A PC=( [Monitor]=Dell [CPU]=HP )
$ echo ${PC[CPU]}
HP
This gives me HP as output
Lets say I have these PC,Monitor amd CPU values stored in variable a , b and c. I am trying fetch the details now but I am getting "bad substitution" error when trying so.
$ a=PC; b=Dell; c=HP
$ echo ${$a[$b]}
bash: ${$a[$b]}: bad substitution
$ echo ${PC[$b]}
Dell
${PC[$b]} however is returning expected output but not {$a[$b]}
Not sure how this can be achieved. Thanks in advance.
What you are trying to do is called indirection - using one variable as the name of another variable.
In bash you do this for normal variables using the syntax ${!var}, as in
a=5
b=a
echo ${!b} # 5
Unfortunately this won't work how you want for an array variable because the syntax ${!array[*]} means something else (getting all keys from an associative array).
Instead, as suggested by a comment below, you can create a string for the entire reference and then use redirection on that:
lookup="$a[$b]"
echo ${!lookup} # will give Dell in your example
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.
This question already has answers here:
How can I parse a YAML file from a Linux shell script?
(23 answers)
Closed 3 years ago.
Is there a "simple" way (without functions or packages) to get the value of a YAML file in a terminal (bash/sh)?
I wanna extract the value of users -> user("kube-admin-local") -> client-certificate-data
This is the YAML example:
users:
- name: "kube-admin-local"
user:
client-certificate-data: 0FURS0tLS0tCk1JSUM2VENDQWRHZ0F3SUJBZ0lJT2wyZ0NHL1BnTWd3RFFZSktvWklodmNOQVFFTEJRQ
- name: kube-admin
user:
client-certificate-data: LS0tLS1CRUd=0FURS0tLS0tCk1JSUM2VENDQWRHZ0F3SUJBZ0lJT2wyZ0NHL1BnTWd3RFFZSktvWklodmNOQVFFTEJ
This won't work in all systems, but
sed -n '/name: "kube-admin-local"/,/name:/s/.*client-certificate-data: \(.*\)/\1/p'
Should do it.
-n: don't print unless explicitly stated
/name: "kube-admin-local"/,/name:/: lines between those matches
rest : substitute data and print
This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 6 years ago.
In Pycharm when we use variable e.g. $privateKey, we get the warning Simple variable usage as below snapshot and recommend us to turn to the syntax ${privateKey}
My question is why we get such warning? What is the risk to use simple variable like that?
When clicking more
Thanks to #Whymarrh. One answer is as below.
since "$foobar" would instead expand foobar
My answer is to separate/distinguish $myVar and notInVar in string "$myVarnotInVar"
In other words
myVar=122
echo "$myVarnotInVar" # will print empty string "" since undefined variable $myVarnotInVar
echo "${myVar}notInVar" # will print 122notInVar