首页 > 解决方案 > Linux命令删除特定目录以外的目录在管道的shell脚本中失败

问题描述

/home/oracle/jenkins/workspace/test/位置,我有多个目录。我想删除所有目录,除了test1我使用终端中的以下内容 -

rm -rf /home/oracle/jenkins/workspace/test/!("test1")

同样,我想通过 Jenkins 管道实现,因此编写了方法 -

def cleanWorkspaceDir() {
    echo "Cleaning workspace"
    sh '''rm -rf /home/oracle/jenkins/workspace/test/!("test1")
    '''
}

但它给出了错误 - /home/oracle/jenkins/workspace/RedmineAndReviewboardProject/SVNCheckout@tmp/durable-810bac2b/script.sh: line 1: syntax error near unexpected token('`

你能帮我解决这个问题吗?

标签: shelljenkinsjenkins-pipelineshjenkins-groovy

解决方案


您可以尝试以下方法:

def cleanWorkspaceDir() {
    echo "Cleaning workspace"
    sh '''find test/ -mindepth 1 '!' -name test1 -type d -exec rm -rf '{}' +
    '''
}

pipeline {
   agent { label 'slave' }

   stages {
      stage('Hello') {
         steps {
            sh 'mkdir -p test/{a/{p,q},b,c/{r,s},test1,test2}'
            sh 'ls -lR'
            cleanWorkspaceDir()
         }
      }
   }
}

将上面的命令替换find为以下任何内容:

find /home/oracle/jenkins/workspace/test/ -mindepth 1 -type d -not -name test1 -delete

find /home/oracle/jenkins/workspace/test/ -mindepth 1 ! -name 'test1' -type d -exec rm -rf {} +

推荐阅读