You are on page 1of 2

#!

/bin/bash

# Define two arrays


array1=(
'04:37:10 -47dBm 5c:e0:c5:bb:eb:da "erbsjr 5.0"'
'04:37:10 -46dBm 5c:e0:c5:bb:eb:da "erbsjr 2.4"'
'04:37:10 -47dBm 6c:e0:c5:bb:eb:da "erbsjr 3.2"'
'04:37:10 -43dBm 6c:e0:c5:bb:eb:da "erbsjr 2.4"'
)

array2=(
'04:37:10 -49dBm 5c:e0:c5:bb:eb:da "erbsjr 4.0"'
'04:37:10 -40dBm 6c:e0:c5:bb:eb:da "erbsjr 2.4"'
)

# Function to parse and convert the second element to a floating-point number


function parse_and_convert_to_float() {
local line="$1"
local parts=($line)
local second_element="${parts[1]}"
local float_value=$(echo "$second_element" | sed 's/\([0-9]\+\)dBm/\1/')
echo "$float_value"
}

# Initialize variables for sum and count


sum_array1=0
sum_array2=0
count_array1=0
count_array2=0

# Loop through each element in array1, parse the second element, and calculate sum
and count
for element in "${array1[@]}"; do
parsed_value=$(parse_and_convert_to_float "$element")
sum_array1=$(echo "$sum_array1 + $parsed_value" | bc)
count_array1=$((count_array1 + 1))
done

# Loop through each element in array2, parse the second element, and calculate sum
and count
for element in "${array2[@]}"; do
parsed_value=$(parse_and_convert_to_float "$element")
sum_array2=$(echo "$sum_array2 + $parsed_value" | bc)
count_array2=$((count_array2 + 1))
done

# Calculate the average values


if [ "$count_array1" -gt 0 ]; then
average_array1=$(echo "scale=2; $sum_array1 / $count_array1" | bc)
echo "Average value of array1: $average_array1"
else
echo "Array1 is empty."
fi

if [ "$count_array2" -gt 0 ]; then


average_array2=$(echo "scale=2; $sum_array2 / $count_array2" | bc)
echo "Average value of array2: $average_array2"
else
echo "Array2 is empty."
fi

You might also like