Want more features on Pastebin? Sign Up, it's FREE!
Guest

bash - clear vs tput clear and cursor up tester

By: a guest on Jul 6th, 2012  |  syntax: Bash  |  size: 0.96 KB  |  views: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2.  
  3. # helper
  4. if [ -z ${1} ] || [ -z ${2} ] || [ "--help" == "${1}" ]; then
  5.     echo
  6.     echo "Usage: $0 [speed in second] [how many cycles] [cursorup] [bash clear]"
  7.     echo
  8.     echo "[cursorup]: 0, no tput cuu1; 1, yes tput cuu1"
  9.     echo "[bash clear]: 0, tput clear; 1, clear; command for each cycle"
  10.     echo "[bash clear]: 2, tput clear; 3, clear; command for each echo"
  11.     echo
  12.     exit 1
  13. fi
  14.  
  15. # parameters
  16. pos=0
  17. sec=$1
  18. how=$2
  19. cur=$3
  20. bas=$4
  21.  
  22. # test
  23. clr='tput clear'
  24. cuu='tput cuu1'
  25.  
  26. # function
  27. clearEcho ()
  28. {
  29.     ori=${1}
  30.     num=$(( ori + 1 ))
  31.     if [ $bas -eq $ori ]; then
  32.         $clr
  33.     else
  34.         if [ $bas -eq $num ]; then
  35.             clear
  36.         fi
  37.     fi
  38.     if [ $bas -eq $ori ] || [ $bas -eq $num ]; then
  39.         echo cycle: $pos
  40.     fi
  41. }
  42.  
  43. # main
  44. while [ $pos -lt $how ]
  45. do
  46.     clearEcho 0
  47.     for i in $(echo "| / - \\")
  48.    do
  49.         clearEcho 2
  50.         echo $i
  51.         if [ $cur -gt 0 ]; then
  52.             $cuu
  53.         fi
  54.         sleep $sec
  55.    done
  56.    let pos++
  57. done
clone this paste RAW Paste Data