From 162e56899a74ec6c0867e0ffbeab857e50cb3aa9 Mon Sep 17 00:00:00 2001 From: joglomedia Date: Wed, 31 Jul 2019 21:55:18 +0700 Subject: [PATCH] Fix shellcheck issue #3 --- .travis.yml | 7 ++++--- lemper.sh | 21 +++++++++++++------ scripts/helper.sh | 12 ----------- scripts/install_imagemagick.sh | 8 +++++--- scripts/install_letsencrypt.sh | 7 ++++--- scripts/install_php.sh | 37 ++++++++++++++++++---------------- 6 files changed, 48 insertions(+), 44 deletions(-) diff --git a/.travis.yml b/.travis.yml index e5026a9..4e1f5e6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,7 @@ env: #script: # Run the source through ShellCheck (http://www.shellcheck.net). # - shellcheck -s bash -x scripts/helper.sh +# - shellcheck -s bash scripts/install_*.sh # - shellcheck -s bash -x lemper.sh # - shellcheck -s bash -x bin/lemper-cli.sh # Run the source through ShUnit2 @@ -29,9 +30,9 @@ matrix: script: # Run the source through ShellCheck (http://www.shellcheck.net). - shellcheck -s bash -x scripts/helper.sh + - #shellcheck -s bash scripts/install_*.sh - shellcheck -s bash -x lemper.sh - - shellcheck -s bash -x bin/lemper-cli.sh - - shellcheck -s bash lib/lemper-create.sh - - shellcheck -s bash lib/lemper-manage.sh + - shellcheck -s bash bin/lemper-cli.sh + - shellcheck -s bash lib/lemper-*.sh # Run the source through ShUnit2 - bash shunit2/run_test.sh \ No newline at end of file diff --git a/lemper.sh b/lemper.sh index cd2d1e2..a1936b1 100755 --- a/lemper.sh +++ b/lemper.sh @@ -35,10 +35,16 @@ if [ -z "${PATH}" ] ; then PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin fi +# Unset existing variables. +# shellcheck source=.env +# shellcheck disable=SC2046 +unset $(grep -v '^#' .env | grep -v '^\[' | sed -E 's/(.*)=.*/\1/' | xargs) + # Export environment variables. if [ -f .env ]; then + # shellcheck source=.env + # shellcheck disable=SC1094 source <(grep -v '^#' .env | grep -v '^\[' | sed -E 's|^(.+)=(.*)$|: ${\1=\2}; export \1|g') - #unset $(grep -v '^#' .env | grep -v '^\[' | sed -E 's/(.*)=.*/\1/' | xargs) else echo "Environment variables required, but not found." exit 1 @@ -58,11 +64,8 @@ if [ "$(id -u)" -ne 0 ]; then exit 1 fi -# Init log. -run init_log - # Make sure this script only run on supported distribution. -export DISTRIB_REPO \ +export DISTRIB_REPO && \ DISTRIB_REPO=$(get_release_name) if [[ "${DISTRIB_REPO}" == "unsupported" ]]; then warning "This installer only work on Ubuntu 16.04 & 18.04 and LinuxMint 18 & 19..." @@ -76,7 +79,7 @@ else fi ### Main ### -case ${1} in +case "${1}" in --install) header_msg echo "" @@ -85,6 +88,9 @@ case ${1} in echo "" read -t 10 -rp "Press [Enter] to continue..." > lemper.log 2>&1 diff --git a/scripts/helper.sh b/scripts/helper.sh index a209359..b320f2c 100644 --- a/scripts/helper.sh +++ b/scripts/helper.sh @@ -6,18 +6,6 @@ # Author : ESLabs.ID (eslabs.id@gmail.com) # Since Version : 1.0.0 -set -e -unset GREP_OPTIONS LEMPHOMEDIR CURRENTTRAP - -# Export environment variables. -if [ -f .env ]; then - source <(grep -v '^#' .env | grep -v '^\[' | sed -E 's|^(.+)=(.*)$|: ${\1=\2}; export \1|g') - #unset $(grep -v '^#' .env | grep -v '^\[' | sed -E 's/(.*)=.*/\1/' | xargs) -else - echo "Environment variables required, but not found." - exit 0 -fi - # Make sure only root can run this installer script. function requires_root() { if [ "$(/usr/bin/id -u)" -ne 0 ]; then diff --git a/scripts/install_imagemagick.sh b/scripts/install_imagemagick.sh index 43c1089..12b839d 100755 --- a/scripts/install_imagemagick.sh +++ b/scripts/install_imagemagick.sh @@ -9,7 +9,9 @@ # Include helper functions. if [ "$(type -t run)" != "function" ]; then BASEDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd ) - . ${BASEDIR}/helper.sh + # shellchechk source=scripts/helper.sh + # shellcheck disable=SC1090 + . "${BASEDIR}/helper.sh" fi # Make sure only root can run this installer script @@ -32,9 +34,9 @@ run rm -fr ImageMagick-7* if "${DRYRUN}"; then status "ImageMagic installed in dryrun mode." else - if [ -n "$(magick -version |grep -o 'Version: ImageMagic *')" ]; then + if magick -version |grep -qo 'Version: ImageMagic *'; then status "ImageMagic version $(magick -version |grep ^Version | cut -d' ' -f3) has been installed." - elif [ -n "$(identify -version |grep -o 'Version: ImageMagic *')" ]; then + elif identify -version |grep -qo 'Version: ImageMagic *'; then status "ImageMagic version $(identify -version |grep ^Version | cut -d' ' -f3) has been installed." fi fi diff --git a/scripts/install_letsencrypt.sh b/scripts/install_letsencrypt.sh index 02632db..3bb6bba 100755 --- a/scripts/install_letsencrypt.sh +++ b/scripts/install_letsencrypt.sh @@ -9,7 +9,8 @@ # Include helper functions. if [ "$(type -t run)" != "function" ]; then BASEDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd ) - . ${BASEDIR}/helper.sh + # shellchechk source=scripts/helper.sh + . "${BASEDIR}/helper.sh" fi # Make sure only root can run this installer script @@ -23,8 +24,8 @@ echo "Welcome to Certbot Let's Encrypt Installation..." echo "" function init_certbotle_install() { - while [[ $INSTALL_CERTBOT != "y" && $INSTALL_CERTBOT != "n" ]]; do - read -p "Do you want to install Certbot Let's Encrypt? [y/n]: " -e INSTALL_CERTBOT + while [[ ${INSTALL_CERTBOT} != "y" && ${INSTALL_CERTBOT} != "n" ]]; do + read -rp "Do you want to install Certbot Let's Encrypt? [y/n]: " -e INSTALL_CERTBOT done if [[ "${INSTALL_CERTBOT}" == Y* || "${INSTALL_CERTBOT}" == y* ]]; then echo -e "\nInstalling Certbot Let's Encrypt client..." diff --git a/scripts/install_php.sh b/scripts/install_php.sh index 15c0cc1..f93127d 100755 --- a/scripts/install_php.sh +++ b/scripts/install_php.sh @@ -9,7 +9,8 @@ # Include helper functions. if [ "$(type -t run)" != "function" ]; then BASEDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd ) - . ${BASEDIR}/helper.sh + # shellchechk source=scripts/helper.sh + . "${BASEDIR}/helper.sh" fi # Make sure only root can run this installer script. @@ -28,7 +29,7 @@ function install_php() { fi # Checking if php already installed. - if [[ -n $(command -v php${PHPv}) ]]; then + if [[ -n $(command -v "php${PHPv}") ]]; then warning "PHP${PHPv} & FPM package already installed..." else echo "Installing PHP${PHPv} & FPM..." @@ -438,22 +439,24 @@ EOL # Add custom php extension (ex .php70, .php71) PHPExt=".php${PHPv//.}" - run sed -i "s/;\(security\.limit_extensions\s*=\s*\).*$/\1\.php\ $PHPExt/" /etc/php/${PHPv}/fpm/pool.d/www.conf + run sed -i "s/;\(security\.limit_extensions\s*=\s*\).*$/\1\.php\ $PHPExt/" \ + "/etc/php/${PHPv}/fpm/pool.d/www.conf" # Enable FPM ping service - run sed -i "/^;ping.path\ =.*/a ping.path\ =\ \/ping" /etc/php/${PHPv}/fpm/pool.d/www.conf + run sed -i "/^;ping.path\ =.*/a ping.path\ =\ \/ping" "/etc/php/${PHPv}/fpm/pool.d/www.conf" # Enable FPM status - run sed -i "/^;pm.status_path\ =.*/a pm.status_path\ =\ \/status" /etc/php/${PHPv}/fpm/pool.d/www.conf + run sed -i "/^;pm.status_path\ =.*/a pm.status_path\ =\ \/status" \ + "/etc/php/${PHPv}/fpm/pool.d/www.conf" # Restart PHP-fpm server - if [[ $(ps -ef | grep -v grep | grep php-fpm | wc -l) > 0 ]]; then - run service php${PHPv}-fpm reload + if [[ $(pgrep -c "php-fpm${PHPv}") -gt 0 ]]; then + run service "php${PHPv}-fpm" reload status "PHP${PHPv}-FPM restarted successfully." - elif [[ -n $(command -v php${PHPv}) ]]; then - run service php${PHPv}-fpm start + elif [[ -n $(command -v "php${PHPv}") ]]; then + run service "php${PHPv}-fpm" start - if [[ $(ps -ef | grep -v grep | grep php-fpm | wc -l) > 0 ]]; then + if [[ $(pgrep -c "php-fpm${PHPv}") -gt 0 ]]; then status "PHP${PHPv}-FPM started successfully." else warning "Something wrong with PHP installation." @@ -480,15 +483,15 @@ function init_php_install() { echo " 6). All available versions" echo "---------------------------------" - while [[ $SELECTED_PHP != "1" && $SELECTED_PHP != "2" \ - && $SELECTED_PHP != "3" && $SELECTED_PHP != "4" \ - && $SELECTED_PHP != "5" && $SELECTED_PHP != "6" ]]; do - read -p "Select an option [1-6]: " SELECTED_PHP + while [[ ${SELECTED_PHP} != "1" && ${SELECTED_PHP} != "2" \ + && ${SELECTED_PHP} != "3" && ${SELECTED_PHP} != "4" \ + && ${SELECTED_PHP} != "5" && ${SELECTED_PHP} != "6" ]]; do + read -rp "Select an option [1-6]: " -i 5 -e SELECTED_PHP done echo "" - case $SELECTED_PHP in + case ${SELECTED_PHP} in 1) PHP_VER="5.6" install_php ${PHP_VER} @@ -529,7 +532,7 @@ function init_php_install() { # Menu Install PHP loader echo "" while [[ ${INSTALL_PHPLOADER} != "y" && ${INSTALL_PHPLOADER} != "n" ]]; do - read -p "Do you want to install PHP Loaders? [y/n]: " -e INSTALL_PHPLOADER + read -rp "Do you want to install PHP Loaders? [y/n]: " -e INSTALL_PHPLOADER done if [[ "${INSTALL_PHPLOADER}" == Y* || "${INSTALL_PHPLOADER}" == y* ]]; then @@ -542,7 +545,7 @@ function init_php_install() { while [[ ${SELECTED_PHPLOADER} != "1" && ${SELECTED_PHPLOADER} != "2" \ && ${SELECTED_PHPLOADER} != "3" ]]; do - read -p "Select an option [1-3]: " SELECTED_PHPLOADER + read -rp "Select an option [1-3]: " SELECTED_PHPLOADER done echo ""