Moved command -v calls to function.

Signed-off-by: Dan Schaper <dan.schaper@pi-hole.net>
This commit is contained in:
Dan Schaper 2018-11-01 12:00:08 -07:00
parent a2ea6e8d47
commit 02f0788033
No known key found for this signature in database
GPG key ID: B4FF14C01CC08DC0

View file

@ -141,10 +141,19 @@ show_ascii_berry() {
" "
} }
is_command() {
# Checks for existence of string passed in as only function argument.
# Exit value of 0 when exists, 1 if not exists. Value is the result
# of the `command` shell built-in call.
local check_command="$1"
command -v "${check_command}" >/dev/null 2>&1
}
# Compatibility # Compatibility
distro_check() { distro_check() {
# If apt-get is installed, then we know it's part of the Debian family # If apt-get is installed, then we know it's part of the Debian family
if command -v apt-get &> /dev/null; then if is_command apt-get ; then
# Set some global variables here # Set some global variables here
# We don't set them earlier since the family might be Red Hat, so these values would be different # We don't set them earlier since the family might be Red Hat, so these values would be different
PKG_MANAGER="apt-get" PKG_MANAGER="apt-get"
@ -165,7 +174,7 @@ if command -v apt-get &> /dev/null; then
iproute_pkg="iproute" iproute_pkg="iproute"
fi fi
# Check for and determine version number (major and minor) of current php install # Check for and determine version number (major and minor) of current php install
if command -v php &> /dev/null; then if is_command php ; then
printf " %b Existing PHP installation detected : PHP version %s\\n" "${INFO}" "$(php <<< "<?php echo PHP_VERSION ?>")" printf " %b Existing PHP installation detected : PHP version %s\\n" "${INFO}" "$(php <<< "<?php echo PHP_VERSION ?>")"
printf -v phpInsMajor "%d" "$(php <<< "<?php echo PHP_MAJOR_VERSION ?>")" printf -v phpInsMajor "%d" "$(php <<< "<?php echo PHP_MAJOR_VERSION ?>")"
printf -v phpInsMinor "%d" "$(php <<< "<?php echo PHP_MINOR_VERSION ?>")" printf -v phpInsMinor "%d" "$(php <<< "<?php echo PHP_MINOR_VERSION ?>")"
@ -226,9 +235,9 @@ if command -v apt-get &> /dev/null; then
} }
# If apt-get is not found, check for rpm to see if it's a Red Hat family OS # If apt-get is not found, check for rpm to see if it's a Red Hat family OS
elif command -v rpm &> /dev/null; then elif is_command rpm ; then
# Then check if dnf or yum is the package manager # Then check if dnf or yum is the package manager
if command -v dnf &> /dev/null; then if is_command dnf ; then
PKG_MANAGER="dnf" PKG_MANAGER="dnf"
else else
PKG_MANAGER="yum" PKG_MANAGER="yum"
@ -521,7 +530,7 @@ verifyFreeDiskSpace() {
printf " %b Your system disk appears to only have %s KB free\\n" "${INFO}" "${existing_free_kilobytes}" printf " %b Your system disk appears to only have %s KB free\\n" "${INFO}" "${existing_free_kilobytes}"
printf " It is recommended to have a minimum of %s KB to run the Pi-hole\\n" "${required_free_kilobytes}" printf " It is recommended to have a minimum of %s KB to run the Pi-hole\\n" "${required_free_kilobytes}"
# if the vcgencmd command exists, # if the vcgencmd command exists,
if command -v vcgencmd &> /dev/null; then if is_command vcgencmd ; then
# it's probably a Raspbian install, so show a message about expanding the filesystem # it's probably a Raspbian install, so show a message about expanding the filesystem
printf " If this is a new install you may need to expand your disk\\n" printf " If this is a new install you may need to expand your disk\\n"
printf " Run 'sudo raspi-config', and choose the 'expand file system' option\\n" printf " Run 'sudo raspi-config', and choose the 'expand file system' option\\n"
@ -813,7 +822,7 @@ setStaticIPv4() {
# Use ip to immediately set the new address # Use ip to immediately set the new address
ip addr replace dev "${PIHOLE_INTERFACE}" "${IPV4_ADDRESS}" ip addr replace dev "${PIHOLE_INTERFACE}" "${IPV4_ADDRESS}"
# If NetworkMangler command line interface exists and ready to mangle, # If NetworkMangler command line interface exists and ready to mangle,
if command -v nmcli &> /dev/null && nmcli general status &> /dev/null; then if is_command nmcli && nmcli general status &> /dev/null; then
# Tell NetworkManagler to read our new sysconfig file # Tell NetworkManagler to read our new sysconfig file
nmcli con load "${IFCFG_FILE}" > /dev/null nmcli con load "${IFCFG_FILE}" > /dev/null
fi fi
@ -1309,7 +1318,7 @@ install_manpage() {
# Default location for man files for /usr/local/bin is /usr/local/share/man # Default location for man files for /usr/local/bin is /usr/local/share/man
# on lightweight systems may not be present, so check before copying. # on lightweight systems may not be present, so check before copying.
printf " %b Testing man page installation" "${INFO}" printf " %b Testing man page installation" "${INFO}"
if ! command -v mandb &>/dev/null; then if ! is_command mandb ; then
# if mandb is not present, no manpage support # if mandb is not present, no manpage support
printf "%b %b man not installed\\n" "${OVER}" "${INFO}" printf "%b %b man not installed\\n" "${OVER}" "${INFO}"
return return
@ -1347,7 +1356,7 @@ stop_service() {
# Can softfail, as process may not be installed when this is called # Can softfail, as process may not be installed when this is called
local str="Stopping ${1} service" local str="Stopping ${1} service"
printf " %b %s..." "${INFO}" "${str}" printf " %b %s..." "${INFO}" "${str}"
if command -v systemctl &> /dev/null; then if is_command systemctl ; then
systemctl stop "${1}" &> /dev/null || true systemctl stop "${1}" &> /dev/null || true
else else
service "${1}" stop &> /dev/null || true service "${1}" stop &> /dev/null || true
@ -1361,7 +1370,7 @@ start_service() {
local str="Starting ${1} service" local str="Starting ${1} service"
printf " %b %s..." "${INFO}" "${str}" printf " %b %s..." "${INFO}" "${str}"
# If systemctl exists, # If systemctl exists,
if command -v systemctl &> /dev/null; then if is_command systemctl ; then
# use that to restart the service # use that to restart the service
systemctl restart "${1}" &> /dev/null systemctl restart "${1}" &> /dev/null
# Otherwise, # Otherwise,
@ -1378,7 +1387,7 @@ enable_service() {
local str="Enabling ${1} service to start on reboot" local str="Enabling ${1} service to start on reboot"
printf " %b %s..." "${INFO}" "${str}" printf " %b %s..." "${INFO}" "${str}"
# If systemctl exists, # If systemctl exists,
if command -v systemctl &> /dev/null; then if is_command systemctl ; then
# use that to enable the service # use that to enable the service
systemctl enable "${1}" &> /dev/null systemctl enable "${1}" &> /dev/null
# Otherwise, # Otherwise,
@ -1395,7 +1404,7 @@ disable_service() {
local str="Disabling ${1} service" local str="Disabling ${1} service"
printf " %b %s..." "${INFO}" "${str}" printf " %b %s..." "${INFO}" "${str}"
# If systemctl exists, # If systemctl exists,
if command -v systemctl &> /dev/null; then if is_command systemctl ; then
# use that to disable the service # use that to disable the service
systemctl disable "${1}" &> /dev/null systemctl disable "${1}" &> /dev/null
# Otherwise, # Otherwise,
@ -1408,7 +1417,7 @@ disable_service() {
check_service_active() { check_service_active() {
# If systemctl exists, # If systemctl exists,
if command -v systemctl &> /dev/null; then if is_command systemctl ; then
# use that to check the status of the service # use that to check the status of the service
systemctl is-enabled "${1}" &> /dev/null systemctl is-enabled "${1}" &> /dev/null
# Otherwise, # Otherwise,
@ -1512,7 +1521,7 @@ install_dependent_packages() {
# amount of download traffic. # amount of download traffic.
# NOTE: We may be able to use this installArray in the future to create a list of package that were # NOTE: We may be able to use this installArray in the future to create a list of package that were
# installed by us, and remove only the installed packages, and not the entire list. # installed by us, and remove only the installed packages, and not the entire list.
if command -v debconf-apt-progress &> /dev/null; then if is_command debconf-apt-progress ; then
# For each package, # For each package,
for i in "${argArray1[@]}"; do for i in "${argArray1[@]}"; do
printf " %b Checking for %s..." "${INFO}" "${i}" printf " %b Checking for %s..." "${INFO}" "${i}"
@ -1661,7 +1670,7 @@ configureFirewall() {
firewall-cmd --reload firewall-cmd --reload
return 0 return 0
# Check for proper kernel modules to prevent failure # Check for proper kernel modules to prevent failure
elif modinfo ip_tables &> /dev/null && command -v iptables &> /dev/null; then elif modinfo ip_tables &> /dev/null && is_command iptables ; then
# If chain Policy is not ACCEPT or last Rule is not ACCEPT # If chain Policy is not ACCEPT or last Rule is not ACCEPT
# then check and insert our Rules above the DROP/REJECT Rule. # then check and insert our Rules above the DROP/REJECT Rule.
if iptables -S INPUT | head -n1 | grep -qv '^-P.*ACCEPT$' || iptables -S INPUT | tail -n1 | grep -qv '^-\(A\|P\).*ACCEPT$'; then if iptables -S INPUT | head -n1 | grep -qv '^-P.*ACCEPT$' || iptables -S INPUT | tail -n1 | grep -qv '^-\(A\|P\).*ACCEPT$'; then
@ -1791,7 +1800,7 @@ installPihole() {
# Give pihole access to the Web server group # Give pihole access to the Web server group
usermod -a -G ${LIGHTTPD_GROUP} pihole usermod -a -G ${LIGHTTPD_GROUP} pihole
# If the lighttpd command is executable, # If the lighttpd command is executable,
if [[ -x "$(command -v lighty-enable-mod)" ]]; then if is_command lighty-enable-mod ; then
# enable fastcgi and fastcgi-php # enable fastcgi and fastcgi-php
lighty-enable-mod fastcgi fastcgi-php > /dev/null || true lighty-enable-mod fastcgi fastcgi-php > /dev/null || true
else else
@ -1841,7 +1850,7 @@ installPihole() {
# SELinux # SELinux
checkSelinux() { checkSelinux() {
# If the getenforce command exists, # If the getenforce command exists,
if command -v getenforce &> /dev/null; then if is_command getenforce ; then
# Store the current mode in a variable # Store the current mode in a variable
enforceMode=$(getenforce) enforceMode=$(getenforce)
printf "\\n %b SELinux mode detected: %s\\n" "${INFO}" "${enforceMode}" printf "\\n %b SELinux mode detected: %s\\n" "${INFO}" "${enforceMode}"
@ -2341,7 +2350,7 @@ main() {
printf " %b Sudo utility check" "${INFO}" printf " %b Sudo utility check" "${INFO}"
# If the sudo command exists, # If the sudo command exists,
if command -v sudo &> /dev/null; then if is_command sudo ; then
printf "%b %b Sudo utility check\\n" "${OVER}" "${TICK}" printf "%b %b Sudo utility check\\n" "${OVER}" "${TICK}"
# Download the install script and run it with admin rights # Download the install script and run it with admin rights
exec curl -sSL https://raw.githubusercontent.com/pi-hole/pi-hole/master/automated%20install/basic-install.sh | sudo bash "$@" exec curl -sSL https://raw.githubusercontent.com/pi-hole/pi-hole/master/automated%20install/basic-install.sh | sudo bash "$@"