70 lines
1.9 KiB
Bash
Executable File
70 lines
1.9 KiB
Bash
Executable File
#!/bin/zsh
|
|
|
|
if [[ $# -ne 2 ]]; then
|
|
echo "Usage: $0 <command> <package>"
|
|
echo "Checks if a command is installed and if not, installs the package."
|
|
echo "Supported package managers: apt, pacman, dnf, yum, brew, port"
|
|
exit 1
|
|
fi
|
|
|
|
COMMAND=$1
|
|
PACKAGE=$2
|
|
|
|
error() {
|
|
echo "Your package manager is not supported."
|
|
echo "$COMMAND could not be installed automatically."
|
|
echo "Please install $COMMAND manually before running this script."
|
|
exit 1
|
|
}
|
|
|
|
if ! command -v $COMMAND &> /dev/null
|
|
then
|
|
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
|
|
echo "$COMMAND not found. Installing $PACKAGE..."
|
|
|
|
DISTRO=$(cat /etc/os-release | grep -oP '(?<=^ID=).+' | tr -d '"')
|
|
|
|
if [[ $DISTRO == "arch" ]]; then
|
|
sudo pacman -S $PACKAGE
|
|
exit 0
|
|
elif [[ $DISTRO == "archarm" ]]; then
|
|
sudo pacman -S $PACKAGE
|
|
exit 0
|
|
elif [[ $DISTRO == "ubuntu" ]]; then
|
|
sudo apt-get install $PACKAGE
|
|
exit 0
|
|
elif [[ $DISTRO == "fedora" ]]; then
|
|
sudo dnf install $PACKAGE
|
|
exit 0
|
|
elif [[ $DISTRO == "centos" ]]; then
|
|
sudo yum install $PACKAGE
|
|
exit 0
|
|
elif [[ $DISTRO == "debian" ]]; then
|
|
sudo apt install $PACKAGE
|
|
exit 0
|
|
else
|
|
error
|
|
fi
|
|
|
|
elif [[ "$OSTYPE" == "darwin"* ]]; then
|
|
echo "Command not found. Installing $COMMAND..."
|
|
|
|
if command -v "brew" &> /dev/null
|
|
then
|
|
brew install $PACKAGE
|
|
|
|
elif command -v "port" &> /dev/null
|
|
then
|
|
sudo port install $PACKAGE
|
|
|
|
else
|
|
echo "Homebrew or MacPorts not found. Please install one of them before running this script."
|
|
exit 1
|
|
fi
|
|
else
|
|
error
|
|
fi
|
|
else
|
|
echo "Package $PACKAGE which provides the command $COMMAND, is already installed."
|
|
fi
|