Okay all you Linux/shell folks out there. This is my first shell script (2nd if you count "Hello World") and would appreciate some feedback.
Basically, I need to test whether htop
is installed; if it is, then move on; if it is not, then install it. If the user is root then just do it, and if not, sudo and install it. I have "borrowed" code from here to stitch this together. It works in my test environment. I would like to know:
What did I miss? What other variables do I need to account for? Are there better ways to go about this?
if ! [ -x "$(command -v htop)" ]; then
# set the var SUDO to "" (blank)
SUDO=""
# if the user ID is not 0 (root is 0)
if [ $EUID != "0" ] ; then
# set the var SUDO to "sudo"
SUDO="sudo"
fi
# Let the user know it is not installed & then install it, using sudo if not root
echo 'Error: htop is not installed.' && $SUDO apt install htop >&1
# exit the script
exit 0
fi
sudo
for a user if they haven't explicitly granted it (by running the script viasudo
in the first place), and it's possible the user would prefer a different installation mechanism, such as compiling from source. \$\endgroup\$