0

I'm writing a bash script that is a "package manager" that uses pi-apps.
I want to implement a search function so I can search apps. right now what I'm doing is this (template is a folder I don't wan't it to list):

#test-search.sh

PI_APPS_DIR="$HOME/pi-apps"

function list-all() {
    for dir in $PI_APPS_DIR/apps/*/; do
        dirname=$(basename "$dir")
        if [[ "$dirname" != "template" ]]; then
            echo -e "\n${bold}${inverted}${light_blue}$dirname${normal}"
            DESC="${green}$(cat "$dir"/description)${normal}"
            echo -e $DESC
        fi
    done
}

function search() {
    for dir in $PI_APPS_DIR/apps/*/; do
        dirname=$(basename "$dir")
        if [[ "$dirname" != "template" ]]; then
            echo "$dirname" | grep "$1"
        fi
    done
}


# the function is then called like this
if [[ "$1" == "search" ]]; then
    #search a app
    FIND="$(search "$2")"
    list-all | grep "$FIND"
fi

and run it like this: ./test-search.sh search "APP" (app being the search term).

what happens is this: if the searh term is in the name of a app and its description, I get something like this:

./test-search.sh search "Q"
QEMU
description.......

but if I use q, it will just list all the apps. and if the search term is only in the description of the app, only the description gets printed.

the file tree looks like this:

~/pi-apps/apps inside apps there is a folder for each app named the name of the app, Zoom for example. there is 1 folder called template that I don't want to list.

here is the same project by someone else but written in python: https://github.com/techcoder20/PiAppsTerminalAdvanced/blob/main/PiAppsTerminalAdvanced.py I think it helps to understand what I want to do.

12
  • 1
    What is your question? Commented Mar 22, 2021 at 22:58
  • @berndbausch how to implement search that searches from a list of apps, then prints them with their description (using cat for the description) so the output is similar to the one of apt search Commented Mar 22, 2021 at 23:03
  • 2
    I thought you have a script that does that. If it doesn't work as expected, you need to say what unexpected results you get. Commented Mar 22, 2021 at 23:06
  • 1
    When search returns the empty string, you then call list-all and grep for the empty string. Therefore, everything is listed. Commented Mar 22, 2021 at 23:32
  • 1
    Another problem: Only those lines of the description are listed that contain the search string. Commented Mar 22, 2021 at 23:34

1 Answer 1

1

I hope this can be accepted as an answer.

Your algorithm should be:

  1. find directories whose names contain the search string. If any are found, print the directory name and cat the corresponding description.

For example:

for dir in *${SEARCHSTRING}*
do
  echo $dir
  cat "$dir/description"
done
  1. among all the other directories, find descriptions that contain the search string. If found, see above.

For example:

for dir in *
do
  if [[ $dir != *${SEARCHSTRING}* ]]
  then 
    if grep -q "$SEARCHSTRING"
    then 
      echo $dir
      cat "$dir/$SEARCHSTRING"
    fi
  fi
done

The two loops can be combined into one without much effort. CAVEAT: I just threw them together without testing and without adapting them to your existing program.

What your algorithm actually does:

  1. Find directories that contain the search string. Then cat all descriptions and grep for the result of that search, even if that result is empty. There are at least two unwanted consequences: When searching for a non-existing directory, everything will be printed. Also, only the description lines that contain the search string are printed.
  2. You don't address the case that the description, but not the directory, contains the search string.

This can't be fixed by changing a line or two. I think a redesign is needed.

1
  • I have no idea how to do that, could you give a example please? Commented Mar 23, 2021 at 7:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .