Skip to main content
2 of 5
added 5 characters in body
jesse_b
  • 41.6k
  • 14
  • 108
  • 162

I wouldn't call this elegant but I think it might do the job:

search_packages () {
    local packages=($@)
    local results
    for package in "${packages[@]}"; do
        results=($(apt-cache -n search "$package"))
        if [[ "${#results[@]}" -eq 0 ]]; then
            echo "$package not found."
        elif [[ "${#results[@]}" -eq 1 ]]; then
            do stuff with "$package"
        else
            echo "Warning! Found multiple packages:"
            for result in "${results[@]}"; do
                echo -e "\t$result"
            done
        fi
    done
}

I don't have a debian machine to test on unfortunately. I've included the -n for "names-only" option of apt-cache to try and limit the search results as it looks like you are mostly sure of what you are searching.

Can be run like:

$ search_packages hunspell-en-zz hunspell-en
$ my_packages=('hunspell-en-zz' 'hunspell-en')
$ search_packages "${my_packages[@]}"
jesse_b
  • 41.6k
  • 14
  • 108
  • 162