Home > Enterprise >  Jenkins use expression/parenthesis in shell command
Jenkins use expression/parenthesis in shell command

Time:06-08

How to make file expressions work with Jenkinsfile? I want to run simple command rm -rv !(_deps) to remove all populated files except _deps directory. What I've tried so far:

  • sh '''rm -rv !(_deps)''' which caused script.sh: line 1: syntax error near unexpected token `('
  • sh 'rm -rv !\\(_deps\\)' which caused rm: cannot remove '!(_deps)': No such file or directory (but it DOES exist)
  • sh 'rm -rv !\(_deps\)' which caused unexpected char: '\'
  • sh 'rm -rv !(_deps)' which caused syntax error near unexpected token `('

CodePudding user response:

In Bash to use pattern matching you have to enable extglob:

So the following will work. Make sure your shell executor is set to bash.

sh """
   shopt -s extglob
   rm -rv !(_deps)
  """

If you don't want to enable extglob you can use a different command to get the directories deleted. Following is one option.

ls | grep -v _deps | xargs rm -rv

In Jenkins

 sh """
    ls | grep -v _deps | xargs rm -rv
    """
  • Related