Home > Back-end >  Bash shell: How to get the boolean vars names as string
Bash shell: How to get the boolean vars names as string

Time:09-28

for bool in $jobdummyjob1 $jobdummyjob2 $jobdummyjob3
do
echo "Boolean Value is $bool"
    if "$bool" ; then 
        echo "$alljobs"
        curl -X POST https://jenkins.delasport.com/job/$bool/build --user [email protected]:11880a2d5164f159e10c61e77cc416e311
    fi
done

All I need is to take somehow the names of jobdummyjob1, 2, 3 and put them in the URL as a string. Those vars are booleans so when I do this I get true or false in the URL. I do not need the variable value, but its name.

First I run the 'for' and I go through each object. Each object contains boolean value. Then, I do the true/false check and if true, I need to get the string name of the same variable and put it in the URL . This is a Jenkins job.

CodePudding user response:

You can use variable indirection:

for name in jobdummyjob1 jobdummyjob2 jobdummyjob3
do
bool=${!name}
echo "Boolean Value is $bool"
    if "$bool" ; then 
        echo "$alljobs"
        curl -X POST https://jenkins.delasport.com/job/"$name"/build --user [email protected]:11880a2d5164f159e10c61e77cc416e311
    fi
done

But it's cleaner to use an associative array:

declare -A bools
bools=([jobdummyjob1]=true [jobdummyjob2]=false [jobdummyjob3]=true)
for name in "${!bools[@]}" ; do
    bool=${bools[$name]}
    if ...
done

CodePudding user response:

I made it work with this script, but I face another problem, which should be the last one I hope:

countOuter=0

countInner=0

for bool in $jobdummyjob1 $jobdummyjob2 $jobdummyjob3 $18dummyjob do countOuter=$((countOuter 1)) for name in jobdummyjob1 jobdummyjob2 jobdummyjob3 18dummyjob do countInner=$((countInner 1))

        if [ "$countOuter" -eq "$countInner" ] && "$bool"; then
        curl -X POST https://jenkins.delasport.com/job/$name/build --user [email protected]:11880a2d5164f159e10c61e77cc416e311
        echo "They're equal $countOuter : $countInner"; 
        echo "SHOW BOOL ------- $bool"
         countInner=0
        fi
       
done
countInner=0

done echo "Inner: $countInner ||||| Outer: $countOuter"

The issue is that if the variable starts with number as it is the case with "18dummyjob", it is excluded somehow and jenkins says that: /tmp/jenkins18288022382660040755.sh: 13: /tmp/jenkins18288022382660040755.sh: dummyjob: not found The numbers are literally skipped which changes the variable value. Do you have any idea what kind of string operation could I use in this case?

  • Related