blox blox - 1 month ago 6
Linux Question

Multiple variables into one variable with wildcard

I have this script:

#!/bin/bash
ping_1=$(ping -c 1 www.test.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//')
ping_2=$(ping -c 1 www.test1.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//')
ping_3=$(ping -c 1 www.test2.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//')
ping_4=$(ping -c 1 www.test3.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//' )


Then I would like to treat the outputs of ping_1-4 in one variable. Something like this:

#!/bin/bash

if [ "$ping_*" -gt 50 ]; then
echo "One ping is to high"
else
echo "The pings are fine"
fi


Is there a possibilty in bash to read these variable with some sort of wildcard?

$ping_*


did nothing for me. Glad for any point in the right direction, thanks!

Answer

I don't think there's such wildcard. But you could use a loop to iterate over values, for example:

exists_too_high() {
    for value; do
        if [ "$value" -gt 50 ]; then
            return 0
        fi
    done
    return 1
}

if exists_too_high "$ping_1" "$ping_2" "$ping_3" "$ping_4"; then
    echo "One ping is to high"
else
    echo "The pings are fine"
fi
Comments