Skip to content

Instantly share code, notes, and snippets.

@jameshulse
Created January 12, 2024 22:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameshulse/dcf3686066466f71578247a7ce27bb94 to your computer and use it in GitHub Desktop.
Save jameshulse/dcf3686066466f71578247a7ce27bb94 to your computer and use it in GitHub Desktop.
Bash script to determine how a binary was installed
how() {
YELLOW='\033[1;33m'
NC='\033[0m'
if [[ "$1" == "" || "$1" == "-h" || "$1" == "--help" ]]; then
echo "Usage: $0 <binary name>"
echo ""
echo "This will search for the binary in a few different places and try and determine how it was installed."
return 1
fi
if command -v dnf > /dev/null; then
DNF_RESULT=`dnf list --installed | grep "$1"`
if echo "$DNF_RESULT" | awk '{print $1}' | cut -d'.' -f1 | grep -q "$1"; then
if echo "$DNF_RESULT" | grep -q "@@commandline"; then
echo "Installed via: ${YELLOW}dnf${NC} (local/manual install of .rpm)"
return 0
else
echo "Installed via: ${YELLOW}dnf${NC}"
return 0
fi
fi
fi
if command -v brew > /dev/null; then
if brew list | awk '{print}' | grep -q "$1"; then
echo "Installed via: ${YELLOW}brew${NC}"
return 0
fi
fi
if command -v flatpak > /dev/null; then
# TODO
fi
if command -v snap > /dev/null; then
# TODO
fi
if command -v composer > /dev/null; then
COMPOSER_HOME=`composer config --list --global | grep '\[home\]' | awk '{print $2}'`
if [[ -f "$COMPOSER_HOME/vendor/bin/$1" ]]; then
echo "Installed via: ${YELLOW}composer${NC} (global)"
return 0
fi
if [[ -f "./vendor/bin/$1" ]]; then
echo "Installed via: ${YELLOW}composer${NC} (local)"
return 0
fi
fi
if command -v alias > /dev/null; then
if alias | grep "^$1=" > /dev/null; then
echo "${YELLOW}$1${NC} is an alias"
return 0
fi
fi
echo "Checks didn't return anything. Probably manually installed. TODO: Implement flatpak, snap etc"
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment