Collection of random code snippets and examples.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

60 lines
1.5 KiB

  1. #!/bin/bash
  2. VERBOSE=0
  3. CONFIG_DIR=$HOME/.config/crolim
  4. MEMORY_FILE=${CONFIG_DIR}/memory
  5. STATUS_COMMAND="date +%D"
  6. function usage() {
  7. echo "Usage: $0 [OPTIONS] COMMAND"
  8. echo " $0 -h"
  9. echo ""
  10. echo " -h ... show help"
  11. echo ""
  12. echo " OPTIONS"
  13. echo " -m --memory-file ... location to store previous status value"
  14. echo " -s --status-command ... command to execute to get the current status"
  15. echo " -v --verbose ... verbose mode"
  16. echo " COMMAND ... command to limit until memory and status are the same"
  17. }
  18. # read the options
  19. TEMP=`getopt -o m:s:hv --long memory-file:,status-command:,help,verbose -- "$@"`
  20. eval set -- "$TEMP"
  21. while true ; do
  22. case "$1" in
  23. -h | --help) usage; exit 0
  24. ;;
  25. -v | --verbose) VERBOSE=1 ; shift
  26. ;;
  27. -m | --memory-file)
  28. MEMORY_FILE=$2 ; shift 2
  29. ;;
  30. -s | --status-command)
  31. STATUS_COMMAND=$2 ; shift 2
  32. ;;
  33. --) shift ; break ;;
  34. *) echo "Internal error!" ; exit 1 ;;
  35. esac
  36. done
  37. # check limiting condition
  38. #mkdir -p `dirname $MEMORY_FILE`
  39. STATUS_NOW=`eval $STATUS_COMMAND`
  40. STATUS_MEMORY=""
  41. if [[ -f $MEMORY_FILE ]]; then
  42. STATUS_MEMORY=`cat $MEMORY_FILE`
  43. fi
  44. if [[ "$STATUS_NOW" == "$STATUS_MEMORY" ]]; then
  45. [[ $VERBOSE == 1 ]] && echo "Limiting, status is the same: ${STATUS_NOW}"
  46. exit 0
  47. else
  48. [[ $VERBOSE == 1 ]] && echo "Status changed from '${STATUS_MEMORY}' to '${STATUS_NOW}'"
  49. echo ${STATUS_NOW} > $MEMORY_FILE || { echo "Cannot update memory file: $MEMORY_FILE" ; exit 1 ; }
  50. fi
  51. # run the command!
  52. eval $@