Linux命令删除特定目录以外的目录的操作在管道的Shell脚本中失败

时间:2020-04-24 10:19:51

标签: shell jenkins jenkins-pipeline sh jenkins-groovy

/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('`

能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

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 {} +