0

In the Jenkinsfile under script section, I am using the following code to return output something like the following.

deleting 1
deleting 2
deleting 3

I have tried the following script but it seems to be not working.

script {
       prdirectory_lists = ['1','2','3']
       def size3 = prdirectory_lists.size()

        for(k=0;k<size3;k++){
        sh(returnStdout: true, script: 'ssh user1@192.168.1.12 echo deleting prdirectory_lists[k] ')
        //sh(returnStdout: true, script: 'ssh user1@192.168.1.12 echo deleting ${prdirectory_lists[k]} ')
        //sh(returnStdout: true, script: 'ssh user1@192.168.1.12 echo "deleting ${prdirectory_lists[k]}" ')
         }
}
Michael Kemmerzell
  • 4,802
  • 4
  • 27
  • 43
satish kumar
  • 45
  • 1
  • 10

3 Answers3

1

You have to use the correct String Interpolation which is "${variable}" to access the value of a variable.

sh(returnStdout: true, script: 'ssh user1@192.168.1.12 echo deleting "${prdirectory_lists[k]}" ')

The official Jenkins documentation has some good examples for this: String interpolation

Michael Kemmerzell
  • 4,802
  • 4
  • 27
  • 43
0

Try this:

        sh returnStdout: true, 
           script: """ssh user1@192.168.1.12 echo "deleting ${prdirectory_lists[k]}" """
MaratC
  • 6,418
  • 2
  • 20
  • 27
0

You always use single quoted strings that do not have a string interpolation facility

The following should work:

def list = ['1','2','3']

list.each {item ->
   sh(returnStdout: true, script: "ssh user1@192.168.1.12 echo deleting $item")
}

All in all there are three types of strings in Groovy:

  1. Single Quoted - like usual java strings without string interpolation
  2. Double Quoted - Single line strings with an interpolation facility
  3. Triple Quoted - Multi line string with an interpolation facility

I believe Doble Quoted strings will suit you best in this case

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97