Advertisement
Guest User

check_unused_bash_completion.sh

a guest
May 9th, 2011
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.26 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # check_unused_bash_completion.sh:
  5. #   Checks unused bash_completion files and moves them into
  6. #   /etc/bash_completion.d/unused/.
  7. #
  8.  
  9. BASH_COMPLETION_DIR=/etc/bash_completion.d
  10. BASH_COMPLETION_DIR_UNUSED="$BASH_COMPLETION_DIR/unused"
  11.  
  12. # This function checks whether we have a given program on the system.
  13. # No need for bulky functions in memory if we don't.
  14. #
  15. have()
  16. {
  17.     unset -v have
  18.     # Completions for system administrator commands are installed as well in
  19.     # case completion is attempted via `sudo command ...'.
  20.     PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin type $1 &>/dev/null &&
  21.     have="yes"
  22. }
  23.  
  24. if [ ! -d $BASH_COMPLETION_DIR_UNUSED ]; then
  25.     mkdir $BASH_COMPLETION_DIR_UNUSED
  26. fi
  27.  
  28. for i in $(LC_ALL=C command ls "$BASH_COMPLETION_DIR"); do
  29.     unset -v have
  30.     i=$BASH_COMPLETION_DIR/$i
  31.     if [[ ${i##*/} != @(*~|*.bak|*.swp|\#*\#|*.dpkg*|*.rpm@(orig|new|save)|Makefile*) \
  32.         && -f $i && -r $i ]]; then
  33.         for j in $(grep -P '^\(?have' "$i" | grep -o -P '(?<=\bhave )[^ )]+'); do
  34.             have "$j" && break
  35.         done
  36.         if [ "$have" = "yes" ]; then
  37.             echo "$i: used"
  38.         else
  39.             echo "$i: unused"
  40.             mv "$i" $BASH_COMPLETION_DIR_UNUSED
  41.         fi
  42.     fi
  43. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement