Home > Net >  Can't run sh "for-do-done" loop in the Jenkinsfile
Can't run sh "for-do-done" loop in the Jenkinsfile

Time:09-12

I have a file named volumes with the list of volumes, separated by space. Here is an example of the file content

vol-0e9cd38819c7a6cb8 vol-0baba5cee0c7fc89a vol-0e7fae905aaffe3a1

I can delete all of them by using this loop in sh:

for volume in $(cat volumes); do; aws ec2 delete-volume --volume-id $volume; done

But when I try to do it inside of the Jenkinsfile like this:

sh "for volume in \$(cat volumes); do; aws ec2 delete-volume --volume-id \$volume; done"

I get the following error:

syntax error near unexpected token `;'

I've tried to escape the characters in different ways and also different types of the sh block, but it doesn't help, I still get the same error.

Please help me to resolve this issue.

CodePudding user response:

There is no ; after do. It's:

for volume in $(cat volumes); do aws ec2 delete-volume --volume-id $volume; done

for i in $(cat is an anti pattern. https://mywiki.wooledge.org/BashFAQ/001 . In this case I would use xargs.

xargs -n1 aws ec2 delete-volume --volume-id < volumes
  • Related