Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading