Looking for guidance on my while loop and how to get it to actually have a countdown and then checks the query status again etc... any guidance? Right now I'm looking to see if I can get it to count down from 59 to zero...
STATUS='DONE'
QUERY_STATUS=$(curl .....)
while [ "$STATUS" != "$QUERY_STATUS" ]; do
for (( i=60; i>0; i--)); do
printf "\rWaiting for Query to finish, will check back in $i seconds"
i=$((i + 1))
done
QUERY_STATUS=$(curl .....)
done
#!/bin/bash
STATUS='DONE'
while true; do
QUERY_STATUS=$(curl …) # You can just do this once inside the loop
# and exit the loop with a guard
[[ $STATUS = $QUERY_STATUS ]] && break
for i in {60..1}; do # You had i-- here, but i + 1 elsewhere
# Might as well use `printf` the way it was meant to be used ;)
printf '\rWaiting for Query to finish, will check back in %d seconds' "$i"
sleep 1 # You weren't actually sleeping inside the loop.
done
done
I was able to get the following to work:
STATUS='DONE'
QUERY_STATUS=$(curl .....)
while [ "$STATUS" != "$QUERY_STATUS" ]; do
for (( i=60; i>0; )); do
printf "\rWaiting for Query to finish, will check back in $i seconds"
sleep 1;
i=$((i-1))
done
QUERY_STATUS=$(curl ....)
done
Related
I have below code
i=0
for s in / - \\ \|
do
printf "\rWaiting for application start to finish $i $s"
sleep 1
((i++))
if [[ $i -gt 30 ]]
then
break
fi
done
The loop always ends after 3 iterations. Any reason as why?
This might be what you are looking for:
#!/bin/bash
a=('/' '-' '\' '|')
for ((i = 0; i < 30; ++i)); do
printf '\rWaiting for application start to finish %d %s' \
"$i" "${a[i%4]}"
sleep 1
done
echo
My mistake. The for loop which has three arguments is exiting instead of the if condition failing.
Requirement: Based upon IF condition in the called function: myfunc, echo hello in the for loop should not get executed and control should go to the next iteration.
In the below script, when the value of k becomes 2 and 3, echo hello should not get executed.
This is the script that I am trying to develop but no success.
#!/usr/bin/env bash
myfunc() {
if [[ $k -gt 1 ]]; then
echo "in the loop"
return
else
echo continue
fi
}
for (( k=1; k<=3; k++ ))
do
myfunc
echo hello
done
Please help.
Your loop is all wrong and I don't know why you have an if / else if you're just interested in one output:
#!/usr/bin/env bash
myfunc() {
if [[ $k -lt 2 ]]; then
echo "hello my value is $k"
fi
}
for (( k=1; k<=3; k++ ))
do
myfunc
echo "$k just to prove it is looping" # this is always run regardless of what's in the function
done
output:
hello my value is 1
1 just to prove it is looping
2 just to prove it is looping
3 just to prove it is looping
#!/usr/bin/env bash
myfunc() {
if [[ $k -gt 1 ]]; then
echo "in the loop"
x=1
else
echo Welcome
fi
}
for (( k=1; k<=3; k++ ))
do
myfunc
if [[ $x -eq 1 ]];then
continue
fi
echo hello
done
What is the difference between using a colon, which means "do nothing" and continue, which means skip.
if [[ -s $file ]] ; then
:
fi
if [[ -s $file ]] ; then
continue
fi
: is a synonym for true. It does not prevent later commands in the same block or loop from running.
Compare:
for (( i=0; i<3; i++ )); do
echo "Starting iteration $i"
(( i == 1 )) && { echo " About to run :"; :; echo " Just ran :"; }
(( i == 2 )) && { echo " About to run continue"; continue; echo " Just ran continue"; }
echo "Ending iteration $i"
done
Our output is:
Starting iteration 0
Ending iteration 0
Starting iteration 1
About to run :
Just ran :
Ending iteration 1
Starting iteration 2
About to run continue
Note that we made it to "ending" after running :, but not after running continue.
It depends on your program's logic.
Outside of a loop you get
$ continue
bash: continue: only meaningful in a `for', `while', or `until' loop
How can this while loop be limited to maximum 10 retries?
#!/bin/sh
while ! test -d /somemount/share/folder
do
echo "Waiting for mount /somemount/share/folder..."
sleep 1
done
Keep a counter:
#!/bin/sh
while ! test -d /somemount/share/folder
do
echo "Waiting for mount /somemount/share/folder..."
((c++)) && ((c==10)) && break
sleep 1
done
You can also use a for loop and exit it on success:
for try in {1..10} ; do
[[ -d /somemount/share/folder ]] && break
done
The problem (which exists in the other solutions, too) is that once the loop ends, you don't know how it ended - was the directory found, or was the counter exhausted?
I would comment but I do not have enough points for that. I want to contribute anyway.
So this makes it work even if the while loop is nested in another loop. before the break the c variable is being reset to zero.
credits to #anubhava who came up with the original solution.
#!/bin/sh
while ! test -d /somemount/share/folder
do
echo "Waiting for mount /somemount/share/folder..."
((c++)) && ((c==10)) && c=0 && break
sleep 1
done
You can use until (instead of "while ! ... break), with a counter limit:
COUNT=0
ATTEMPTS=10
until [[ -d /somemount/share/folder ]] || [[ $COUNT -eq $ATTEMPTS ]]; do
echo -e "$(( COUNT++ ))... \c"
sleep 1
done
[[ $COUNT -eq $ATTEMPTS ]] && echo "Could not access mount" && (exit 1)
Notes:
Just like setting counter as variable, you can set var condition="[[ .. ]]", and use until eval $condition to make it more generic.
echo $(( COUNT++ )) increases the counter while printing.
If running inside a function, use "return 1" instead of "exit 1".
How can this while loop be limited to maximum 10 retries?
#!/bin/sh
while ! test -d /somemount/share/folder
do
echo "Waiting for mount /somemount/share/folder..."
sleep 1
done
Keep a counter:
#!/bin/sh
while ! test -d /somemount/share/folder
do
echo "Waiting for mount /somemount/share/folder..."
((c++)) && ((c==10)) && break
sleep 1
done
You can also use a for loop and exit it on success:
for try in {1..10} ; do
[[ -d /somemount/share/folder ]] && break
done
The problem (which exists in the other solutions, too) is that once the loop ends, you don't know how it ended - was the directory found, or was the counter exhausted?
I would comment but I do not have enough points for that. I want to contribute anyway.
So this makes it work even if the while loop is nested in another loop. before the break the c variable is being reset to zero.
credits to #anubhava who came up with the original solution.
#!/bin/sh
while ! test -d /somemount/share/folder
do
echo "Waiting for mount /somemount/share/folder..."
((c++)) && ((c==10)) && c=0 && break
sleep 1
done
You can use until (instead of "while ! ... break), with a counter limit:
COUNT=0
ATTEMPTS=10
until [[ -d /somemount/share/folder ]] || [[ $COUNT -eq $ATTEMPTS ]]; do
echo -e "$(( COUNT++ ))... \c"
sleep 1
done
[[ $COUNT -eq $ATTEMPTS ]] && echo "Could not access mount" && (exit 1)
Notes:
Just like setting counter as variable, you can set var condition="[[ .. ]]", and use until eval $condition to make it more generic.
echo $(( COUNT++ )) increases the counter while printing.
If running inside a function, use "return 1" instead of "exit 1".