Jenkins sed Terraform shell - jenkins

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'''

Related

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.

How to escape this sed command in a Jenkinsfile Pipeline?

I'm doing a simple substitution, and this works fine at the command line:
sed "s/pub Url =.*/pub Url = 'https:\/\/example.com:3207';/g" myfile.ts
I'm trying to run it within a Jenkinsfile, and like 40 builds later I cannot get the escape quoting right.
Pretty sure it will look something like this:
sh 'sed \\"s/pub Url =.*/pub Url = \\'https:\\\/\\\/example.com:3207\\';/g\\" myfile.ts'
Yet that results in the following error:
WorkflowScript: 4: unexpected char: '\' # line 4, column 49.
ub Url =.*/pub Url = \\'https:\\\/\\\/ex
I feel like I've tried dozens of variants but nothing is working.
Here is among the most common errors I'm getting: which points to escaping issue
sed: -e expression #1, char 1: unknown command: `"'
I really just need a pipeline expert that can likely see exactly what I'm doing wrong and know where to quote it.
As noted here: https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4 this is not uncommon to fight this type of stuff in the pipeline files and it seems like it's just trial and error.
Ok after much trial and error, this is working.
Looks like I had to use triple single quotes around the command. Good thing I don't need to interpolate!
sh '''sed \"s/pub Url =.*/pub Url = \\'https:\\/\\/example.com:3207\\';/g\" afile.txt'''
Hope this is helpful to someone in the future that's fighting this!
To add more on this... I had an issue with sed -i 's/\_/\//g' abc.txt command running fine in CLI but resulting in an unxpected char \ error in jenkins, so, i had to replace this:
sed -i 's/\_/\//g' abc.txt
with
sed -i "s/\\_/\\//g" abc.txt
To remove this error from jenkins.

jenkins escape sed command

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

Bind key prefix + ; to open command-prompt in tmux

Normal way to open the command-prompt in tmux is prefix + :. I want to bind the sequence prefix + ; to open the command prompt. I am too lazy to hit the shift key.
When I put this in my tmux.conf: bind-key ; command-prompt, I get this error: /Users/skilbjo/.tmux.conf:19: usage: bind-key [-cnr] [-t mode-table] [-T key-table] key command [arguments]
which is funny, because when I do prefix + ? (alias for tmux list-keys), this is listed: bind-key -T prefix : command-prompt. How does this sorcery work? I even tried bind-key -T prefix ; command-prompt to no avail, same error message
tmux uses semicolon as a command separator.
From the tmux man page:
Multiple commands may be specified together as part of a command sequence. Each command should be separated by spaces and a semicolon; commands are executed sequentially from left to right and lines ending with a backslash continue on to the next line, except when escaped by another backslash. A literal semicolon may be included by escaping it with a backslash (for example, when specifying a command sequence to bind-key).
What you'll want to do is:
unbind-key \;
bind-key \; command-prompt

Resources