Advertisement
metalx1000

Basic BASH Timer #2

Sep 7th, 2018
1,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.61 KB | None | 0 0
  1. #!/bin/bash
  2. ######################################################################
  3. #Copyright (C) 2018  Kris Occhipinti
  4. #https://filmsbykris.com
  5.  
  6. #This program is free software: you can redistribute it and/or modify
  7. #it under the terms of the GNU General Public License as published by
  8. #the Free Software Foundation, either version 3 of the License, or
  9. #(at your option) any later version.
  10.  
  11. #This program is distributed in the hope that it will be useful,
  12. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #GNU General Public License for more details.
  15.  
  16. #You should have received a copy of the GNU General Public License
  17. #along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. ######################################################################
  19.  
  20. tmp="$HOME/.mytimer.tmp"
  21.  
  22. if [ "$1" = "new" ] && [ -f "$tmp" ]
  23. then
  24.   rm "$tmp"
  25. fi
  26.  
  27. function main(){
  28.   echo "Welcome to my timer"
  29.   check_timer;
  30.   start_timer
  31.   exit 0
  32. }
  33.  
  34. function check_timer(){
  35.   if [ -f "$tmp" ]
  36.   then
  37.     echo "Timer Reloaded - To restart run '$0 new'"
  38.     let start_time="$(cat $tmp)";
  39.   else
  40.     echo "New Timer Started"
  41.     let start_time="$(date +%s)";
  42.     echo "$start_time" > "$tmp"
  43.   fi
  44. }
  45.  
  46. function start_timer(){
  47.   while [ 1 ];
  48.   do
  49.     let current_time="$(date +%s)"
  50.     let seconds=$current_time-$start_time;
  51.  
  52.     echo -en "\r                                        \r"
  53.     printf "Timer: %02d:%02d:%02d:%02d" "$((seconds/86400))" "$((seconds/3600%24))" "$((seconds/60%60))" "$((seconds%60))"
  54.     sleep 1;
  55.   done
  56. }
  57.  
  58. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement