1

I wanted to try this approach in bash and it seems there is no straightforward way to do so. Example of what I was messing with:

Var1=foo
Var2=bar
Var3=

Var3 is deliberately left undefined in my tests with this.

After those variables, I use something ideally like this:

declare -a array=(Var1 Var2 Var3)

for array_element in ${array[@]};
do 
    if [[ $array_element ]];
        then 
            echo "Element $array_element has content - true";
            echo "The value of $array_element is foo."
            echo
        else 
            echo "Element $array_element has no content - false";
            echo
    fi
done

The desired end result I'm after to see if I can do it would be that if you had 3 or 30 possible variables, it would tell you if an array element has content or not, and then output that specific variable content. In this case, "foo" and "bar".

  1. Define a variable
  2. Define an array that includes the name of the variable only
  3. Check to see if the defined array has content (not if it's set--just is it blank or not)
  4. Then output the actual originally defined value of the array element.

Dynamic arrays, sort of? A basic bunch of

if [[ $Var1 ]] 
if [[ $Var2 ]] 
if [[ $Var3 ]] 

works fine, but I want to wrap it up into something like this if I can to have less repetition of code. The long goal would be to have the variable be read, spit out whatever is defined as variables in a separate config file, and then fire off various functions defined elsewhere against that dynamic content. So, if this can be made to work, where I have up above:

echo "The value of $array_element is foo."

Would be replaced with a bunch of various functions() that would use foo as $1, essentially. But this part got me hitched up. My test above (unsurprisingly) fails as expected with all True hits, because it's just affirming $array_element is indeed set from the reading of the $array. I'm stuck on how to get the content that's defined up top under the $array_element. If you toss an "echo $Var1" anywhere in the file, it also outputs the expected "foo" regardless of where it is.

Can you do this in bash? I don't think I've seen variables used this way before and I'm not sure if I'm stuck on some mechanical problem/bash limitation or a logic problem in looking at it. I've been mucking around with it and a couple of approaches for a while with no luck, and Google and Stack Overflow searches have come up dry. Thanks for any help.

1

1 Answer 1

0

This is the correct way of looping through an array that may contain empty elements:

Var1=foo
Var2=bar
Var3=
declare -a array=("$Var1" "$Var2" "$Var3")

for ((i=0; i<${#array[@]}; i++))
do 
    array_element="${array[$i]}"

    echo "processing [$array_element]"

    if [[ $array_element ]];
        then 
            echo "Element $array_element has content - true";
            echo "The value of $array_element is foo."
            echo
        else 
            echo "Element $array_element has no content - false";
            echo
    fi
done

OUTPUT:

processing [foo]
Element foo has content - true
The value of foo is foo.

processing [bar]
Element bar has content - true
The value of bar is foo.

processing []
Element  has no content - false
Sign up to request clarification or add additional context in comments.

4 Comments

I was close, thank you -- but how about getting the output to show the specific initial variable? The value of Var1 is foo.
Note this line: echo "processing [$array_element]" which is printing all values of your array. Pls note that you can only get specific element in array using a numeric index like 0, 1, 2 etc.
Replace $array_element by Var$((i+1)) in the echo command.
@Lsrei: If this answer helped you solve your problem, please consider marking it as "accepted", so users facing a similar problem in the future will be able to see it easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.