diff options
| -rwxr-xr-x | is_installed | 64 |
1 files changed, 35 insertions, 29 deletions
diff --git a/is_installed b/is_installed index 5ae7b1d..371248e 100755 --- a/is_installed +++ b/is_installed @@ -24,39 +24,40 @@ E_NOPKGFOUND=70 E_NOARGS=71 PKGS_PATH="/var/log/packages" -PROG=$(basename $0) +PROG="${0##*/}" -# colors -RED='\033[1;31m' -ORANGE='\033[1;33m' -GREEN='\033[1;32m' -MAGENTA='\033[1;35m' -NC='\033[0m' +# colors (disabled when stdout is not a terminal, e.g. piped) +if [ -t 1 ]; then + RED='\033[1;31m' + ORANGE='\033[1;33m' + GREEN='\033[1;32m' + MAGENTA='\033[1;35m' + NC='\033[0m' +else + RED='' ORANGE='' GREEN='' MAGENTA='' NC='' +fi function search_file () { SEARCH=$1 - if [ -z $1 ]; then + if [ -z "$SEARCH" ]; then #we don't have a string to search echo -e "Usage: ${MAGENTA}$PROG ${NC}[file-search] ${GREEN}< filename >" echo -e "${RED}exiting${NC}" exit $E_NOARGS fi - finding=$(grep -Hi "$SEARCH" ${PKGS_PATH}/*) - if [[ ! -z $finding ]]; then - old_IFS=$IFS - IFS=$'\n' + finding=$(grep -Hi "$SEARCH" "${PKGS_PATH}"/*) + if [[ -n $finding ]]; then old_pkgname="" - for line in $finding; do - pkgname=$(echo $line | cut -d":" -f 1) - match=$(echo $line | cut -d":" -f 2) + while IFS= read -r line; do + pkgname="${line%%:*}" + match="${line#*:}" if [[ $pkgname != $old_pkgname ]]; then - echo -e "${ORANGE}$(basename $pkgname)${NC}" + echo -e "${ORANGE}${pkgname##*/}${NC}" fi old_pkgname=$pkgname echo -en "\t${GREEN}$match${NC}\n" - done - IFS=${old_IFS} + done <<< "$finding" else echo -e "${ORANGE}no match for pattern ${GREEN}'$SEARCH' ${ORANGE}found" echo -e "${RED}exiting${NC}" @@ -66,14 +67,14 @@ function search_file () { function search_package () { SEARCH=$1 - if [ -z $1 ]; then + if [ -z "$SEARCH" ]; then #we don't have a string to search echo -e "Usage: ${MAGENTA}$PROG ${GREEN}<filename>" echo -e "${RED}exiting${NC}" exit $E_NOARGS fi - finding=$(/bin/ls -1 $PKGS_PATH |grep -i "$SEARCH") - if [[ ! -z $finding ]]; then + finding=$(ls -1 "$PKGS_PATH" | grep -i "$SEARCH") + if [[ -n $finding ]]; then echo -en "${GREEN}$finding\n" echo -e "${NC}" else @@ -85,15 +86,20 @@ function search_package () { function info () { SEARCH=$1 - if [ -z $1 ]; then + if [ -z "$SEARCH" ]; then #we don't have a string to search echo -e "Usage: ${MAGENTA}$PROG ${ORANGE}info ${GREEN}<filename>" echo -e "${RED}exiting${NC}" exit $E_NOARGS fi - finding=$(/bin/ls -1 $PKGS_PATH |grep -i "$SEARCH") - if [[ ! -z $finding ]]; then - less $PKGS_PATH/$finding + mapfile -t matches < <(ls -1 "$PKGS_PATH" | grep -i "$SEARCH") + if [[ ${#matches[@]} -eq 1 ]]; then + less "$PKGS_PATH/${matches[0]}" + elif [[ ${#matches[@]} -gt 1 ]]; then + # multiple matches: list them, do not feed garbage to less + echo -e "${ORANGE}multiple packages match ${GREEN}'$SEARCH'${ORANGE}, be more specific:${NC}" + printf "${GREEN}\t%s\n${NC}" "${matches[@]}" + exit $E_NOPKGFOUND else echo -e "${ORANGE}no package matching ${GREEN}'$SEARCH' ${ORANGE}found" echo -e "${RED}exiting${NC}" @@ -113,23 +119,23 @@ function help () { echo -e "${NC}" } # controlla se vengono passati parametri -if [ -z $1 ]; then +if [ -z "$1" ]; then help exit $E_NOARGS else case $1 in "file-search" ) - search_file $2 + search_file "$2" ;; "info" ) - info $2 + info "$2" ;; "help" ) help exit $SHOWHELP ;; * ) - search_package $1 + search_package "$1" ;; esac fi |
