2

这是我当前的目录结构

/ <-- current working Dir
  /php/
    file1.php
    file2.php
    file3.txt

我正在尝试执行以下常规命令

def cp = 'cp -f *.php /tmp/'
def cpProc = cp.execute(null, new File('./php/')
cpProc.waitfor()
log.info 'current exitvalue :' + cpProc.exitValue()
log.info 'current proc out : ' + cpProc.text

但我不断得到cp: cannot stat *.php': No such file or directory,我已经验证了文件存在并且我已经验证了我当前的工作目录

如果我执行log.info 'ls -la'.execute(null, new File('./php/')),我会看到 PHP 和 .txt 文件。

这似乎是一个长镜头,但我认为在指定的工作目录中执行命令时使用通配符可能存在错误,除非我遗漏了什么?

我正在使用 groovy 1.7.5

4

1 回答 1

2

这个版本适合我,试试看:

    #!/usr/bin/env groovy

    command = ["sh", "-c", "cp -f *.php /tmp/"]
    def cpProc = command.execute(null, new File('./php/'))
    cpProc.waitFor()
    print 'current exitvalue :' + cpProc.exitValue() + '\n'
    print 'current proc out : ' + cpProc.text + '\n'

    print 'ls -la'.execute(null, new File('/tmp/')).text

这个问题的第一个答案解释了为什么您的版本不起作用:Groovy execute "cp *" shell command

于 2012-12-28T10:20:15.617 回答