Executing bash strings using scala.sys.process - bash

I recently discovered sys.process package in Scala, and was amused by its power.
But when I try to combine it with bash pipes and backticks, I get stuck.
This obviously doesn't work:
scala> "echo `date`" !!
res0: String = "
"`date`
"
I tried to use the bash executable to get the desired behavior:
scala> "bash -e echo `date`" !!
/bin/echo: /bin/echo: cannot execute binary file
java.lang.RuntimeException: Nonzero exit value: 126
What am I doing wrong?
Edit:
scala> "bash -ic 'echo `date`'" !!
`date`': unexpected EOF while looking for matching `''
`date`': syntax error: unexpected end of file
java.lang.RuntimeException: Nonzero exit value: 1

You're doing multiple things wrong actually. You should be using the -c option of bash and you should be using a Seq[String] with each parameter to bash in its own String, or the scala library will just split the String at every space character. (This is why Rex Kerr's solution doesn't work.)
scala> import sys.process.stringSeqToProcess
import sys.process.stringSeqToProcess
scala> Seq("bash", "-c", "echo `date`")!!
res20: String =
"Sun Dec 4 16:40:04 CET 2011
"

Related

How do I source a zsh script from a bash script?

I need to extract some variables and functions from a zsh script into a bash script. Is there any way to do this? What I've tried (some are embarrassingly wrong, but covering everything):
. /script/path.zsh (zsh-isms exist, so it fails)
exec zsh
. /script/path.zsh
exec bash
zsh << 'EOF'
. /script/path.zsh
EOF
chsh -s zsh
. /script/path.zsh
chsh -s bash
This thread is the closest I've found. Unfortunately, I have too many items to import for that to be feasible, and neither script is anywhere near a polyglot. However, the functions and variables that I need to import are polyglots.
You can "scrape" the zsh source file for what you need, then execute the code in bash using eval. Here's an example for doing this for a few functions:
File script.zsh:
test1() {
echo "Hello from test1"
}
test2() {
echo $((1 + $1))
}
File script.sh (bash):
# Specify source script and functions
source_filename="script.zsh"
source_functions=" \
test1 \
test2 \
"
# Perform "sourcing"
function_definitions="$(python -B -c "
import re
with open('${source_filename}', mode='r') as file:
content = file.read()
for func in '${source_functions}'.split():
print(re.search(func + r'\(\).*?\n}', content, flags=re.DOTALL).group())
" )"
eval "${function_definitions}"
# Try out test functions
test1 # Hello from test1
n=5
echo "$n + 1 is $(test2 $n)" # 5 + 1 is 6
Run the bash script and it will make use of the functions test1 and test2 defined in the zsh script:
bash script.sh
The above makes use of Python, specifically its re module. It simply looks for character sequences of the form funcname(), and assumes that the function ends at the first }. So it's not very general, but works if you write your functions in this manner.

Need to Run Bash commands in groovy Jenkins script

While running this Bash command in an sh """ block I am getting an error in the below Groovy Jenkins code.
I am getting this error:
/home/jenkins/workspace/_api-build_features_SSSVCS-12870#tmp/durable-be642e71/script.sh: line 1: syntax error: unterminated quoted string
for the below 2 lines of Groovy code:
aa = sh(script: "aa=\"\$(helm2 version --short --client|awk '{print substr(\$2,1,2)}'\"; echo \$aa", returnStdout: true).trim()
I am using the variable aa in below if then else loop, Please suggest me.
sh """
if [ aa == v2 ]; then
helm package --save=false ${extraArgs} ${PROJ}
else
helm package ${PROJ}
fi
sh """
In the first line, you miss a closing the bracket ')'. In the second block, consider removing the second sh, otherwise will be part of the whole string between the two """.

sh -c: Unterminated quoted string error in groovy call [duplicate]

