From 96aa95a3ee3800ba5b63ae1da8221eac1a58b9f4 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Mon, 29 Jun 2026 11:33:14 +0200 Subject: Speed up file-search and fix quoting/info bugs Replace per-line $(echo|cut) and $(basename) in search_file with bash parameter expansion (${line%%:*}, ${line#*:}, ${pkgname##*/}). On a 2504-package system this turns a broad 'lib' search from a >15min hang into 3.3s, and 'zlib' from 1.5s to 0.04s. Also: - info: guard single match via mapfile; list candidates on multiple matches instead of feeding less a broken multi-line path. - Quote $1/$2/$PKGS_PATH so empty args and spaces are handled. - Blank the color vars when stdout is not a TTY (no ANSI in pipes). - $(basename $0) -> ${0##*/}, /bin/ls -> ls. Co-Authored-By: Claude Opus 4.8 --- is_installed | 64 +++++++++++++++++++++++++++++++++--------------------------- 1 file 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}" 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}" 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 -- cgit v1.2.3