search.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env bash
  2. # Presents FreeBSD "pkg search" results with additional information.
  3. #
  4. # Shows FreeBSD "pkg search" output with (a) if package is installed
  5. # and (b) if different version is installed than the results.
  6. #
  7. # Example output:
  8. # $ bash search.sh xset
  9. # [*] xset-1.2.5 User preference utility for X
  10. # [*] (installed: 1.1.1) xsetroot-1.1.2 Root window parameter setting utility for X
  11. # [ ] xsettingsd-1.0.0.2_1 Daemon that implements the XSETTINGS specification
  12. # Function to return modified "pkg search" results.
  13. # Params:
  14. # $1: What to do "pkg search" for
  15. function better_search_freebsd_package {
  16. # Get general search result
  17. local _search=$(pkg search "$1")
  18. # Get all installed package info
  19. local _installed=$(pkg info -a)
  20. local the_package=''
  21. local the_package_name=''
  22. local the_package_version=''
  23. local installed_package=''
  24. local installed_package_name=''
  25. local installed_package_version=''
  26. while IFS='' read line; do
  27. # Gather info from current line
  28. # Package name and version without description
  29. the_package="${line%% *}"
  30. # Package name only
  31. the_package_name="${the_package%-*}"
  32. # Package version only
  33. the_package_version="${the_package##*-}"
  34. # Gather info about installed package
  35. installed_package=$(grep "^${the_package_name}\-[0-9]" <<<"$_installed")
  36. installed_package="${installed_package%% *}"
  37. installed_package_name="${installed_package%%-*}"
  38. installed_package_version="${installed_package##*-}"
  39. # Print line
  40. if [ -z "$installed_package" ]; then
  41. echo -n '[ ] '
  42. else
  43. echo -n '[*] '
  44. if [ "$the_package_version" != "$installed_package_version" ]; then
  45. echo -n "(installed: ${installed_package_version}) "
  46. fi
  47. fi
  48. echo "$line"
  49. done <<<"$_search"
  50. }
  51. # When this script is run with an argument, pass it to the function.
  52. better_search_freebsd_package "$1"