There are various ways to show a countdown in your shell scripts.
First define your message:
msg="Purging cache please wait..."
Now clear the screen and display the message at row 10 and column 5 using tput:
clear
tput cup 10 5
Next you need to display the message:
echo -n "$msg"
Find out the length of string:
l=${#msg}
Calculate the next column:
l=$(( l+5 ))
Finally use a bash for loop to show countdown:
for i in {30..01}
do
tput cup 10 $l
echo -n "$i"
sleep 1
done
echo
Here is a complete shell script:
#!/bin/bash # Purpose: Purge urls from Cloudflare Cache # Author: Vivek Gite {www.cyberciti.biz} under GPL v2.x+ # -------------------------------------------------------- # Set me first # zone_id="My-ID" api_key="My_API_KEY" email_id="My_EMAIL_ID" row=2 col=2 urls="$@" countdown() { msg="Purging ${1}..." clear tput cup $row $col echo -n "$msg" l=${#msg} l=$(( l+$col )) for i in {30..1} do tput cup $row $l echo -n "$i" sleep 1 done } # Do it for u in $urls do amp_url="${u}amp/" curl -X DELETE "https://api.cloudflare.com/client/v4/zones/${zone_id}/purge_cache" \ -H "X-Auth-Email: ${email_id}" \ -H "X-Auth-Key: ${api_key}" \ -H "Content-Type: application/json" \ --data "{\"files\":[\"${u}\",\"${amp_url}\"]}" &>/dev/null && countdown "$u" done echo
You can run it as follows:
./script.sh url1 url2
POSIX shell version
From this post:
countdown() ( IFS=: set -- $* secs=$(( ${1#0} * 3600 + ${2#0} * 60 + ${3#0} )) while [ $secs -gt 0 ] do sleep 1 & printf "\r%02d:%02d:%02d" $((secs/3600)) $(( (secs/60)%60)) $((secs%60)) secs=$(( $secs - 1 )) wait done echo )
It can be run as follows:
countdown "00:00:10" # 10 sec
countdown "00:00:30" # 30 sec
countdown "00:01:42" # 1 min 42 sec
Sample session:
🐧 4 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
How come you could escape double quotes inside double quote strings like that when it wasn’t possible before ?
You can always do that. There was no such problem:
echo "This is a \"test\"."
That never worked before (syntax error), had to use glueing like someone said to a question of escaping double quotes in a double quote string:
echo "This is a "'"test"'"."
Now it works. That was one of the stupidities of Bash, and only now they fixed it.
Thanks, this post helped me out! I added some functionality so the screen does not clear… This let me see the command output from the previous loops. And I made it into an infinite looping tool which was my use case.
https://gist.github.com/chawkins88/289d2093a6eefab8a2ffe19c8bd4d52c