jenkins escape sed command - jenkins

Can someone escape this sed shell command in Jenkins groovy script for me?
So hard.
sh ("""
sed "s/(AssemblyInformationalVersion\(\")(.*)(\")/\1${productVersion}\3/g"
AssemblyInfoGlobal/AssemblyInfoGlobal.cs -r
""")

The triple-double-quote (""") string literal syntax allows for variable/expression substitution (interpolation), so the backslash (\) is interpreted as a special character "escape". Since the first open paren is not such a special character, Groovy compilation fails. If your intent is to have literal backslashes in the resulting string, you need to escape the backslashes. That is, use a double-backslash (\\) to substitute for one literal backslash.
Thus:
sh ("""
sed "s/(AssemblyInformationalVersion\\(\\")(.*)(\\")/\\1${productVersion}\\3/g"
AssemblyInfoGlobal/AssemblyInfoGlobal.cs -r
""")

So if you like to replace some chars or word in an String groovy variable, for example replacing "/" with "/" in order to escape an special character, which in our case will be the forward slash you can use the code below.
So afterwards we'll be able to apply the linux sed command without getting an error (for instance using sed to replace a place holder value with the desired value in an .env file).
Below we show a Jenkins Pipeline Groovy code:
String s = "A/B/C"
your_variable = s.replace("/", "\\/")
sh "sed -i -e 's/string_to_replace/${your_variable}/g' path/to/file/.env"
NOTE: ${your_variable} will get the content of your variable.
VALIDATION:
echo "Your variable new content: ${your_variable}"
RESULT:
Your variable new content: A\/B\/C

Related

illegal string body character after dollar sign in Jenkins pipeline

I am able to fetch VAULT Secrets from shell script "vault kv get -field=vCenter test/hello" but unable to do same using Jenkins pipeline because of the syntax.
cd $WORKSPACE#script && ./getvms.py -u zzz -p $(vault kv get -field=vCenter test/hello
error
WorkflowScript: 5: illegal string body character after dollar sign;
solution: either escape a literal dollar sign "$5" or bracket the value expression "${5}" # line 5, column 81.
reLinuxBuild#fplu.fpl.com -p $(vault kv

Jenkins sed Terraform shell

I have a Jenkins pipeline that I need to run a sed on a file but I am getting an error of line 2: syntax error: unexpected ")"
My file is this:-
name=""
age=""
My Jenkins sh line is:
"""sed -i -e 's|(name *= *")"|\1${params.NAME}"|g' -e 's|(age *= *")"|\1${params.AGE}"|g' vars.txt"""
I can run the sed on my shell fine and it works, but Jenkins doesn't like it for some reason.
If I run it through the Jenkins Pipeline Syntax Generator I get the same error.
You are using BRE POSIX pattern, and to create a capturing group there, you need to use escaped parentheses, \(...\). However, in the triple-quoted string literal, you need to escape the backslash to get a literal backslash in the resulting string.
You need to fix the line you have like this:
'''sed -i' ' -e 's|\\(name *= *"\\)"|\\1'"${params.NAME}"'"|g' -e 's|\\(age *= *"\\)"|\\1'"${params.AGE}"'"|g' vars.txt'''

Why this command does not work in Jenkins?

I am a Jenkins beginner.
Why does this command work?
sed -i -E s/'image: '(.*)${stack_name}-${service_name}:.*\$/'image: '\1${stack_name}-${service_name}:${version}/g
And why does the same command not work when it is included in a Jenkinsfile?
sh "sed -i -E s/'image: '(.*)${stack_name}-${service_name}:.*\$/'image: '\1${stack_name}-${service_name}:${version}/g"
The error is:
/opt/jenkins_data/workspace/secuview-front_master-Z2ADTSIGTSEJOG3UYRU4FPDUF5VZMB3SMQLEOUD46TUZG4POWKYQ#tmp/durable-a484faaf/script.sh: line 2: syntax error near unexpected token `('
Under the hood Jenkinsfiles are essentially Apache Groovy scripts, therefore string escaping rules for Groovy apply. When you have slashes they need to be escaped (e.g. \ -> \\) and when you're using double quotes using ${} literals actually get interpreted by the script instead of being passed to the shell step.
Try this instead:
sh 'sed -i -E s/\'image: \'\\(.*\\)${stack_name}-${service_name}:.*\\$/\'image: \'\\1${stack_name}-${service_name}:${version}/g'

How to include special character in a jenkinsfile

When i do a sed in my jenkinsfile as
"sh "sed -i 's|HEAP=.*|HEAP="\-Xms1024m \-Xmx1024m"|' $DIR/bin/myfile"
i get the error as unexpected char :'\'.
How to specify special characters in a jenkinsfile??
Try using
"""sh sed -i 's|HEAP=.*|HEAP="\-Xms1024m \-Xmx1024m"|' $DIR/bin/myfile"""
Use the \ character to escape a character.

Get specific value of preprocessor macro

In my build settings i have define some preprocessor macros
i.e. SANDBOX_ENV=1
I want to use the value of SANDBOX_ENV in my shell script.
I have tried echo "SANDBOX value is = ${GCC_PREPROCESSOR_DEFINITIONS}"
but its giving me all macros values like DEBUG=1 SANDBOX_ENV=1 COCOAPODS=1
I want to use value that is assigned to SANDBOX_ENV
Try this:
#!/bin/bash
GCC_PREPROCESSOR_DEFINITIONS="DEBUG=1 SANDBOX_ENV=1 COCOAPODS=1"
# delete everything before our value ans stuff into TMPVAL
TMPVAL="${GCC_PREPROCESSOR_DEFINITIONS//*SANDBOX_ENV=/}"
# remove everything after our value from TMPVAL and return it
TMPVAL="${TMPVAL// */}"
echo $TMPVAL; #outputs 1
HTH,
bovako
You should be able to parse it easily with awk or something, but here's how I'd do it:
echo $GCC_PREPROCESSOR_DEFINITIONS | grep -Po 'SANDBOX_ENV=\d+' | sed 's/SANDBOX_ENV=//'
In your echo context:
echo "SANDBOX value is $(echo $GCC_PREPROCESSOR_DEFINITIONS | grep -Po 'SANDBOX_ENV=\d+' | sed 's/SANDBOX_ENV=//')"
Basically I piped the contents of GCC_PREPROCESSOR_DEFINITIONS and grepped out the SANDBOX_ENV portion.
grep -P
is to use the Perl regex \d+, because I don't like POSIX. Just a preference. Essentially what
grep -P 'SANDBOX_ENV=\d+'
does is to find the line in the content piped to it that contains the string "SANDBOX_ENV=" and any number of digits succeeding it. If the value might contain alphanumerics you can change the \d for digits to \w for word which encompasses a-zA-Z0-9 and you get:
grep -Po 'SANDBOX_ENV=\w+'
The + just means there must be at least one character of the type specified by the character before it, including all succeeding characters that matches.
the -o (only-matching) in grep -Po is used to isolate the match so that instead of the entire line you just get "SANDBOX_ENV=1".
This output is then piped to the sed command where I do a simple find and replace where I replaced "SANDBOX_ENV=" with "", leaving only the value behind it. There are probably easier ways to do it like with awk, but you'll have to learn that yourself.
If you want to have something self contained within the Build Settings and you don't mind slight indirection, then:
Create User-Defined settings SANDBOX_ENV=1 (or whatever value you want)
In Preprocessor Macros, add SANDBOX_ENV=${SANDBOX_ENV}
In your shell, to test, do
echo ${SANDBOX_ENV}
With the User-Defined Settings, you'll still be able to modify the value for Build Configuration and Architecture. So, for example, you could make the Debug config be SANDBOX_ENV=0 and Release be SANDBOX_ENV=1.
Might be the obvious answer, but have you simply tried:
echo ${SANDBOX_ENV}
If that doesn't work, try using eval:
eval "${GCC_PREPROCESSOR_DEFINITIONS}"
echo ${SANDBOX_ENV}

Resources