Command-line history logging utilities
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.

61 lines
1.1 KiB

  1. #!/usr/bin/env bash
  2. set -e
  3. usage() {
  4. cat<<HELP
  5. $(basename "$0") [OPTIONS]
  6. OPTIONS:
  7. -o <columns> Where <columns> is a comma-separated list of values to
  8. display in output.
  9. -[NUM] Where [NUM] is the number of commands to show.
  10. EXAMPLES:
  11. Show last 5 commands:
  12. $ $(basename "$0") -5
  13. Show date and command of last 3 commands:
  14. $ $(basename "$0") -o datetime,command
  15. HELP
  16. }
  17. show_log() {
  18. local log_count cmd
  19. log_count="$1"
  20. columns="$2"
  21. cmd=$(printf \
  22. 'SELECT %s FROM commands ORDER BY datetime DESC LIMIT %d;' \
  23. "$columns" \
  24. "$log_count")
  25. exec sqlite3 -line ~/cli.db "$cmd"
  26. }
  27. main() {
  28. local log_count columns
  29. log_count=3
  30. columns='*'
  31. while [ -n "$1" ]; do
  32. case "$1" in
  33. --help|-h)
  34. usage
  35. exit 0
  36. ;;
  37. -o)
  38. shift
  39. columns="$1"
  40. ;;
  41. -*)
  42. log_count="${1:1}"
  43. ;;
  44. esac
  45. shift
  46. done
  47. show_log "$log_count" "$columns"
  48. }
  49. main "$@"