I have a bash script that checks ping for 163 sites(stores around the country), which is this:
#!/bin/bash
#storestest.sh
#version 0.9.2
clear;
printf "Check stores procedure started;
date +"%d/%m/%Y %H:%M:%S"
\n";
declare -a STORESITE=($(cat 'stores.txt' | awk -F, '{print $1}')); # Declaring an array and populated by the file stores.txt
declare -i UP=0; # Create a variable to serve as a counter for stores that are up
declare -i DOWN=0; # Create a variable to serve as a counter for stores that are down
touch storesdown.txt; # Create a file if does not exist to store the list of the store the stores that are down
printf "" > storesdown.txt; # Clear the contents of the file
touch storesup.txt; # Create a file if does not exist to store the list of the store the stores that are up
printf "" > storesup.txt; # Clear the contents of the file
whiptail --title "Testing stores connectivity" --backtitle "Store Test" --yes-button "OK" --no-button "Cancel" --yesno "Check stores procidure has started" 10 50
if [ $? -ne 0 ]; then
exit;
fi
for i in "${STORESITE[@]}" ; do
ping -c 3 $i > /dev/null 2> /dev/null;
if [ $? -eq 0 ]; then
echo "$i is up" >> storesup.txt;
let UP++
sleep 1
else
echo "$i is down" >> storesdown.txt;
let DOWN++
sleep 1
fi
printf "Total: $UP are online and $DOWN are off line.\r";
done
echo "Total: $UP are online and $DOWN are off line.";
exit;
The above script is working fine but I decided to make it a bit fancier by adding a gauge to show the total progress of it.
So here is what I did next:
#!/bin/bash
#storestest.sh
#version 0.9.3
clear;
printf "Check stores procedure started; date +"%d/%m/%Y %H:%M:%S"
\n";
declare -a STORESITE=($(cat 'stores.txt' | awk -F, '{print $1}')); # Declaring an array and populated by the file stores.txt
declare -i UP=0; # Create a variable to serve as a counter for stores that are up
declare -i DOWN=0; # Create a variable to serve as a counter for stores that are down
touch storesdown.txt; # Create a file if does not exist to store the list of the store the stores that are down
printf "" > storesdown.txt; # Clear the contents of the file
touch storesup.txt; # Create a file if does not exist to store the list of the store the stores that are up
printf "" > storesup.txt; # Clear the contents of the file
whiptail --title "Testing stores connectivity" --backtitle "Store Test" --yes-button "OK" --no-button "Cancel" --yesno "Check stores procidure has started" 10 50
if [ $? -ne 0 ]; then
exit;
fi
total="${#STORESITE[*]}";
{
for ((g = 0; g /dev/null 2> /dev/null;
if [ $? -eq 0 ]; then
echo "$i is up" >> storesup.txt;
let UP++
sleep 2
else
echo "$i is down" >> storesdown.txt;
let DOWN++
sleep 2
fi
printf "Total: $UP are online and $DOWN are off line.\r";
done
sleep 1
echo $g
done
} | whiptail --gauge "Please wait" 6 60 0
echo "Total: $UP are online and $DOWN are off line.";
exit;
Which does not work as I thought it should be.... That was the best way I could think and write to make at least the whiptail work, but apparently what it happens is to skip the nested loop.
P.S I know some of my coding is obsolete and old fashion way of bash scripting.
Asked by ChrisH
(21 rep)
Nov 25, 2015, 01:32 AM
Last activity: Oct 26, 2023, 05:08 PM
Last activity: Oct 26, 2023, 05:08 PM