Yuriy
Yuriy

Reputation: 31

Loop variable is not passed to shell script in Jenkins

I am trying to pass a loop variable to a shell script, but my shell script is getting an empty variable value

pipeline {
 agent any     
  stages {     
   stage('Test') {     
    steps {                 
     script {
      elements = ['1', '2', '3', '4']
      for (String val in elements){
       echo val
       check = sh (script: """bash ./check_access/check.sh ${val}""", returnStdout: true).trim()                            
       echo "check: ${check}"                          
      }
     }
    }
   }
  }
}

./check_access/check.sh

#!/bin/sh
echo "val: $val"

the result i get

18:13:30  1
18:13:31  + bash ./check_access/check.sh 1
18:13:31  check: val:
.
.
.
18:13:31  4
18:13:32  + bash ./check_access/check.sh 4
18:13:32  check: val:

desired result

bash ./check_access/check.sh 1
check: val: 1
.
.
.
bash ./check_access/check.sh 4
check: val: 4

Could you please help

Upvotes: 1

Views: 277

Answers (1)

Yuriy
Yuriy

Reputation: 31

Solution: I pass val as the first parameter to my script. So I should use $1 inside the script, not $val

Upvotes: 2

Related Questions