Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # Most Used Commands (muc)
- # keep a list of the most used commands
- # Author Chris Reid <spikeysnack@gmail.com>
- # DATE 4 Oct 2016
- # LICENSE "free for non-commercial use.
- # Modification allowed but original
- # attribution must be published.
- #
- OLDIFS=${IFS}
- MUCDIR="$HOME/.muc"
- LEN=10 # how many to show
- REPLACE_TGZ= # compress for space?
- COMPRESSED="${MUCDIR}/mucfile.tgz"
- MUCFILE="${MUCDIR}/mucfile"
- TOPTEN="${MUCDIR}/topten.muc"
- CURRDIR="${PWD}"
- usage()
- {
- cat <<EOF
- usage: $0 [options] [<N>]
- N number of results (default 10)
- OPTIONS:
- -c clean muc dir of all files
- -h show help
- -u update muc file
- -v verbose
- -z replace tarfile
- EOF
- }
- while getopts "chuvz" OPTION
- do
- case $OPTION in
- c)
- CLEAR=1
- ;;
- h)
- usage
- exit 1
- ;;
- u)
- UPDATE=1
- ;;
- v)
- VERBOSE=1
- ;;
- z)
- REPLACE_TGZ=1
- ;;
- ?)
- usage
- exit
- ;;
- esac
- done
- #shift $(($OPTIND - 1))
- N=${!OPTIND}
- if [[ $N ]] ; then
- if (( $N == $N )) ; then # need a number
- LEN=${N}
- else
- [[ $VERBOSE ]] && echo "need a number (10)"
- usage
- exit -1
- fi
- fi
- [[ $CLEAR ]] && rm -f "${MUCFILE}" "${COMPRESSED}" "${TOPTEN}"
- [ -d "${MUCDIR}" ] && mkdir -p "${MUCDIR}" # create muc dir if needed
- [ -f "${COMPRESSED}" ] && tar --extract --gunzip --file "${COMPRESSED}" --directory "${MUCDIR}" --local 2>/dev/null # decompress if found
- now=$(date +"%Y-%m-%d") # today's date
- if ! [ -f "${MUCFILE}" ] ; then
- touch "${MUCFILE}"
- echo "${now}" > "${MUCFILE}"
- cat $HOME/.bash_history >> "${MUCFILE}" # create file from history if needed
- fi
- last=$(stat -c %y ${MUCFILE} | cut -d ' ' -f1) # check date of mucfile
- # echo -e "[$last|$now]\n"
- if [[ $UPDATE ]] || ! [ "X${last}" == "X${now}" ] ; then # if not current
- [[ $VERBOSE ]] && echo "Updating muc file"
- replace="1c\\${now}" # (sed) replace first line of file
- sed -i ${replace} "${MUCFILE}" # replace 1st line
- cat ~/.bash_history >> "${MUCFILE}" # append bash history
- REPLACE_TGZ="1" # we will update archive
- tar --create --directory "${MUCDIR}" --gzip --file "${COMPRESSED}" "${MUCFILE}" 2>/dev/null
- fi
- # lines are sorted, dups are removed, counted, and reverse sorted
- # by number, largest first
- curr=$(awk '{print $0}' ${MUCFILE} | sort | uniq -c | sort -rn)
- let i=0
- declare -a top_ten
- while IFS= read -r line
- do
- line=$( echo "${line}" | sed -e 's/^ *//' -e 's/ *$//' ) # trim leading/trailing spaces
- if [[ $VERBOSE ]] ; then
- echo -e "${line}"
- else
- line=$( echo "${line}" | cut -d " " -f2- )
- echo -e "${line}"
- fi
- top_ten[$i]=${line}
- # count to 10 then bail
- [ $i -gt $(($LEN)) ] && break
- i=$(($i+1))
- done < <(printf %s "${curr}" ) ## read from variable
- ## top ten
- echo "TOP TEN" > "${TOPTEN}"
- for (( i=0; i < ${#top_ten[@]}; i++))
- do
- echo ${top_ten[$i]} >> "${TOPTEN}"
- done
- # save space if new file
- if [[ $REPLACE_TGZ ]] ; then
- tar --create --directory "${MUCDIR}" --gzip --file "${COMPRESSED}" "${MUCFILE}" "${TOPTEN}" 2>/dev/null
- fi
- rm "$MUCFILE"
- IFS=${OLDIFS}
- # Local Variables: #
- # mode: shell-script #
- # tab-width: 4 #
- # End: #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement