Advertisement
Guest User

Untitled

a guest
Apr 28th, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.01 KB | None | 0 0
  1. #   apt_completion - APT-related completion functions for bash
  2. #
  3. #   Copyright (C) 2007 Martin Nordholts <enselic@gmail.com>
  4. #
  5. #   This file heavily based on 'bash_completion' which is
  6. #   Copyright (C) Ian Macdonald <ian@caliban.org>
  7. #
  8. #   This program is free software; you can redistribute it and/or modify
  9. #   it under the terms of the GNU General Public License as published by
  10. #   the Free Software Foundation; either version 2, or (at your option)
  11. #   any later version.
  12. #
  13. #   This program is distributed in the hope that it will be useful,
  14. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. #   GNU General Public License for more details.
  17. #
  18. #   You should have received a copy of the GNU General Public License
  19. #   along with this program; if not, write to the Free Software Foundation,
  20. #   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. #
  22. #   Last edited: 2007-03-24
  23.  
  24. ############################################################
  25. ########################    SETUP    #######################
  26.  
  27.  
  28. # These variables specifies what aliases you want
  29.  
  30. SUDO_APT_GET_INSTALL_ALIAS=sagi
  31. SUDO_APT_GET_REMOVE_ALIAS=sagr
  32. APT_CACHE_SHOW_ALIAS=acsh
  33. APT_CACHE_SEARCH_ALIAS=acs
  34.  
  35.  
  36. ########################  END SETUP  #######################
  37. ############################################################
  38.  
  39. # features supported by bash 2.05 and higher
  40. if [ ${BASH_VERSINFO[0]} -eq 2 ] && [[ ${BASH_VERSINFO[1]} > 04 ]] ||
  41.    [ ${BASH_VERSINFO[0]} -gt 2 ]; then
  42.     filenames="-o filenames"
  43. fi
  44.  
  45. # declare the aliases
  46. alias $SUDO_APT_GET_INSTALL_ALIAS='sudo apt-get install'
  47. alias $SUDO_APT_GET_REMOVE_ALIAS='sudo apt-get remove'
  48. alias $APT_CACHE_SHOW_ALIAS='apt-cache show'
  49.  
  50. #does not require completion, but is useful to have aliased
  51. alias $APT_CACHE_SEARCH_ALIAS='apt-cache search'
  52.  
  53.  
  54.  
  55. #
  56. # initialize completion
  57. #
  58.  
  59. # This function checks whether we have a given program on the system.
  60. # No need for bulky functions in memory if we don't.
  61. #
  62. have()
  63. {
  64.     unset -v have
  65.     PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin type $1 &>/dev/null &&
  66.         have="yes"
  67. }
  68.  
  69. have apt-get &&
  70. _apt_get_install()
  71. {
  72.     local cur
  73.  
  74.     COMPREPLY=()
  75.     cur=${COMP_WORDS[COMP_CWORD]}
  76.  
  77.     COMPREPLY=( $( apt-cache pkgnames $cur 2> /dev/null ) )
  78.     return 0
  79. } &&
  80. complete -F _apt_get_install $filenames $SUDO_APT_GET_INSTALL_ALIAS
  81.  
  82. have apt-get &&
  83. _apt_get_remove()
  84. {
  85.     local cur
  86.  
  87.     COMPREPLY=()
  88.     cur=${COMP_WORDS[COMP_CWORD]}
  89.  
  90.     if [ -f /etc/debian_version ]; then
  91.         # Debian system
  92.         COMPREPLY=( $( _comp_dpkg_installed_packages \
  93.                 $cur ) )
  94.     else
  95.         # assume RPM based
  96.         _rpm_installed_packages
  97.     fi
  98.  
  99.     return 0
  100. } &&
  101. complete -F _apt_get_remove $filenames $SUDO_APT_GET_REMOVE_ALIAS
  102.  
  103. have apt-cache &&
  104. _apt_cache_show()
  105. {
  106.     local cur
  107.  
  108.     COMPREPLY=()
  109.     cur=${COMP_WORDS[COMP_CWORD]}
  110.    
  111.     COMPREPLY=( $( apt-cache pkgnames $cur 2> /dev/null ) )
  112.  
  113.     return 0
  114. } &&
  115. complete -F _apt_cache_show $filenames $APT_CACHE_SHOW_ALIAS
  116.  
  117.  
  118.  
  119. #
  120. # setup get-packages functions
  121. #
  122.  
  123. have dpkg && {
  124. have grep-status && {
  125. _comp_dpkg_installed_packages()
  126. {
  127.     grep-status -P -e "^$1" -a -FStatus 'install ok installed' -n -s Package
  128. }
  129. } || {
  130. _comp_dpkg_installed_packages()
  131. {
  132.     grep -A 2 "Package: $1" /var/lib/dpkg/status | \
  133.         grep -B 2 'ok installed' | grep "Package: $1" | cut -d\  -f2
  134. }
  135. }
  136. }
  137.  
  138. have rpm &&
  139. _rpm_installed_packages()
  140. {
  141.     local ver nodig nosig
  142.  
  143.     if [ -r /var/log/rpmpkgs -a \
  144.         /var/log/rpmpkgs -nt /var/lib/rpm/Packages ]; then
  145.         # using RHL 7.2 or later - this is quicker than querying the DB
  146.         COMPREPLY=( $( sed -ne \
  147.         's|^\('$cur'.*\)-[0-9a-zA-Z._]\+-[0-9a-z.@]\+.*\.rpm$|\1|p' \
  148.                 /var/log/rpmpkgs ) )
  149.     else
  150.         nodig=""
  151.         nosig=""
  152.         ver=$(rpm --version)
  153.         ver=${ver##* }
  154.      
  155.         if [[ "$ver" > "4.0.4" ]]; then
  156.             nodig="--nodigest"
  157.         fi
  158.         if [[ "$ver" > "4.0.99" ]]; then
  159.             nosig="--nosignature"
  160.         fi
  161.  
  162.         COMPREPLY=( $( rpm -qa $nodig $nosig | sed -ne \
  163.         's|^\('$cur'.*\)-[0-9a-zA-Z._]\+-[0-9a-z.@]\+$|\1|p' ) )
  164.     fi
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement