Using Cygwin sed, on windows command prompt, sed takes \\\ to convert a path to : e.g.
C:\>echo 'PATH_TO_SOURCES' | sed 's/PATH_TO_SOURCES/\C:\\\WorkArea\\\workspace\\\sources/g'
would result in C:\WorkArea\workspace\sources
But when I have to use the same in Jenkins pipeline, it takes \\\\\\ in the path to convert it to single . Here is the command line I had to generate to give me 'C:\WorkArea\workspace\sources'
stage('SED')
{
steps {
script {
cfgFile="MyCfg.xml"
dir(AutomationScripts) {
bat (""" cat MyCfg.xml | sed "s/PATH_TO_SOURCES/\C:\\\\\\WorkArea\\\\\\workspace\\\\\\sources/g" > newMyCfg.xml""")
}
}
}
}
Update: I experimented by changing quotes as mentioned in the comments, but nothing seemed to be changed. Here are the pipelines (starting with bat) and the results:
Pipeline: bat (" echo 'PATH_TO_SOURCES' | c:\\cygwin64\\bin\\sed 's/PATH_TO_SOURCES/\\C:\\\\\\WorkArea\\\\\\workspace\\\\\\sources/g' ")
Output: C:\>echo 'PATH_TO_SOURCES' | c:\cygwin64\bin\sed 's/PATH_TO_SOURCES/\C:\\\WorkArea\\\workspace\\\sources/g'
'C:\WorkArea\workspace\sources'
Pipeline: bat (" echo 'PATH_TO_SOURCES' | c:\\cygwin64\\bin\\sed 's/PATH_TO_SOURCES/\\C:\\WorkArea\\workspace\\sources/g'")
Output: C:\>echo 'PATH_TO_SOURCES' | c:\cygwin64\bin\sed 's/PATH_TO_SOURCES/\C:\WorkArea\workspace\sources/g'
'C:WorkAreaworkspacesources'
Pipeline: bat ("""echo 'PATH_TO_SOURCES' | c:\\cygwin64\\bin\\sed "s/PATH_TO_SOURCES/\\C:\\WorkArea\\workspace\\sources/g" """)
Output: C:\>echo 'PATH_TO_SOURCES' | c:\cygwin64\bin\sed "s/PATH_TO_SOURCES/\C:\WorkArea\workspace\sources/g"
'C:WorkAreaworkspacesources'
Pipeline: bat ("echo 'PATH_TO_SOURCES' | c:\\cygwin64\\bin\\sed 's/PATH_TO_SOURCES/\\C:\\\\WorkArea\\\\workspace\\\\sources/g'")
Output: C:\>echo 'PATH_TO_SOURCES' | c:\cygwin64\bin\sed 's/PATH_TO_SOURCES/\C:\\WorkArea\\workspace\\sources/g'
'C:WorkAreaworkspacesources'
Pipeline: bat ("""echo 'PATH_TO_SOURCES' | c:\\cygwin64\\bin\\sed "s/PATH_TO_SOURCES/\\C:\\\\WorkArea\\\\workspace\\\\sources/g" """)
Output: C:\>echo 'PATH_TO_SOURCES' | c:\cygwin64\bin\sed "s/PATH_TO_SOURCES/\C:\\WorkArea\\workspace\\sources/g"
'C:WorkAreaworkspacesources'
Although, it was a bat call and supposed to behave the same as it happens at the command prompt.
Looks like Pipeline itself escaping \.
Any idea if I'm missing something here?
I realized that that groovy pipeline script escapes \, so to get C:\Program Files\Microsoft, so have to use \\ i.e. C:\\Program Files\\Microsoft.
On the other hand, (Observed in Windows cygwin 64) sed takes \\\ to get \ as mentioned in my original post i.e.
C:\>echo 'PATH_TO_SOURCES' | sed 's/PATH_TO_SOURCES/\C:\\\WorkArea\\\workspace\\\sources/g' would result in C:\WorkArea\workspace\sources
So using sed in the groovy pipeline, at first groovy would do the escaping in the path string and then the result would be passed on to the sed, which would again do its own escaping on the string, so we have to add \\\\\\ in our paths, which would be escaped by the groovy and it would become \\\. Then sed would escape \\\ to a \. Here is the example I posted, is working like that:
Pipeline: bat (" echo 'PATH_TO_SOURCES' | c:\\cygwin64\\bin\\sed 's/PATH_TO_SOURCES/\\C:\\\\\\WorkArea\\\\\\workspace\\\\\\sources/g' ")
Output: C:\>echo 'PATH_TO_SOURCES' | c:\cygwin64\bin\sed 's/PATH_TO_SOURCES/\C:\\\WorkArea\\\workspace\\\sources/g'
'C:\WorkArea\workspace\sources'
Related
I am trying to convert a shell script to PowerShell script, I am facing issue with below shell commands
label=`echo -n "signing key" | hexdump -ve '/1 "%02x"'`
sign_nist="00000001${label}0000000100"
/testing$ echo $sign_nist
000000017369676e696e67206b65790000000100
/testing$ echo -n $sign_nist | sed -e 's/../\\x&/g'
\x00\x00\x00\x01\x73\x69\x67\x6e\x69\x6e\x67\x20\x6b\x65\x79\x00\x00\x00\x01\x00
/testing$ echo -ne "$(echo -n $sign_nist | sed -e 's/../\\x&/g')"
signing key
when I try to do the same thing in PowerShell using the below commands my output is different
PS C:\testing> $sign_nist
000000017369676E696E67206B65790000000100
PS C:\testing>
$prep = $sign_nist -split '(..)' -ne ''
for($i = 0; $i -lt $prep.length; $i++)
{
$prep[$i] = [System.Convert]::ToUInt32($prep[$i],16)
}
$sign_key_input=$prep -join '';
$sign_key_input
00011151051031101051101033210710112100010
could someone please help me how to get signing key to $sign_key_input with PowerShell?
Thanks in advance for the help.
I think appending \x for each byte works for linux to treat it as hex value but how to indicate the same in powershell?
I try to get information from a Linux server in a powershell script
I use the Windows forms and i get the informations in a RichTextBox.
There is no problem for some basic commands.
Example :
$infoversion = Invoke-SSHCommand -Index 0 -Command "uname -r"
$Result_Info.text += "Kernel Version : $($infoversion.Output) + ("`n")
But when i use a command with a pipe and the '$' character, it doesn't work.
Example :
$infouptime = Invoke-SSHCommand -Index 0 -Command "uptime -p | awk '{print $2,$3,$4,$5,$6,$7}'"
$Result_Info.text += "Server up since $($infouptime.Output)" + ("`n")
(This command work directly on the server)
I try to set \$ or 'e or '$' or "+chr(38)+" but nothing works
If someone can help me, it will be nice :)
PowerShell uses the backtick ` as the escape character in expandable strings:
$infouptime = Invoke-SSHCommand -Index 0 -Command "uptime -p | awk '{print `$2,`$3,`$4,`$5,`$6,`$7}'"
Alternatively, since you don't actually have any variables that need expanding, you could use a single-quoted string literal - the escape sequence for a literal ' is simply '':
$infouptime = Invoke-SSHCommand -Index 0 -Command 'uptime -p | awk ''{print $2,$3,$4,$5,$6,$7}'''
Bash works as expected
$ echo '{"foo": "0"}' | jq 'select(.foo == "0")'
{
"foo": "0"
}
But in Powershell,
PS> echo '{"foo": "0"}' | jq 'select(.foo == "0")'
PS>
Why powershell results this?
You have to backslash the doublequotes unfortunately.
'{"foo": "0"}
{"foo": "1"}' | jq -c 'select(.foo == \"0\")'
{"foo":"0"}
I was trying to run a PowerShell script saved in a text document by using the following:
cat myScript.txt | Invoke-Expression
It works when each command is on its own line but throws an error saying I am missing syntax:
Invoke-Expression: At line:1 char:14
+ function foo {
+ ~
Missing closing '}' in statement block or type definition.
The code in myScript.txt is as follows:
function foo {
param([int]$a, [int]$b)
echo "$a + $b"
}
foo 1 2
Is there a way to run this script without changing the text file to merge all functions to one line?
You can also use out-string to convert output into string.
cat myScript.txt | out-string | Invoke-Expression
I have a few filenames in a directory
blabla.01
blabla.02
...
I'm trying to make a new file with the following format:
01 new stuff here
02 more new stuff
...
I wrote a script and dumbed it down a bit:
#!/bin/bash
FILES=$(find . -type f -name "blabla*" | awk -F'[.]' '$(NF-1)>=1' | sort)
for f in $FILES
do
echo -n $f | cut -d "." -f 3
echo "test"
done
the 'test' will be the output of another code..
However in this example i get something like:
01
test02
test
Thanks
Try
printf '%s%s\n' "$(echo "$f" | cut -d . -f3)" "test"
The echo | cut could probably be replaced with something like "${f##*.}" if you always want the laat field.