Advertisement
tolikpunkoff

waiter

Sep 2nd, 2016
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.28 KB | None | 0 0
  1. #!/bin/bash
  2. #waiter script (L) 8^12
  3.  
  4. MESSAGE="Wait"
  5. WTIMEOUT=0
  6. ITEM_ARR=0 #current item counter
  7.  
  8. CH_S[0]='-' #pseudographic items
  9. CH_S[1]='/'
  10. CH_S[2]='|'
  11. CH_S[3]='\'
  12.  
  13. print_help()
  14. {
  15.     echo "use waiter <time> [message]"
  16.     echo "<time> - wait time"
  17.     echo "[message] - optional text message"
  18. }
  19.  
  20. #parameters check
  21.  
  22. if [ -z $1 ];then #if no parameters
  23.     print_help
  24.     exit 2
  25. fi
  26.  
  27. #if help request
  28. if [ $1 = "-h" ];then
  29.     print_help
  30.     exit 2
  31. fi
  32.  
  33. if [ $1 = "--help" ];then
  34.     print_help
  35.     exit 2
  36. fi
  37.  
  38. if [ -n "$2" ];then
  39.     MESSAGE=$2
  40. fi
  41.  
  42. if (echo $1 | grep -E -q "^?[0-9]+$");then
  43.     WTIMEOUT=$1
  44. else
  45.     echo "Not a number in first parameter <time>"
  46.     exit 1
  47. fi
  48.  
  49. echo -n $MESSAGE" ("$WTIMEOUT" secounds): "
  50. tput sc #save cursor position
  51.  
  52. while [ $WTIMEOUT -ge 0 ]; do
  53.    
  54.     #print timeout and current pseudographic char
  55.     printf '%3s %s' $WTIMEOUT ${CH_S[ITEM_ARR]}
  56.     tput rc #restore cursor position
  57.     sleep 1
  58.    
  59.     #decrease timeout and increase current item ctr.
  60.     let "WTIMEOUT=WTIMEOUT-1"
  61.     let "ITEM_ARR=ITEM_ARR+1"
  62.    
  63.     if [ $ITEM_ARR -eq 4 ];then
  64.     #if items ctr > number of array items
  65.     #starting with 0 item
  66.     let "ITEM_ARR=0"
  67.     fi
  68.        
  69. done
  70.  
  71. #next message starting with new string
  72. printf '\n'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement