我该如何在Groovy字符串的execute方法中传递包含空格的参数?只是像在shell中一样添加空格并不能帮助:
println 'ls "/tmp/folder with spaces"'.execute().text
这将给ls调用三个破碎的参数。
我该如何在Groovy字符串的execute方法中传递包含空格的参数?只是像在shell中一样添加空格并不能帮助:
println 'ls "/tmp/folder with spaces"'.execute().text
这将给ls调用三个破碎的参数。
println(['ls', '/tmp/folder with spaces'].execute().text)
def command = 'bash ~my_app/bin/job-runner.sh -n " MyJob today_date=20130202 " '
File file = new File("hello.sh")
file.delete()
file << ("#!/bin/bash\n")
file << (command)
def proc = "bash hello.sh".execute() // Call *execute* on the file
如果需要处理常规引号、管道等内容,可以使用 bash -c
的一个奇怪技巧。
['bash','-c',
'''
docker container ls --format="{{.ID}}" | xargs -n1 docker container inspect --format='{{.ID}} {{.State.StartedAt}}' | sort -k2,1
'''].execute().text
["bash","-c","带有空格和引号的命令"].execute().text
- lepe对我来说,使用列表有点笨重。
这样做就可以了:
def exec(act) {
def cmd = []
act.split('"').each {
if (it.trim() != "") { cmd += it.trim(); }
}
return cmd.execute().text
}
println exec('ls "/tmp/folder with spaces"')
更复杂的例子:
println runme('mysql "-uroot" "--execute=CREATE DATABASE TESTDB; USE TESTDB; \\. test.sql"');
你尝试过转义空格吗?
println 'ls /tmp/folder\ with\ spaces'.execute().text