track (1813B)
1 #!/usr/bin/env bash 2 PARAMS="" 3 4 calc_values() { 5 [ -f /tmp/timetrackfile ] || { echo "No trackfile." && exit 1; } 6 start_time_s=$(cat /tmp/timetrackfile && rm /tmp/timetrackfile) 7 end_time_s=$(date +%s) 8 duration_s=$((end_time_s - start_time_s)) 9 start_fmt=$(date -r "$start_time_s" +%H:%M) 10 end_fmt=$(date -r "$end_time_s" +%H:%M) 11 hours=$((duration_s / 3600)) 12 minutes=$(((duration_s % 3600)/60)) 13 duration_fmt="$(printf "%02d" $hours):$(printf "%02d" $minutes)" 14 } 15 print_duration() { 16 echo "Complete." 17 echo "Start: $start_fmt" 18 echo "End: $end_fmt" 19 echo "Duration: $duration_s sec, $duration_fmt" 20 echo "$(date +%d-%m-%Y),$start_fmt,$end_fmt,$duration_fmt" 21 } 22 23 while (( "$#" )); do 24 case "$1" in 25 -a|--append) 26 append=1 27 shift 28 ;; 29 -o|--output) 30 outfile="$2" 31 shift 2 32 ;; 33 -h|--help) 34 echo "Usage:" 35 echo "track start tracking" 36 echo "track stop [-a] [-o outfile] stop tracking, -o to specify an output file, -a to append" 37 exit 0 38 ;; 39 --) # end arg parsing 40 shift 41 break 42 ;; 43 -*) # unsupported flags 44 echo "Unsupported flag $1" >&2 45 exit 1 46 ;; 47 *) # preserve positional arguments 48 PARAMS="$PARAMS $1" 49 shift 50 ;; 51 esac 52 done 53 eval set -- "$PARAMS" 54 55 if [ $# -eq 0 ]; then 56 date +%s > /tmp/timetrackfile 57 echo "Tracking time." 58 elif [ "$1" = "status" ] || [ "$1" = "s" ]; then 59 if [ -f /tmp/timetrackfile ]; then 60 echo "Tracking since $(date -r <(cat /tmp/timetrackfile))." 61 else 62 echo "Not tracking" 63 fi 64 elif [ "$1" = "stop" ]; then 65 calc_values 66 67 if [ -z "$outfile" ]; then 68 print_duration 69 else 70 if [ "$append" = 1 ]; then 71 print_duration | tee -a "$outfile" 72 else 73 print_duration | tee "$outfile" 74 fi 75 fi 76 fi