This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 9 months ago.
Below jq query output comes correctly.
ROUTE_ID= jq -r '.[][]? | select(.pattern? == "*test.com/testcards/email/*").id' route.json
route.json file contains a json output.
But echo "this is route $ROUTE_ID" or echo "this is route $ROUTE_ID does not return value for $ROUTE_ID"
What you are currently doing is setting the environment variable ROUTE_ID to nothing for the execution of jq, eg:
MY_ENV=abc command
Will set the environment variable "MY_ENV" to "abc" for the execution of command.
What you want to do is store the output of your command a variable, for this you'll need to use command substitutions:
my_var=$(command)
In your case:
route_id=$(jq -r '.[][]? | select(.pattern? == "test.com/testcards/email/").id' route.json)
Nitpicking; use lowercase variable names when possible, as UPPER_CASE are "reserved" for exported environment variables.
Related
This question already has answers here:
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 1 year ago.
I'm writing pipeline in Jenkins. My code looks something like below:
void someFun(){
sh '''
VAR='a_b_c_d'
TEMPVAR=$VAR | tr '_' '-'
echo "With hyphens $TEMPVAR-blah-blah"
echo "With underscores $VAR"
'''
}
stage{
someFun()
}
All I want to achieve is a way to replace underscores from 1st variable and use its value in 2nd variable. Also. I'm not intending to mutate VAR. And I want to store the value, not just print it.
When I'm using this above approach, I'm getting TEMPVAR empty.
What I'm trying to possible to achieve is possible? If yes, what is the way to achieve it?
I read multiple posts but couldn’t find any helpful:(
You can do it in many ways, like with:
tr, but in this case you need to use an additional shell:
TEMPVAR="$(echo "$VAR" | tr _ -)"
or even better with string substitution:
TEMPVAR="${VAR//_/-}"
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 4 years ago.
I'm trying to assign a variable with a value I get from grep, here's my code
a="i
am
a
string
"
b="$a"|grep am
echo "$b"
I expect the result is am, but the result b is empty. But when I code echo "$a"|grep am directly, I get the right result. why and how can I assign the result to b?
Do it like this:-
**b=$(echo "$a"|grep am)
**
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
This question already has answers here:
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Closed 6 years ago.
I'm trying to get the values of some php variables into a bash script in order to set other bash variables. Basically I need to be able to access the value of a variable - variable name.
the script can find & read the file, find the php variables, but when I try to set them it just hangs. Here is what I have:
variables=(database_user database_password dbase);
paths=(modx_core_path modx_connectors_path modx_manager_path modx_base_path);
for index in "${variables[#]}"; do
index=\$$index
echo $index;
$index="$(grep -oE '\$${!index} = .*;' $config | tail -1 | sed 's/$${!index} = //g;s/;//g')";
done
not sure what I am doing wrong here...
You are trying to perform an indirect assignment.
You should get rid of these two lines :
index=\$$index
echo $index;
By simply writing :
echo "${!index}"
Which does an indirect expansion cleanly (gives you the value of the variable whose name is contained in variable index).
Next, the problematic line is this one:
$index="$(grep -oE '\$${!index} = .*;' ... (rest omitted)
In Bash, an assignment cannot begin with a $.
One way you can perform an indirect assignment is this (after removing the index re-assignment as suggested above) :
printf -v "$index" "$(grep -oE '\$${!index} = .*;' ... (rest omitted)
This uses the -v option of printf, which causes the value passed as the final argument to be assigned to a variable, the name of which is passed to the -v option.
There are other ways of handling indirect assignment/expansions, some with code injection risks, as they use (potentially uncontrolled) data as code. This is something you may want to research further.
Please note I am assuming the actual grep command substitution works (I have not tested).
This question already has answers here:
How to create a bash variable like $RANDOM
(3 answers)
Closed 5 years ago.
I would like to have a shell variable that could be dynamically run every time it is refered, for example, i would like to have a variable $countPwd which could return the count of files/dirs in the current directory, it could be defined as:
countPwd=`ls | wc -l`
and if I do echo $countPwd it would only show the value when I define the variable, but it won't update automatically when I change my current directory. So how do I define such a variable in bash that the value of it get updated/calculated on the fly?
Update:
The $PWD is a perfect example of a variable get evaluated in the real time. You don't need to use $() or backticks `` to evaluate it. How is it defined in bash?
Make a function:
countPwd() {
ls | wc -l
}
Then call the function like any other command:
echo "There are $(countPwd) files in the current directory."
Another option: store the command in a variable, and evaluate it when required:
countPwd='ls | wc -l'
echo $(eval "$countPwd")
You'll need to write some C code: write a bash's loadable buitins to do the heavy lifting in defining variables with dynamic values like $SECONDS or $RANDOM ($PWD is just set by cd).
More details in my answer here (a duplicate of this question).