blox
blox

Reputation: 23

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 possibility in bash to read these variables with some sort of wildcard?

$ping_*

Did nothing for me.

Upvotes: 2

Views: 714

Answers (5)

agc
agc

Reputation: 8406

Yes bash can list variables that begin with $ping_, by using its internal compgen -v command, (see man bash under SHELL BUILTIN COMMANDS), i.e.:

for f in `compgen -v ping_` foo ; do 
    eval p=\$$f
    if [ "$p" -gt 50 ]; then
        echo "One ping is too high"
        break 1
    fi
    [ $f=foo ] && echo "The pings are fine"
done

Note the added loop item foo -- if the loop gets through all the variables, then print "the pings are fine".

Upvotes: 0

mklement0
mklement0

Reputation: 438153

To complement the existing, helpful answers with an array-based solution that demonstrates:

  • several advanced Bash techniques (robust array handling, compound conditionals, handling the case where pinging fails)
  • an optimized way to extract the average timing from ping's output by way of a single sed command (works with both GNU and BSD/macOS sed).
  • reporting the servers that either took too long or failed to respond by name.
#!/usr/bin/env bash

# Determine the servers to ping as an array.
servers=( 'www.test.com' 'www.test1.com' 'www.test2.com' 'www.test3.com' )

# Initialize the array in which timings will be stored, paralleling the
# "${servers[@]}" array.
avgPingTimes=()

# Initialize the array that stores the names of the servers that either took
# too long to respond (on average), or couldn't pe pinged at all.
failingServers=()

# Determine the threshold above which a timing is considered too high, in ms.
# Note that a shell variable should contain at least 1 lowercase character.
kMAX_TIME=50

# Determine how many pings to send per server to calculate the average timing
# from.
kPINGS_PER_SERVER=1

for server in "${servers[@]}"; do

  # Ping the server at hand, extracting the integer portion of the average
  # timing.
  # Note that if pinging fails, $avgPingTime will be empty. 
  avgPingTime="$(ping -c "$kPINGS_PER_SERVER" "$server" |
                   sed -En 's|^.* = [^/]+/([^.]+).+$|\1|p')"

  # Check if the most recent ping failed or took too long and add
  # the server to the failure array, if so.
  [[ -z $avgPingTime || $avgPingTime -gt $kMAX_TIME ]] && failingServers+=( "$server" )

  # Add the timing to the output array.
  avgPingTimes+=( "$avgPingTime" )

done

if [[ -n $failingServers ]]; then # pinging at least 1 server took too long or failed
  echo "${#failingServers[@]} of the ${#servers[@]} servers took too long or couldn't be pinged:"
  printf '%s\n' "${failingServers[@]}"
else
  echo "All ${#servers[@]} servers responded to pings in a timely fashion."
fi

Upvotes: 0

that other guy
that other guy

Reputation: 123490

The answer to your stated problem is that yes, you can do this with parameter expansion in bash (but not in sh):

#!/bin/bash
ping_1=foo
ping_2=bar
ping_etc=baz
for var in "${!ping_@}"
do
  echo "$var is set to ${!var}"
done

will print

ping_1 is set to foo
ping_2 is set to bar
ping_etc is set to baz

Here's man bash:

   ${!prefix*}
   ${!prefix@}
          Names matching prefix.  Expands to the names of variables  whose
          names begin with prefix, separated by the first character of the
          IFS special variable.  When @ is used and the expansion  appears
          within  double  quotes, each variable name expands to a separate
          word.

The answer to your actual problem is to use arrays instead.

Upvotes: 3

Lara Maia
Lara Maia

Reputation: 134

You can use "and" (-a) param:

if [ $ping_1 -gt 50 -a \
     $ping_2 -gt 50 -a \
     $ping_3 -gt 50 -a ]; then
   ...
   ...

Or instead of defining a lot of variables, you can make an array and check with a loop:

pings+=($(ping -c 1 www.test.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//'))
pings+=($(ping -c 1 www.test1.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//'))
pings+=($(ping -c 1 www.test2.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//'))
pings+=($(ping -c 1 www.test3.com | tail -1| awk '{print $4}' | cut -d '/' -f 2 | sed 's/\.[^.]*$//' ))

too_high=0
for ping in ${pings[@]}; do
    if [ $ping -gt 50 ]; then
        too_high=1
        break
    fi
done

if [ $too_high -eq 1 ]; then
    echo "One ping is to high"
else
    echo "The pings are fine"
fi

Upvotes: 1

janos
janos

Reputation: 124656

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

Upvotes: 1

Related Questions