Replace underscore with hyphen while writing script in Jenkins [duplicate] - shell

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//_/-}"

Related

UNIX Replace string without replacing space [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 5 years ago.
For string matching purposes I need to define a bash variable with leading spaces.
I need to define this starting from an integer, like:
jj=5
printf seems to me a good idea, so if I want to fill spaces up to 6 character:
jpat=`printf " %6i" $jj`
but unluckly when I am trying to recall the variable:
echo $jpat
the leading whitespaces are removed and I only get the $jj integer as it was.
Any solution to keep such spaces?
(This is equivalent to this: v=' val'; echo $v$v. Why aren't there leading and multiple spaces in output?)
Use More Quotes! echo "$jpat" will do what you want.
There is another issue with what you're doing: Command substitutions will remove trailing newlines. It's not an issue in the printf command you're using, but for example assigning jpat=$(printf " %6i\n" "$jj") would give you exactly the same result as your command.

how to use variable variable names in bash script [duplicate]

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).

Replace the lines if one part of string matches for and writing the same $Variable as it is [duplicate]

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 6 years ago.
have a file call config.php which contains something like this.
<?php
$main="dev.digin.io";
What I need to do is, Whatever the string values which contain after $main= need be replaced with a content in a shell variable like $MainDomain.
Final config.php need to be like this. (consider bash shell variable consist some this like $MainDomain=prod.mail.com )
<?php
$main="prod.mail.com";
This nasty-looking sed substitution gets you what you want:
$ MainDomain=prod.mail.com
$ sed -E "s/^(\\\$main=\")[^\"]+(\";)$/\1$MainDomain\2/" config.php
<?php
$main="prod.mail.com";
Add the -i switch to edit the file "in-place". Use -i.bak to create a backup file config.php.bak.
It looks as though the best thing to do in this situation would be to use a configuration file and adjust your PHP code to read from it, as modifying source code like this is a risky business.
#Daz, could you please try following and let me know if this helps you.
awk -vVAR="$MainDomain" '/\$mainDomain/{sub(/=.*/,"="VAR,$0);} 1' Input_file
Where -vVAR="$MainDomain" VAR is awk's variable which has shell variable named MainDomain's value in it. In awk we can't directly shell variable values so this is how we could assign it to a variable of awk and get it used into it.
Let me know if you have any queries on same.

Appending text to the end of a variable [duplicate]

This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 8 years ago.
The following works, but I don't want the space that it returns:
read input
file= "$input"
file= "$file ins.b" # how to get rid of the space here?
echo "$file"
This outputs 'file ins.b'
I don't want the space between file and ins.b
If I don't leave that space in the code it returns only '.b'. What can I do to resolve this problem?
Append like:
file="${file}ins.b"
If you don't use braces then it treats fileins as a variable and expands it. Since, it's probably not set it just prints .b.
Related: When do we need curly braces in variables using Bash?
In bash you can also reference variables like ${file}. So this should work for you:
file="${file}ins.b"
You don't need to expand the old value at all; bash has a += operator:
file+="ins.b"
file="${file}ins.b"
or
file=$file"ins.b"

Unable to print value of shell variable stored in another variable [duplicate]

This question already has answers here:
Lookup variable value from string in shell script
(2 answers)
Closed 8 years ago.
I'm trying to find all variables which match a particular pattern and print their values.
test_a="apple"
test_b="banana"
test_c="carrot"
test_d="doughnut"
test_show_all () {
local i
for i in ${!test_*}; do
printf "..$i\n"
# printf "..$i-->${$i}\n"
done
}
The loop is finding the correct variables.
But if I uncomment the second line of the for loop, bash is unhappy with the syntax ${$i}. I thought this should work since $i holds the name of a variable, so I thought ${$i} should expand to value of that stored name.
The indirect variable reference is ${!var}. Change your code to
printf "..$i-->${!i}\n"
and it should work.
Not useful now, but in bash 4.3, you will also be able to use namerefs.
for name in ${!test_*}; do
declare -n value=$name
printf "..$name->$value\n"
done
As a short cut, if you only want to iterate over the values (without regard to the actual name of the variable), you can use
declare -n value
for value in ${!test_*}; do
printf "..$value\n"
done

Resources