I'm new to writing bash scripts and was wondering if I could get someone's advice on a part of the script I'm working on.
Intended purpose of code
Check if a package exists using dpkg
, and if it doesn't, offer to install it for the user. This snippet is part of a larger script that installs a particular Conky configuration along with all of its dependencies with minimal effort from the user.
Concerns
- I feel as though there is a more elegant way to check if a package is installed using
dpkg
(code was found on Stack Overflow). - Is there a better way of handling the (y/n) response?
Here is the code that I am using:
declare -a packages=("conky-all" "lm-sensors");
for i in "${packages[@]}"; do
if [ $(dpkg-query -W -f='${Status}' $i 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
echo "$i is not installed, would you like to install it now? (Y/N)";
read response
if [ "$response" == "y" ] || [ "$response" == "Y" ]; then
sudo apt-get install "$i";
else
echo "Skipping the installation of $i...";
echo "Please note that this Conky configuration will not work without the $i package.";
fi
else
echo "The $i package has already been installed.";
fi
done