我希望能够在Groovy脚本中执行一个shell命令,其中脚本的一部分是文件名变量,该变量可能包含空格。
根据 groovy execute with parameters containing spaces 的解释,我可以使用数组来处理这个问题。
根据 groovy execute with parameters containing spaces 的解释,我可以使用数组来处理这个问题。
def filename = '/tmp/folder with spaces'
// does not work
'ls "' + filename + '"'.execute()
// works
['ls', filename].execute()
但我不知道如何使用数组重新格式化以下命令,其中“filename”是一个包含有或没有空格的文件名的Groovy变量:
mdfind "$(/usr/libexec/PlistBuddy -c 'Print RawQuery' filename)"
这个不起作用:
// not working
["mdfind", "\$(/usr/libexec/PlistBuddy -c 'Print RawQuery' " + filename + ")"].execute()
["mdfind", "\$(/usr/libexec/PlistBuddy -c 'Print RawQuery' ", filename, ")"].execute()