'Bash shell: How to get the boolean vars names as string

for bool in $jobdummyjob1 $jobdummyjob2 $jobdummyjob3
do
echo "Boolean Value is $bool"
    if "$bool" ; then 
        echo "$alljobs"
        curl -X POST https://jenkins.xxxxxxxxx.com/job/$bool/build --user [email protected]:ewfwedf3f234523555235235235235235
    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.



Solution 1:[1]

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.xxxxxxxx.com/job/"$name"/build --user [email protected]:xxxxxxxx
    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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vesselin Tonchev