Advertisement
spikeysnack

muc

Jun 10th, 2017
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.50 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # Most Used Commands (muc)
  4. # keep a list of the most used commands
  5.  
  6. # Author Chris Reid <spikeysnack@gmail.com>
  7. # DATE   4 Oct 2016
  8. # LICENSE  "free for non-commercial use.
  9. #           Modification allowed but original
  10. #           attribution must be published.
  11. #
  12.  
  13. OLDIFS=${IFS}
  14.  
  15. MUCDIR="$HOME/.muc"
  16.  
  17. LEN=10 # how many to show
  18.  
  19. REPLACE_TGZ=  # compress for space?
  20.  
  21. COMPRESSED="${MUCDIR}/mucfile.tgz"
  22. MUCFILE="${MUCDIR}/mucfile"
  23. TOPTEN="${MUCDIR}/topten.muc"
  24. CURRDIR="${PWD}"
  25.  
  26.  
  27. usage()
  28. {
  29.         cat <<EOF
  30.  
  31. usage: $0 [options]  [<N>]
  32.         N  number of results (default 10)
  33.  
  34. OPTIONS:
  35.    
  36.    -c      clean muc dir of all files
  37.    -h      show help
  38.    -u      update muc file
  39.    -v      verbose
  40.    -z      replace tarfile  
  41. EOF
  42. }
  43.  
  44. while getopts "chuvz" OPTION
  45. do
  46.     case $OPTION in
  47.  
  48.         c)
  49.             CLEAR=1
  50.             ;;
  51.  
  52.         h)
  53.             usage
  54.             exit 1
  55.             ;;
  56.         u)
  57.             UPDATE=1
  58.             ;;
  59.         v)
  60.             VERBOSE=1
  61.             ;;
  62.        
  63.         z)
  64.             REPLACE_TGZ=1
  65.             ;;
  66.        
  67.         ?)
  68.             usage
  69.             exit
  70.             ;;
  71.     esac
  72. done
  73.  
  74. #shift $(($OPTIND - 1))
  75. N=${!OPTIND}
  76.  
  77. if [[ $N ]] ; then
  78.  
  79.     if  (( $N == $N )) ; then  # need a number     
  80.         LEN=${N}
  81.     else
  82.         [[ $VERBOSE ]] && echo "need a number (10)"
  83.         usage
  84.         exit -1
  85.     fi
  86. fi
  87.  
  88.  
  89. [[ $CLEAR ]] && rm -f "${MUCFILE}" "${COMPRESSED}" "${TOPTEN}"
  90.  
  91.  
  92. [ -d "${MUCDIR}"     ] &&   mkdir -p "${MUCDIR}"    # create muc dir if needed
  93.  
  94.  
  95. [ -f "${COMPRESSED}" ] &&   tar --extract --gunzip --file "${COMPRESSED}" --directory "${MUCDIR}" --local  2>/dev/null  # decompress if found
  96.  
  97.    
  98. now=$(date +"%Y-%m-%d") # today's date
  99.  
  100. if ! [ -f "${MUCFILE}" ] ; then
  101.     touch "${MUCFILE}"
  102.     echo "${now}" > "${MUCFILE}"
  103.     cat $HOME/.bash_history >> "${MUCFILE}" # create file from history if needed
  104. fi
  105.  
  106. last=$(stat -c %y  ${MUCFILE} | cut -d ' ' -f1)   # check date of mucfile
  107.  
  108. # echo -e "[$last|$now]\n"
  109.  
  110. if [[ $UPDATE ]] || ! [ "X${last}" == "X${now}" ] ; then # if not current
  111.  
  112.     [[ $VERBOSE ]] && echo "Updating muc file"
  113.  
  114.     replace="1c\\${now}"                # (sed) replace first line of file
  115.  
  116.     sed -i ${replace}  "${MUCFILE}"     # replace 1st line
  117.  
  118.     cat ~/.bash_history >> "${MUCFILE}" # append bash history
  119.  
  120.     REPLACE_TGZ="1"                     # we will update archive
  121.  
  122.     tar --create --directory "${MUCDIR}" --gzip --file "${COMPRESSED}" "${MUCFILE}"  2>/dev/null
  123. fi
  124.  
  125.  
  126. # lines are sorted, dups are removed, counted, and reverse sorted
  127. # by number, largest first
  128. curr=$(awk '{print $0}' ${MUCFILE} | sort | uniq -c | sort -rn)
  129.  
  130. let i=0
  131. declare -a top_ten
  132.  
  133. while IFS= read -r  line
  134. do
  135.    
  136.     line=$( echo "${line}" |  sed -e 's/^ *//' -e 's/ *$//' ) # trim leading/trailing spaces
  137.    
  138.    
  139.     if [[ $VERBOSE ]] ; then
  140.         echo -e "${line}"
  141.     else
  142.         line=$( echo  "${line}" | cut -d " " -f2- )
  143.         echo -e "${line}"
  144.     fi
  145.        
  146.     top_ten[$i]=${line}
  147.    
  148.     # count to 10 then bail
  149.     [ $i -gt $(($LEN)) ] && break
  150.  
  151.    
  152.     i=$(($i+1))
  153.  
  154. done < <(printf %s "${curr}" )  ## read from variable
  155.  
  156.  
  157.  
  158. ## top ten
  159.  
  160. echo "TOP TEN" > "${TOPTEN}"
  161.  
  162. for (( i=0; i < ${#top_ten[@]}; i++))
  163. do
  164.     echo ${top_ten[$i]} >> "${TOPTEN}"
  165. done
  166.  
  167.  
  168.  
  169. # save space if new file
  170.  
  171. if [[ $REPLACE_TGZ ]] ; then
  172.     tar --create --directory "${MUCDIR}" --gzip --file "${COMPRESSED}" "${MUCFILE}" "${TOPTEN}" 2>/dev/null
  173. fi
  174.  
  175. rm "$MUCFILE"
  176.  
  177. IFS=${OLDIFS}
  178.  
  179. # Local Variables:   #
  180. # mode: shell-script #
  181. # tab-width: 4       #
  182. # End:               #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement