#!/bin/bash # Using two digits the represent the value being counted down # Two-digits set in decimal representation used by printf # Usage: 'countdown #delay #style' # Delay is an integer from 1 to 99 (what happens if zero?) # Style is a string, either "numbers" or "style" # Arguments should always be given in that order # Both arguments may be omitted, in that case delay defaults to ten seconds and style to "numbers" defaultdelay=10 numberstyle="numbers" symbolstyle="symbols" defaultstyle=$numberstyle if [ $# -gt 2 ]; then # More than two arguments echo Too many arguments exit else # Two or less arguments if [ $# -eq 2 ]; then # Exactly two arguments # First argument should specify the delay delaysecs=$1 # Second argument should specify the display style style=$2 else if [ $# -eq 1 ]; then # Only one argument # Argument specifies the delay delaysecs=$1 # Style defaults to "numbers" style="numbers" else # No arguments submitted # Delay defaults delaysecs=$defaultdelay # Style defaults style=$defaultstyle fi fi fi # Adjustment by one second #delaysecs=$[$delaysecs-1] delaystatic=$delaysecs if [ $style == $numberstyle ]; then while [ $delaysecs -gt 0 ] do sleep 1 & printf "\r" # Text before counter printf "Stand by... " printf "%02d" $[delaysecs] delaysecs=$[$delaysecs-1] wait done # Printing the zero (end of count-down) printf "\b\b" printf "%02d" $[delaysecs] printf " - Done!" echo else # style is symbolstyle printf -v line '%*s' "$delaysecs" printf "%s\r" "${line// /#}" while [ $delaysecs -gt 0 ] do sleep 1 & printf "+" delaysecs=$[$delaysecs-1] wait done #printf "\b " echo fi # Bash quoting with ANSI-C style # \b - backspace # \r - carriage return # \n - newline # \f - form-feed # \t - horizontal tabulator # \v - vertical tabulator # http://bash-hackers.org/wiki/doku.php/commands/builtin/printf