在groovy脚本中使用简单的双引号

时间:2019-03-26 16:28:22

标签: groovy

我在groovy脚本中有以下代码:

script = ''' 
def val1 = 'val1' 
def val2 = 'val2' 
def cmd = """ cd /path/to/my/folder && python -c 'import MyClass; MyClass.my_method(\"${val1}\",\"${val2}\")' 
"""
def proc = ["bash", "-c", command].execute()
proc.waitFor()
print proc.in.text 
'''; 

我想在脚本之外使用参数:

def val1 = 'val1' 
def val2 = 'val2' 
script = ''' 
def cmd = """ 
cd /path/to/my/folder && python -c 'import MyClass; MyClass.my_method(\"${val1}\",\"${val2}\")' 
""" 
def proc = ["bash", "-c", command].execute()
proc.waitFor()
print proc.in.text
'''; 

我尝试将单引号和双三引号结合使用,但是它不起作用,您是否知道如何解决此问题?

我想在脚本之外使用参数:

def val1 = 'val1' 
def val2 = 'val2' 
script = """ 
def cmd = """ 
cd /path/to/my/folder && python -c 'import MyClass; MyClass.my_method(\"${val1}\",\"${val2}\")' 
""" 
def proc = ["bash", "-c", command].execute()
proc.waitFor()
print proc.in.text
""";

2 个答案:

答案 0 :(得分:1)

我想知道这是否是您所需要的:在外部用三重双引号替换占位符

def val1 = 'val1' 
def val2 = 'val2' 
script = """ 
    def cmd = '''
        if cd /path/to/my/folder; then
            python -c 'import MyClass; MyClass.my_method("${val1}","${val2}")' 
        fi
    '''
    def proc = ["bash", "-c", command].execute()
    proc.waitFor()
    print proc.in.text
"""

答案 1 :(得分:0)

很难确定您需要什么,但是类似这样的事情会更有意义...

def val1 = 'val1' 
def val2 = 'val2'
def cmd = "cd /path/to/my/folder && python -c 'import MyClass; MyClass.my_method(${val1},${val2})'" 

script = """\
def proc = ["bash", "-c", ${cmd}].execute()
proc.waitFor()
print proc.in.text
"""

如果您确实想在cmd ...

中使用双引号,就可以这样。

def val1 = 'val1' 
def val2 = 'val2'
def cmd = "cd /path/to/my/folder && python -c 'import MyClass; MyClass.my_method(\"${val1}\",\"${val2}\")'" 

script = """\
def proc = ["bash", "-c", ${cmd}].execute()
proc.waitFor()
print proc.in.text
"""

我希望有帮助。