This question already has an answer here:
Why does this curl command fail when executed in groovy?
(1 answer)
Closed 3 years ago.
I have researched the issue and I think the problem is that I am calling bash from a variable.
There are some great resources including very similar questions on Stackexchange.
The closest match would be this question.
There is an FAQ that tries to help.
I try to call a shell command from groovy script.
Here is a working minimal example:
def working()
{
printf "start\n"
def cmd = "sh -c 'ls'"
def proc = cmd.execute()
proc.waitFor()
if (proc.exitValue() > 1)
{
printf cmd + "\nexitcode:" + proc.exitValue().toString() + "\n"
println "[ERROR] ${proc.getErrorStream()}"
}
printf "end\n"
}
Here is the code that gives me a headache
def notworking()
{
printf "start\n"
def cmd = "sh -c 'command -v ls'"
def proc = cmd.execute()
proc.waitFor()
if (proc.exitValue() > 1)
{
printf cmd + "\nexitcode:" + proc.exitValue().toString() + "\n"
println "[ERROR] ${proc.getErrorStream()}"
}
printf "end\n"
}
I need to use sh in order to call command.
My error output is:
sh -c 'command -v ls'
exitcode:2
[ERROR] -v: 1: -v: Syntax error: Unterminated quoted string
I am pretty sure that this is due to how the arguemnts are actually split.
I am not able to apply any of the array tips from the other questions / responses.
Having done my due dilligance in researching this I am confident this is not a dublicate.
This is also relevant on a broad basis since it can be useful to anyone using jenkins and developing groovy scripts.
I was able to circumvent the split by using an environemnt variable and a different execute function.
def cmd = "sh -c \$args"
def proc = cmd.execute(["args=command -v $command_name"], null)
proc.waitFor()
I found the documentation for the syntax here.
public Process execute(List envp, File dir)
User cfrick points out that there is a better solution here.

Executing shell command in groovy throws 'unexpected char' error

I am trying to execute a shell command n groovy
def shellString = "s/\[\|]\|\s\|'\|(\|)//g"
def temp2 = "echo response| sed -e ${shellString}".execute()
It throws compilation error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 33: unexpected char: '\' # line 33, column 24.
def shellString = "s/\[\|]\|\s\|'\|(\|)//g"
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)
at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350)
at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:139)
at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110)
at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234)
at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:131)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:125)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:330)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
shellString isn't a slashed string, so not sure why a \ would create a problem. Any help is appreciated.
You need to escape slash to avoid compilation errors:
def shellString = "s/\\[\\|]\\|\\s\\|'\\|(\\|)//g"
def temp2 = "echo response| sed -e ${shellString}".execute()
println temp2.text
Output:
response| sed -e s/\[\|]\|\s\|'\|(\|)//g
This will fail on many levels. The biggest problem you will face is the fact, that execute is really just executing a process (not a shell command). So first of all you can not use | at all. Next quoting arguments will not work, because execute will just split at whitespace. So if you want to use "shellisms" use the equivalent of sh -c "..." instead and use execute on a string array. E.g.
["sh", "-c", "..."].execute()
Then you can put your ... shell code in there with all the redirections, quotings, env-vars etc. with the proper Groovy quoting applied as mentioned in the other answer.
And to circumvent all of that: why even bother with sed here? Just use replaceAll on the resulting string on the groovy side of things.

Bash script error. What is wrong with this script?

I'm new at bash script writing and I have this error. I have looked everywhere to find an answer with no success. What is wrong with this script?
#!/bin/bash
exec >> /Users/k_herriage/bin/post-gererate.out 2>&1
date
set -x
mynewfile="~/bin/convert_tst.txt"
myfile=fopen($mynewfile,'w+' );
#echo $myfile
fwrite($myfile, "testing");
fclose($myfile);
exit (0)
line 7: syntax error near unexpected token `('
line 7:`myfile = fopen ( '~/bin/convert_tst.txt','w' );'
Few points:
Calling a function in bash does not require parens, it is syntactically equivalent to a command:
do_something arg1 arg2 arg3
There is no need to do open-append-close sequence in bash, it is perfectly doable with a single command:
echo "testing" >> $mynewfile; ##
>> means "append", where if it was >, it would mean "overwrite" or "discard content". (Both will create the file if it didn't exist.)

Resources