Make whiptail work in a for loop
This is my first question ever here... So please be gentle :)
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 <= $(( $total -1)); g++)); do
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 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.
I can't get for the life of me where I am wrong. In general I am stuck here and can't get any further.
Thank you very much in advance for any answer. I hope I will be able also to contribute here in the future.
P.S I know some of my coding is obsolete and old fashion way of bash scripting.
bash scripting for whiptail
add a comment |
This is my first question ever here... So please be gentle :)
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 <= $(( $total -1)); g++)); do
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 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.
I can't get for the life of me where I am wrong. In general I am stuck here and can't get any further.
Thank you very much in advance for any answer. I hope I will be able also to contribute here in the future.
P.S I know some of my coding is obsolete and old fashion way of bash scripting.
bash scripting for whiptail
This line has a typo: "for i in "${WEBISTE[@]}"; do"
– Thomas Dickey
Nov 25 '15 at 1:55
Yep just found it and correct it. Before I jump to check the stores I did some testings with websites, so that's why that variable has that name. Change and correct it but the problem still exists. Thanks fro the comment Thomas
– ChrisH
Nov 25 '15 at 1:58
add a comment |
This is my first question ever here... So please be gentle :)
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 <= $(( $total -1)); g++)); do
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 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.
I can't get for the life of me where I am wrong. In general I am stuck here and can't get any further.
Thank you very much in advance for any answer. I hope I will be able also to contribute here in the future.
P.S I know some of my coding is obsolete and old fashion way of bash scripting.
bash scripting for whiptail
This is my first question ever here... So please be gentle :)
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 <= $(( $total -1)); g++)); do
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 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.
I can't get for the life of me where I am wrong. In general I am stuck here and can't get any further.
Thank you very much in advance for any answer. I hope I will be able also to contribute here in the future.
P.S I know some of my coding is obsolete and old fashion way of bash scripting.
bash scripting for whiptail
bash scripting for whiptail
edited Jan 11 at 11:30
Kusalananda
124k16234385
124k16234385
asked Nov 25 '15 at 1:32
ChrisHChrisH
12
12
This line has a typo: "for i in "${WEBISTE[@]}"; do"
– Thomas Dickey
Nov 25 '15 at 1:55
Yep just found it and correct it. Before I jump to check the stores I did some testings with websites, so that's why that variable has that name. Change and correct it but the problem still exists. Thanks fro the comment Thomas
– ChrisH
Nov 25 '15 at 1:58
add a comment |
This line has a typo: "for i in "${WEBISTE[@]}"; do"
– Thomas Dickey
Nov 25 '15 at 1:55
Yep just found it and correct it. Before I jump to check the stores I did some testings with websites, so that's why that variable has that name. Change and correct it but the problem still exists. Thanks fro the comment Thomas
– ChrisH
Nov 25 '15 at 1:58
This line has a typo: "for i in "${WEBISTE[@]}"; do"
– Thomas Dickey
Nov 25 '15 at 1:55
This line has a typo: "for i in "${WEBISTE[@]}"; do"
– Thomas Dickey
Nov 25 '15 at 1:55
Yep just found it and correct it. Before I jump to check the stores I did some testings with websites, so that's why that variable has that name. Change and correct it but the problem still exists. Thanks fro the comment Thomas
– ChrisH
Nov 25 '15 at 1:58
Yep just found it and correct it. Before I jump to check the stores I did some testings with websites, so that's why that variable has that name. Change and correct it but the problem still exists. Thanks fro the comment Thomas
– ChrisH
Nov 25 '15 at 1:58
add a comment |
1 Answer
1
active
oldest
votes
Your script has an extra loop (which is executed anyway). It is pinging the array subscript rather than the subscripted member of the array.
Here is a version without the extra loop (and pinging the expected $site
):
#!/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
total="${#STORESITE[*]}";
whiptail --title "Testing stores connectivity" --backtitle "Store Test" --yes-button "OK" --no-button "Cancel" --yesno "Check stores procedure has started, with $total stores" 10 50
if [ $? -ne 0 ]; then
exit;
fi
{
UP=0
DOWN=0
for ((g = 0; g <= $(( $total -1)); g++)); do
site="${STORESITE[$g]}"
ping -c 3 $site > /dev/null 2> /dev/null;
if [ $? -eq 0 ]; then
echo "$site is up" >> storesup.txt;
let UP++
else
echo "$site is down" >> storesdown.txt;
let DOWN++
fi
# sleep 2
# stdbuf --output=L printf "Total: $UP are online and $DOWN are off line.r"
stdbuf --output=L echo $(( ( g * 100 ) / total ))
sleep 1
done
} | whiptail --gauge "Please wait" 6 60 0
online=$(wc -l storesup.txt | awk '{print $1;}')
offline=$(wc -l storesdown.txt | awk '{print $1;}')
echo "Total: $online are online and $offline are off line.";
exit;
Other fixes:
- I used
stdbuf
to make the output line-buffered (which helps make the gauge work as expected). - The redirection makes
UP
andDOWN
in a subprocess, so the totals did not get to the last line. I worked around that by counting the number of lines in the output files. - Finally, it shows a percentage in the progress bar.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f245304%2fmake-whiptail-work-in-a-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your script has an extra loop (which is executed anyway). It is pinging the array subscript rather than the subscripted member of the array.
Here is a version without the extra loop (and pinging the expected $site
):
#!/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
total="${#STORESITE[*]}";
whiptail --title "Testing stores connectivity" --backtitle "Store Test" --yes-button "OK" --no-button "Cancel" --yesno "Check stores procedure has started, with $total stores" 10 50
if [ $? -ne 0 ]; then
exit;
fi
{
UP=0
DOWN=0
for ((g = 0; g <= $(( $total -1)); g++)); do
site="${STORESITE[$g]}"
ping -c 3 $site > /dev/null 2> /dev/null;
if [ $? -eq 0 ]; then
echo "$site is up" >> storesup.txt;
let UP++
else
echo "$site is down" >> storesdown.txt;
let DOWN++
fi
# sleep 2
# stdbuf --output=L printf "Total: $UP are online and $DOWN are off line.r"
stdbuf --output=L echo $(( ( g * 100 ) / total ))
sleep 1
done
} | whiptail --gauge "Please wait" 6 60 0
online=$(wc -l storesup.txt | awk '{print $1;}')
offline=$(wc -l storesdown.txt | awk '{print $1;}')
echo "Total: $online are online and $offline are off line.";
exit;
Other fixes:
- I used
stdbuf
to make the output line-buffered (which helps make the gauge work as expected). - The redirection makes
UP
andDOWN
in a subprocess, so the totals did not get to the last line. I worked around that by counting the number of lines in the output files. - Finally, it shows a percentage in the progress bar.
add a comment |
Your script has an extra loop (which is executed anyway). It is pinging the array subscript rather than the subscripted member of the array.
Here is a version without the extra loop (and pinging the expected $site
):
#!/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
total="${#STORESITE[*]}";
whiptail --title "Testing stores connectivity" --backtitle "Store Test" --yes-button "OK" --no-button "Cancel" --yesno "Check stores procedure has started, with $total stores" 10 50
if [ $? -ne 0 ]; then
exit;
fi
{
UP=0
DOWN=0
for ((g = 0; g <= $(( $total -1)); g++)); do
site="${STORESITE[$g]}"
ping -c 3 $site > /dev/null 2> /dev/null;
if [ $? -eq 0 ]; then
echo "$site is up" >> storesup.txt;
let UP++
else
echo "$site is down" >> storesdown.txt;
let DOWN++
fi
# sleep 2
# stdbuf --output=L printf "Total: $UP are online and $DOWN are off line.r"
stdbuf --output=L echo $(( ( g * 100 ) / total ))
sleep 1
done
} | whiptail --gauge "Please wait" 6 60 0
online=$(wc -l storesup.txt | awk '{print $1;}')
offline=$(wc -l storesdown.txt | awk '{print $1;}')
echo "Total: $online are online and $offline are off line.";
exit;
Other fixes:
- I used
stdbuf
to make the output line-buffered (which helps make the gauge work as expected). - The redirection makes
UP
andDOWN
in a subprocess, so the totals did not get to the last line. I worked around that by counting the number of lines in the output files. - Finally, it shows a percentage in the progress bar.
add a comment |
Your script has an extra loop (which is executed anyway). It is pinging the array subscript rather than the subscripted member of the array.
Here is a version without the extra loop (and pinging the expected $site
):
#!/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
total="${#STORESITE[*]}";
whiptail --title "Testing stores connectivity" --backtitle "Store Test" --yes-button "OK" --no-button "Cancel" --yesno "Check stores procedure has started, with $total stores" 10 50
if [ $? -ne 0 ]; then
exit;
fi
{
UP=0
DOWN=0
for ((g = 0; g <= $(( $total -1)); g++)); do
site="${STORESITE[$g]}"
ping -c 3 $site > /dev/null 2> /dev/null;
if [ $? -eq 0 ]; then
echo "$site is up" >> storesup.txt;
let UP++
else
echo "$site is down" >> storesdown.txt;
let DOWN++
fi
# sleep 2
# stdbuf --output=L printf "Total: $UP are online and $DOWN are off line.r"
stdbuf --output=L echo $(( ( g * 100 ) / total ))
sleep 1
done
} | whiptail --gauge "Please wait" 6 60 0
online=$(wc -l storesup.txt | awk '{print $1;}')
offline=$(wc -l storesdown.txt | awk '{print $1;}')
echo "Total: $online are online and $offline are off line.";
exit;
Other fixes:
- I used
stdbuf
to make the output line-buffered (which helps make the gauge work as expected). - The redirection makes
UP
andDOWN
in a subprocess, so the totals did not get to the last line. I worked around that by counting the number of lines in the output files. - Finally, it shows a percentage in the progress bar.
Your script has an extra loop (which is executed anyway). It is pinging the array subscript rather than the subscripted member of the array.
Here is a version without the extra loop (and pinging the expected $site
):
#!/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
total="${#STORESITE[*]}";
whiptail --title "Testing stores connectivity" --backtitle "Store Test" --yes-button "OK" --no-button "Cancel" --yesno "Check stores procedure has started, with $total stores" 10 50
if [ $? -ne 0 ]; then
exit;
fi
{
UP=0
DOWN=0
for ((g = 0; g <= $(( $total -1)); g++)); do
site="${STORESITE[$g]}"
ping -c 3 $site > /dev/null 2> /dev/null;
if [ $? -eq 0 ]; then
echo "$site is up" >> storesup.txt;
let UP++
else
echo "$site is down" >> storesdown.txt;
let DOWN++
fi
# sleep 2
# stdbuf --output=L printf "Total: $UP are online and $DOWN are off line.r"
stdbuf --output=L echo $(( ( g * 100 ) / total ))
sleep 1
done
} | whiptail --gauge "Please wait" 6 60 0
online=$(wc -l storesup.txt | awk '{print $1;}')
offline=$(wc -l storesdown.txt | awk '{print $1;}')
echo "Total: $online are online and $offline are off line.";
exit;
Other fixes:
- I used
stdbuf
to make the output line-buffered (which helps make the gauge work as expected). - The redirection makes
UP
andDOWN
in a subprocess, so the totals did not get to the last line. I worked around that by counting the number of lines in the output files. - Finally, it shows a percentage in the progress bar.
answered Oct 13 '16 at 9:21
Thomas DickeyThomas Dickey
52.3k594165
52.3k594165
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f245304%2fmake-whiptail-work-in-a-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This line has a typo: "for i in "${WEBISTE[@]}"; do"
– Thomas Dickey
Nov 25 '15 at 1:55
Yep just found it and correct it. Before I jump to check the stores I did some testings with websites, so that's why that variable has that name. Change and correct it but the problem still exists. Thanks fro the comment Thomas
– ChrisH
Nov 25 '15 at 1:58