Par Mushroom.
Ce script recherche si un paquet est installé en regardant dans le /var/log/packages et retourne le nom de celui-ci.
Exécutez le script avec en argument le nom du paquet recherché (ex. “sed”, “glibc-solibs”, “mozilla-firefox”). S'il est installé, binocle retournera son nom, autrement il retournera un code 2.
Pour connaître toutes les options d'utilisation, consultez binocle -h.
Placez simplement le script dans /usr/local/bin ou /usr/bin, avec les permissions et propriétés adéquates :
chmod 755 /usr/local/bin/mirza chown root:root /usr/local/bin/mirza
spklist.
#!/bin/sh # # BINOCLE - check if a package is installed. # # c. 2007-09-10 # r. 2007-09-13 2 # Copyright (c) 2007 Sébastien Boillod. # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. growl () { # Print error message on stderr an exits with the given non-null code. # growl <CODE> <MESSAGE> local ERR=$1 shift 1 if [ "$1" ]; then # If there is a second argument, we print arguments with 'echo'. echo "$*" 1>&2 else # Otherwise we use 'cat' and print stdin. cat <&0 >&2 fi [ $ERR = 0 ] || exit $ERR } reader () { # Print the content of given files from stdin. # reader <VERBOSITY LEVEL> local LS=$(cat <&0) # We making a list to optimize (all read in one process). if [ $1 -gt 1 ]; then cat $LS else sed '/^FILE LIST/,/$$/d' -s $LS fi } lspkg () { # List the packages matching the given name or list them all. # lspkg <BEGIN GLOB> <END GLOB> <NAME> local EXC=0 # EXit Code. if [ -z "$3" ]; then # If there is no name, we list all the packages ls -1 2>/dev/null || EXC=2 elif [ "$1$2" ]; then # We are requested for globs, we go. ls -1 $2$3$1 2>/dev/null || EXC=2 else # Ok, we are looking for an exact name. ( ls -1 $3-* 2>/dev/null | grep "^$3-[^-][^-]*-[^-][^-]*-[^-][^-]*$" ) || EXC=2 fi return $EXC } # Initialization... unset END BGN NOO PKG ARG VRB HLP for ARG in "$@"; do case $ARG in '-b') BGN="*" ;; '-e') END="*" ;; '-q') NOO="y" ;; '-v') VRB=${VRB:-"1"} ;; '-V') VRB=2 ;; '-h') HLP="cat" ;; -*) growl 0 "*** Error: unallowed '$ARG' option." HLP="growl 1" ;; *) if [ -z "$PKG" ]; then PKG=$ARG else growl 0 "*** Error: only one name expected." HLP="growl 1" fi esac done if [ "$HLP" ]; then # We display help and exit if we have to. $HLP << 3NDH3LP Without a name, binocle just lists the content of your '/var/log/packages' (or the directory defined as the 'ROOT' environment variable). With a name, it returns all the package log files that match this one. A name is expected to be the basename of a package, like "sed", "sendmail", or "glibc-solibs" (don't add version, architecture, nor whatever... consider the options bellow if you need to do that). binocle will exit with a code 0 if something is found, 1 if an error occurs and 2 if nothing matched. Usage: binocle [NAME] [-b|-e|-h|-q|-v|-V]. Options: -b --- match the log files that begin with the given name (can be used with '-e'). -e --- match the log files that end with the given name (can be used with '-b'). -h --- print this help on the standard output. -q --- be quiet and don't return anything but an exit code. -v --- be verbose and return the head of the matched log files (package infos and slack-desc). -V --- be very verbose and return the whole content of the matched log files (files list included). Bug reports, suggestions, feedbacks, questions, and so on are welcome here (the license is at the head of the script): <sbb at tuxfamily dot org>. 3NDH3LP exit 0 fi # By default, we check '/var/log/packages'. ARG=${ROOT:-"/var/log/packages"} cd $ARG 2>/dev/null || growl 1 "*** Error: can't enter in the '$ARG' directory." if [ "$NOO" ]; then # "No ouput" mode. lspkg "$BGN" "$END" $PKG 1>/dev/null elif [ "$VRB" ]; then # "Verbose" mode. lspkg "$BGN" "$END" $PKG | reader $VRB else # Default mode. lspkg "$BGN" "$END" $PKG fi # EOF