Attempt to split string on '//' in Jenkinsfile splits on '/' instead - regex

What is the correct way to tokenize a string on double-forward-slash // in a Jenkinsfile?
The example below results in the string being tokenized on single-forward-slash / instead, which is not the desired behavior.
Jenkinsfile
An abbreviated, over-simplified example of the Jenkinsfile containing the relevant part is:
node {
// Clean workspace before doing anything
deleteDir()
try {
stage ('Clone') {
def theURL = "http://<ip-on-lan>:<port-num>/path/to/some.asset"
sh "echo 'theURL is: ${theURL}'"
def tokenizedURL = theURL.tokenize('//')
sh "echo 'tokenizedURL is: ${tokenizedURL}'"
}
} catch (err) {
currentBuild.result = 'FAILED'
throw err
}
}
The Logs:
The log output from the preceding is:
echo 'theURL is: http://<ip-on-lan>:<port-num>/path/to/some.asset'— Shell Script<1s
[ne_Branch-Name-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA] Running shell script
+ echo theURL is: http://<ip-on-lan>:<port-num>/path/to/some.asset
theURL is: http://<ip-on-lan>:<port-num>/path/to/some.asset
echo 'tokenizedURL is: [http:, <ip-on-lan>:<port-num>, path, to, some.asset]'— Shell Script<1s
[ne_Branch-Name-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA] Running shell script
+ echo tokenizedURL is: [http:, <ip-on-lan>:<port-num>, path, to, some.asset]
tokenizedURL is: [http:, <ip-on-lan>:<port-num>, path, to, some.asset]
Note that the logs show that the string is being tokeni on / instead of on //.

tokenize takes string as optional argument that may contain 1 or more characters as delimiters. It treats each character in string argument as separate delimiter so // is effectively same as /
To split on //, you may use split that supports regex:
theURL.split(/\/{2}/)
Code Demo

Related

Implementing a 'for loop' within user_data provisioner file

I am currently using a template_file to provision user_data into an aws_launch_configuration, like so:
resource "aws_launch_configuration" "launch_config" {
...
user_data = "${data.template_file.init.rendered}"
}
data "template_file" "init" {
template = "${file("router-init.sh.tpl")}"
vars {
hub_ip_addresses = "${join(",", aws_instance.gridHub.*.private_ip)}"
}
}
I am feeding in a variable (i.e. hub_ip_addresses) into the router-init.sh.tpl file, and in this file I am making use of the argument like so:
`#!/bin/sh
...
IFS=',' read -r -a array <<< "$hub_ip_addresses"
for element in "${array[#]}"
do
#do stuff with $element
done
Basically, I am splitting the string based on a delimiter, and then looping through each ip address in the array.
This bash script works fine when I run it on my local machine -- however, when terraform executes it, it throws a error:
* data.template_file.init: data.template_file.init: failed to render : parse error at 13:25: expected expression but found invalid sequence "#"
I'm supposing the '#' symbol is causing an issue. Is there a reason why this is so? Do I need to escape it with a '\' ?
EDIT: Not sure if related to this issue, but in the preceeding line in the bash script, IFS=',' read -r -a array <<< "$hub_ip_addresses", the <<< seems to be causing everything else that follows to look as if they are inside a comment (i.e. greyed out as if it was within a quotation mark ').)
You need to escape the $ characters in your template by doubling them up or Terraform will attempt to interpolate them as the input variables to the template.
The template docs cover this briefly although the example given is for inline templates rather than for all templates, including those that are loaded with the file() function.
So something like:
#!/bin/sh
...
IFS=',' read -r -a array <<< "$hub_ip_addresses"
for element in "$${array[#]}"
do
#do stuff with $$element
done

Raw string to a Powershell script through command line

I have powershell script which takes input from a python GUI. And the script looks like (foo.ps1):
#.\foo.ps1
[String]$UserName = $args[0]
[String]$Password = $args[1]
[String[]] $ComputerName = $args[2]
[String[]] $Users = $args[3]
Write-Output "Username is $UserName"
Write-Output "Password is $Password"
foreach ($Computer in $ComputerName) {
Write-Output "Computer is $Computer"
}
foreach ($User in $Users) {
Write-Output "Users is $User"
}
And my execution line in powershell window looks like:
PS dir>powershell -executionpolicy bypass -Command .\foo.ps1 "my_username" "my_password" "Computer1, Computer2" "User1, User2"
Problem arises when my password has special characters like '}', '{','(', ')', ',', '&', ';' need to be passed within these apostrophes. But when it contains characters like ' " `, it throws an exception: "The string is Missing the terminator: '.
How do I solve it. I use python subprocess to input that line to powershell using variables from that python script.
import subprocess
process = subprocess.Popen(['powershell.exe',"""powershell -executionpolicy bypass -File Foo.ps1 "username" "password" "computer1,computer2" "user1,user2" """], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
print(process.communicate())
Expecting a simple way to address this issue.
When we enter the special characters through prompt, that is from 'read-host' line in powershell, it accepts everything without any trouble.
Is there anyway that I could automate that read-host prompt input using python subprocess ?
Try sending all the cmd arguments in a list format.
import subprocess
process = subprocess.Popen("""powershell.exe powershell -executionpolicy bypass -File Foo.ps1 "username" "password" "computer1,computer2" "user1,user2" """.split(), stdin=subprocess.PIPE,stdout=subprocess.PIPE)
print(process.communicate())

Avoid line return when calling AINSI escape sequence

I made this function who change the title of a terminal window by using ainsi escape sequence but after the call of this function a line is jumped in console, how avoid this ?
void setConsoleTitle(std::string const& title)
{
m_title = title;
std::string cmd1 = "echo \"\033]0;";
cmd1 += title;
cmd1 += "\007\"";
system(cmd1.c_str());
}
Thanks.
ReallY DON'T use system here (it starts a new process, in which a shell is run, and then runs echo in that shell, and tears down that new process - which is a lot of work to output a handful of characters to the screen, that can just as well be output with cout or similar - system is acceptable to use if you, say, need to run a compiler or unpack a zip file - something that isn't easy to do in your own program). But if you insist on using system, then use echo -n ..., where man echo explains it as:
-n do not output the trailing newline
However, just using cout will do fine here:
cout << "\033]0;" << title << "\a";
(\a is "alarm", the same as \007 but portable in case your system doesn't use character number 7 for "ring the bell")
Why you want to print "noise" is beyond me, but the above does the same thing as your "echo" command.

groovy regex tokenize().grep() function not producing expected answer

I am attempting to extract the Version tags from a git repo using some regex functions from groovy.
def command = "\"C:/Program Files (x86)/Git/bin/sh.exe\" --login -i -c \"git ls-remote user#mysever:myproject.git refs/tags/[vV]*?.*?.*?\""
def proc = command.execute()
proc.waitFor()
def str = proc.in.text
def pattern = ~/^refs\/tags\/[vV][0-9]+\.[0-9]+\.[0-9]+$/
System.out.println("is everything a string? " + str.tokenize().every{ it instanceof String })
System.out.println("String: $str")
System.out.println("split: ${str.split()}")
System.out.println("contains refs/tags/V0.0.0?: ${str.split()*.trim().contains('refs/tags/V0.0.0')}")
System.out.println("contains refs/tags/V0.0.1?: ${str.split()*.trim().contains('refs/tags/V0.0.1')}")
System.out.println("grep: " + ['refs/tags/V0.0.0', 'refs/tags/V0.0.1'].grep(pattern))
System.out.println("split grep: " + str.split().grep(pattern))
System.out.println("split findAll: " + str.split().findAll{ it =~ pattern })
And my output is this:
String: SHA1asdf refs/tags/V0.0.0
SHA1fdsa refs/tags/V0.0.0^{}
SHA1hgfd refs/tags/V0.0.l
SHA1dfgh refs/tags/V0.0.l^{}
split: [SHA1asdf, refs/tags/V0.0.0, SHA1asdf, refs/tags/V0.0.0^{}, SHA1hgfd, refs/tags/V0.0.l, SHA1dfgh, refs/tags/V0.0.l^{}]
contains refs/tags/V0.0.0?: true
contains refs/tags/V0.0.1?: false
grep: [refs/tags/V0.0.0, refs/tags/V0.0.1]
split grep: [refs/tags/V0.0.0]
split findAll: [refs/tags/V0.0.0]
Why isn't the tokenize().grep() producing the expected answer?
I am using Groovy 2.3.6
UPDATE: I've added more test results, see code and output
Not sure why, but your version V0.0.1 is actually an character l and not 1.
If you just the run the following sample (just changed the str), grep will work just fine:
def str = """
SHA1asdf refs/tags/V0.0.0
SHA1fdsa refs/tags/V0.0.0^{}
SHA1hgfd refs/tags/V0.0.1
SHA1dfgh refs/tags/V0.0.1^{}
"""
def pattern = ~/^refs\/tags\/[vV][0-9]+\.[0-9]+\.[0-9]+$/
System.out.println("String: $str")
System.out.println("tokenize: ${str.tokenize()}")
System.out.println("grep: " + ["refs/tags/V0.0.0", "refs/tags/V0.0.1"].grep(pattern))
System.out.println("tokenize grep: " + str.tokenize().grep(pattern))
produces
grep: [refs/tags/V0.0.0, refs/tags/V0.0.1]
tokenize grep: [refs/tags/V0.0.0, refs/tags/V0.0.1]

Find and return a specific area of a text

Hello everyone and happy new year! I have a text from which I like to return a specific area from a specific string using a python script!
This is the text I have
#!/bin/sh
case "$1" in
start)
ulimit -s 1024
/usr/bin/oscam --config-dir /etc/tuxbox/config/oscam --daemon --pidfile /tmp/oscam.pid --restart 2 --utf8
;;
stop)
kill `cat /tmp/oscam.pid` 2> /dev/null
;;
restart|reload)
$0 stop
sleep 1
$0 start
;;
version)
echo "svn8631"
;;
info)
echo "oscam svn8631"
;;
*)
echo "Usage: $0 start|stop|restart"
exit 1
;;
esac
exit 0
from the text above I need to create a python command that always returns only what is written where the oscam svn8631 is written between the quotes! So far the only thing I managed to do is return another area of this text using this code
try:
f = open("/etc/init.d/softcam", "r")
content = f.read()
f.close()
except:
content = ""
contentInfo = content.split("\n")
if (content != ""):
for line in contentInfo:
if line.__contains__("usr/bin/"):
idex = line.index("n/")
line = line[(idex + 2):]
return line
This of course returns what's after the usr/bin text, and I need another area which comes after the word info). Can anyone help me please? I don't know how to make my script read this specific area I need! Thanks in advance!
Using the content variable that you have created, the
"oscam svn8631" text can be extracted with:
for chunk in content.split(";;"):
if chunk.strip().startswith('info)'):
return chunk.split('"')[1]
The natural way to break up a bash case statement is by splitting it on ";;". Then, we identify the info section (chunk.strip().startswith('info)')) and select the first quoted string in that section (chunk.split('"')[1]).