I have working shell
(not bash
) code, that splits string to varialbes:
sh """
version='1.2.3.4'
echo \$version | { IFS=. read a b c d; echo \$a; }
echo \"a\" \$a
"""
Here is output:
+ version=1.2.3.4
+ IFS=. read a b c d
+ echo 1.2.3.4
+ echo 1
1
+ echo a
a
Problem:
I can't access values of a,b,c,d
outside braces
Question:
How can I access these variables?
UPD:
I run this code via jenkins
pipeline
, which let me use such approach. If I try to use another one, it just ignores attempt to parse string:
stage('test') {
steps {
sh """
version='1.2.3.4'
IFS=. read a b c d <<EOF
\$version
EOF
echo \"a\" \$a
"""
}
}
Output:
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] sh
[ScpPipeline] Running shell script
+ version=1.2.3.4
+ IFS=. read a b c d
[Pipeline] }
UPD_1:
Problem was in spaces and tabs... 0_o So this is working variant (but ugly):
stage('test') {
steps {
sh """
version='1.2.3.4'
IFS=. read a b c d <<EOF
\$version
EOF
echo \"a\" \$a \"b\" \$b \"c\" \$c \"d\" \$d
"""
}
}