bash string manipulation got errors in jenkins shell (not pipeline) [duplicate] - linux

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 3 years ago.
i am trying to get value of variable by given string:
Running this code on Jenkins and its get "bad substitution"
in regular shell it works.
example:
param1="hello"
param2="world"
PARAMS="param1 param2"
for p in $PARAMS;do
echo ${!p}" "
done
what the best way to make it work in Jenkins too.

You must use with $ sign before the name of the variable:
PARAMS="$param1 $param2"
for p in $PARAMS;do
echo ${p}" "
done

Related

How can we execute a string command and then get the result of it to assign a variable in shell script bash [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 12 months ago.
I want to execute a string command and then get the result of it to assign a variable in shell script bash.
for example :
strCode="scontrol show jodid --dd $VALUE"
eval $strCode
The point here is this codepart worked but how can I get the results from the `
eval
` command and assign it to a variable, because I need it to use.
when I type the theResult=eval $strCode it does not work.
thanks for the help
The solution is:
strCode=$(scontrol show jodid --dd $VALUE)
echo "${strCode}"
It's easier than you are doing:
theResult=$(scontrol show jodid --dd $VALUE)
You should follow some bash tutorial, as this is a quite basic question.

Is there any difference in how variables are referenced in shell script? [duplicate]

This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 3 years ago.
Consider the following code:
name=John
echo ${name}
It prints "John", just as expected. Now consider this code:
name=John
echo $name
Again, this code prints "John" just as expected. Both codes work fine.
But I wonder is there any difference between the two, e.g. compatibility?
In your case, there is no difference.
In this case, there is:
name=John
echo ${name}Doe
echo $nameDoe
Read more: here

Change value of variable insead loop Bash [duplicate]

This question already has answers here:
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 4 years ago.
I'm newer on bash scripting ,I have a global variable that I want to change his value insead a loop in my script but still get an error that commande not found
this my script :
SCRIPT_BASE = "/home/scripts/test-Scripts"
CURRENT_SCRIPT_PATH = ""
declare -a arr=("A" "B" "C" "D")
for i in "${arr[#]}"
do
if [ $i == "A" ]; then
CURRENT_SCRIPT_PATH = $SCRIPT_BASE
echo -e "Current Path : $CURRENT_SCRIPT_PATH"
fi
done
when I run this script I get that CURRENT_SCRIPT_PATH commande not found
Thanks in advance for any help
In bash you should be really cautious about spaces in if conditions but also when you assign a value to a variable.
Replace in your code the following tree lines:
SCRIPT_BASE="/home/scripts/test-Scripts"
CURRENT_SCRIPT_PATH=""
CURRENT_SCRIPT_PATH=$SCRIPT_BASE
If you keep a space after the variable name bash will interpret it as a command and as you do not have commands SCRIPT_BASE, CURRENT_SCRIPT_PATH, CURRENT_SCRIPT_PATH in your current $PATH you have the error command not found that is produced.

I cannot make echo display the name with an _ symbol [duplicate]

This question already has answers here:
How to echo "$x_$y" in Bash script?
(4 answers)
When do we need curly braces around shell variables?
(7 answers)
Closed 5 years ago.
I am new at this and I'll simplify my problem. I doing the following script:
echo "Insert number of elements:"
read elements
echo "Insert md number:"
read sim
echo "uni_$elements_md$sim.tpr" >> doc.bash
But when I run the doc.bash file it only shows inside:
uni_4.tpr
Please help me, how must I do my echo so that if I put elements as 14 and sim as 4 (just to put an example), it displays:
uni_14_md4.tpr
Thanks.

Convert string to bash argument [duplicate]

This question already has an answer here:
Bash missing $(HOME) variable
(1 answer)
Closed 5 years ago.
i want to know if there is a way to do something like this
VAR="-o sampleoutput"
./mycommand.sh $(VAR)
Thank you
Edit: This question is different. When i tried to run my command withuot parenthesis too but it did not work. Here is my code
ADDITIONAL_ARGUMENTS="-J-javaagent:/opt/newrelic/newrelic.jar &"
./bin -Dconf=./configfile $ADDITIONAL_ARGUMENTS
This is my result
Bad root server path: /path/to/my/project/&
Remove your () around VAR.
VAR="-o sampleoutput"; ./mycommand.sh $VAR

Resources