From 2824010b8636133e7c82253351b62e6c68ebadbb Mon Sep 17 00:00:00 2001 From: The-Plottwist Date: Sat, 15 Jan 2022 16:13:15 +0300 Subject: [PATCH 1/3] Improved Readability Added section headers and made some rearrangements to improve readability. Removed discouraged '$?' usage. Added install_program function. --- install.sh | 175 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 105 insertions(+), 70 deletions(-) diff --git a/install.sh b/install.sh index c7a9d5a..e7e09b5 100755 --- a/install.sh +++ b/install.sh @@ -1,7 +1,33 @@ #! /usr/bin/env bash -# Grub2 Themes -set -o errexit + +# Exit Immediately if a command fails +set -o errexit + + +# +# ──────────────────────────────────────────────────── I ────────── +# :::::: C O L O R S : : : : : : : : +# ────────────────────────────────────────────────────────────── +# + +CDEF=" \033[0m" # default color +CCIN=" \033[0;36m" # info color +CGSC=" \033[0;32m" # success color +CRER=" \033[0;31m" # error color +CWAR=" \033[0;33m" # waring color +b_CDEF=" \033[1;37m" # bold default color +b_CCIN=" \033[1;36m" # bold info color +b_CGSC=" \033[1;32m" # bold success color +b_CRER=" \033[1;31m" # bold error color +b_CWAR=" \033[1;33m" # bold warning color + + +# +# ────────────────────────────────────────────────────── II ────────── +# :::::: G L O B A L S : : : : : : : : +# ──────────────────────────────────────────────────────────────── +# [ GLOBAL::CONF ] { @@ -18,17 +44,12 @@ THEME_VARIANTS=('tela' 'vimix' 'stylish' 'whitesur') ICON_VARIANTS=('color' 'white' 'whitesur') SCREEN_VARIANTS=('1080p' '2k' '4k' 'ultrawide' 'ultrawide2k') -#COLORS -CDEF=" \033[0m" # default color -CCIN=" \033[0;36m" # info color -CGSC=" \033[0;32m" # success color -CRER=" \033[0;31m" # error color -CWAR=" \033[0;33m" # waring color -b_CDEF=" \033[1;37m" # bold default color -b_CCIN=" \033[1;36m" # bold info color -b_CGSC=" \033[1;32m" # bold success color -b_CRER=" \033[1;31m" # bold error color -b_CWAR=" \033[1;33m" # bold warning color + +# +# ────────────────────────────────────────────────────────── III ────────── +# :::::: F U N C T I O N S : : : : : : : : +# ──────────────────────────────────────────────────────────────────── +# # echo like ... with flag type and display message colors prompt () { @@ -49,7 +70,7 @@ prompt () { # Check command availability function has_command() { - command -v $1 > /dev/null + command -v $1 &> /dev/null #with "&>", all output will be redirected. } usage() { @@ -72,7 +93,7 @@ install() { if [[ "$UID" -eq "$ROOT_UID" ]]; then clear - # Create themes directory if it didn't exist + # Make a themes directory if it doesn't exist prompt -s "\n Checking for the existence of themes directory..." [[ -d "${THEME_DIR}/${theme}" ]] && rm -rf "${THEME_DIR}/${theme}" @@ -187,46 +208,45 @@ install() { updating_grub prompt -w "\n * At the next restart of your computer you will see your new Grub theme: '$theme' " + + #Check if password is cached (if cache timestamp has not expired yet) + elif sudo -n true 2> /dev/null && echo; then #No need for "$?" ==> https://github.com/koalaman/shellcheck/wiki/SC2181 + + sudo "$0" -t ${theme} -i ${icon} -s ${screen} else - #Check if password is cached (if cache timestamp not expired yet) - sudo -n true 2> /dev/null && echo - if [[ $? == 0 ]]; then - #No need to ask for password - sudo "$0" -t ${theme} -i ${icon} -s ${screen} + #Ask for password + if [[ -n ${tui_root_login} ]] ; then + + if [[ -n "${theme}" && -n "${screen}" ]]; then + + sudo -S $0 -t ${theme} -i ${icon} -s ${screen} <<< ${tui_root_login} + fi else - #Ask for password - if [[ -n ${tui_root_login} ]] ; then - if [[ -n "${theme}" && -n "${screen}" ]]; then - sudo -S $0 -t ${theme} -i ${icon} -s ${screen} <<< ${tui_root_login} - fi + + prompt -e "\n [ Error! ] -> Run me as root! " + read -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s + + if sudo -S echo <<< $REPLY 2> /dev/null && echo; then + + #Correct password, use with sudo's stdin + sudo -S "$0" -t ${theme} -i ${icon} -s ${screen} <<< ${REPLY} else - prompt -e "\n [ Error! ] -> Run me as root! " - read -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s - sudo -S echo <<< $REPLY 2> /dev/null && echo - - if [[ $? == 0 ]]; then - #Correct password, use with sudo's stdin - sudo -S "$0" -t ${theme} -i ${icon} -s ${screen} <<< ${REPLY} - else - #block for 3 seconds before allowing another attempt - sleep 3 - prompt -e "\n [ Error! ] -> Incorrect password!\n" - exit 1 - fi + #block for 3 seconds before allowing another attempt + sleep 3 + prompt -e "\n [ Error! ] -> Incorrect password!\n" + exit 1 fi fi - fi + fi } run_dialog() { if [[ -x /usr/bin/dialog ]]; then if [[ "$UID" -ne "$ROOT_UID" ]]; then #Check if password is cached (if cache timestamp not expired yet) - sudo -n true 2> /dev/null && echo - - if [[ $? == 0 ]]; then + if sudo -n true 2> /dev/null && echo; then #No need to ask for password sudo $0 else @@ -237,9 +257,7 @@ run_dialog() { --passwordbox "require root permission" 8 50 \ --output-fd 1 ) - sudo -S echo <<< $tui_root_login 2> /dev/null && echo - - if [[ $? == 0 ]]; then + if sudo -S echo <<< $tui_root_login 2> /dev/null && echo; then #Correct password, use with sudo's stdin sudo -S "$0" <<< $tui_root_login else @@ -324,20 +342,30 @@ updating_grub() { prompt -s "\n * All done!" } +function install_program () { + + if has_command zypper; then + + zypper in "$@" + elif has_command apt-get; then + + apt-get install "$@" + elif has_command dnf; then + + dnf install -y "$@" + elif has_command yum; then + + yum install "$@" + elif has_command pacman; then + + pacman -S --noconfirm "$@" + fi +} + install_dialog() { if [ ! "$(which dialog 2> /dev/null)" ]; then prompt -w "\n 'dialog' need to be installed for this shell" - if has_command zypper; then - sudo zypper in dialog - elif has_command apt-get; then - sudo apt-get install dialog - elif has_command dnf; then - sudo dnf install -y dialog - elif has_command yum; then - sudo yum install dialog - elif has_command pacman; then - sudo pacman -S --noconfirm dialog - fi + install_program "dialog" fi } @@ -374,9 +402,7 @@ remove() { else #Check if password is cached (if cache timestamp not expired yet) - sudo -n true 2> /dev/null && echo - - if [[ $? == 0 ]]; then + if sudo -n true 2> /dev/null && echo; then #No need to ask for password sudo "$0" "${PROG_ARGS[@]}" else @@ -384,9 +410,7 @@ remove() { prompt -e "\n [ Error! ] -> Run me as root! " read -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s - sudo -S echo <<< $REPLY 2> /dev/null && echo - - if [[ $? == 0 ]]; then + if sudo -S echo <<< $REPLY 2> /dev/null && echo; then #Correct password, use with sudo's stdin sudo -S "$0" "${PROG_ARGS[@]}" <<< $REPLY else @@ -404,9 +428,8 @@ dialog_installer() { if [[ ! -x /usr/bin/dialog ]]; then if [[ $UID -ne $ROOT_UID ]]; then #Check if password is cached (if cache timestamp not expired yet) - sudo -n true 2> /dev/null && echo - if [[ $? == 0 ]]; then + if sudo -n true 2> /dev/null && echo; then #No need to ask for password exec sudo $0 else @@ -414,9 +437,7 @@ dialog_installer() { prompt -e "\n [ Error! ] -> Run me as root! " read -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s - sudo -S echo <<< $REPLY 2> /dev/null && echo - - if [[ $? == 0 ]]; then + if sudo -S echo <<< $REPLY 2> /dev/null && echo; then #Correct password, use with sudo's stdin sudo $0 <<< $REPLY else @@ -433,6 +454,13 @@ dialog_installer() { install "${theme}" "${icon}" "${screen}" } + +# +# ────────────────────────────────────────────────────────────────────────── IV ────────── +# :::::: A R G U M E N T H A N D L I N G : : : : : : : : +# ──────────────────────────────────────────────────────────────────────────────────── +# + while [[ $# -gt 0 ]]; do PROG_ARGS+=("${1}") dialog='false' @@ -465,7 +493,7 @@ while [[ $# -gt 0 ]]; do themes+=("${THEME_VARIANTS[3]}") shift ;; - -*|--*) + -*) # "-*" overrides "--*" break ;; *) @@ -492,7 +520,7 @@ while [[ $# -gt 0 ]]; do icons+=("${ICON_VARIANTS[2]}") shift ;; - -*|--*) + -*) break ;; *) @@ -527,7 +555,7 @@ while [[ $# -gt 0 ]]; do screens+=("${SCREEN_VARIANTS[4]}") shift ;; - -*|--*) + -*) break ;; *) @@ -550,6 +578,13 @@ while [[ $# -gt 0 ]]; do esac done + +# +# ──────────────────────────────────────────────── V ────────── +# :::::: M A I N : : : : : : : : +# ────────────────────────────────────────────────────────── +# + # Show terminal user interface for better use if [[ "${dialog:-}" == 'false' ]]; then if [[ "${remove:-}" != 'true' ]]; then From d27e1caf5f82e7ff9e81e9396dcf787c56f7d338 Mon Sep 17 00:00:00 2001 From: The-Plottwist Date: Sat, 15 Jan 2022 20:55:37 +0300 Subject: [PATCH 2/3] Little fixes Almost always ```read``` command should be used with ```-r``` option. Added some clarity about globals. --- install.sh | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/install.sh b/install.sh index e7e09b5..6e4a684 100755 --- a/install.sh +++ b/install.sh @@ -29,8 +29,6 @@ b_CWAR=" \033[1;33m" # bold warning color # ──────────────────────────────────────────────────────────────── # -[ GLOBAL::CONF ] -{ readonly ROOT_UID=0 readonly Project_Name="GRUB2::THEMES" readonly MAX_DELAY=20 # max delay for user to enter root password @@ -38,7 +36,6 @@ tui_root_login= THEME_DIR="/usr/share/grub/themes" REO_DIR="$(cd $(dirname $0) && pwd)" -} THEME_VARIANTS=('tela' 'vimix' 'stylish' 'whitesur') ICON_VARIANTS=('color' 'white' 'whitesur') @@ -225,7 +222,7 @@ install() { else prompt -e "\n [ Error! ] -> Run me as root! " - read -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s + read -r -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s if sudo -S echo <<< $REPLY 2> /dev/null && echo; then @@ -408,7 +405,7 @@ remove() { else #Ask for password prompt -e "\n [ Error! ] -> Run me as root! " - read -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s + read -r -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s #when using "read" command, "-r" option must be supplied ==> https://github.com/koalaman/shellcheck/wiki/SC2162 if sudo -S echo <<< $REPLY 2> /dev/null && echo; then #Correct password, use with sudo's stdin @@ -435,7 +432,7 @@ dialog_installer() { else #Ask for password prompt -e "\n [ Error! ] -> Run me as root! " - read -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s + read -r -p " [ Trusted ] Specify the root password : " -t ${MAX_DELAY} -s if sudo -S echo <<< $REPLY 2> /dev/null && echo; then #Correct password, use with sudo's stdin From 9d244c9d07c5a88d431fa6fe4847f088c861c7a8 Mon Sep 17 00:00:00 2001 From: The-Plottwist Date: Sat, 15 Jan 2022 22:31:18 +0300 Subject: [PATCH 3/3] Fixed issue #149 In the old functionality, the current config file was being replaced with a backup file (if it had presented). However, we now back-up the config file and replace the active theme line instead. --- install.sh | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/install.sh b/install.sh index 6e4a684..d1ef50d 100755 --- a/install.sh +++ b/install.sh @@ -371,32 +371,47 @@ remove() { # Check for root access and proceed if it is present if [ "$UID" -eq "$ROOT_UID" ]; then + echo -e "\n Checking for the existence of themes directory..." if [[ -d "${THEME_DIR}/${theme}" ]]; then rm -rf "${THEME_DIR}/${theme}" else - prompt -e "\n ${theme} grub theme not exist!" + prompt -e "\n Specified ${theme} theme does not exist!" exit 0 fi - # Backup grub config - if [[ -f "/etc/default/grub.bak" ]]; then - rm -rf /etc/default/grub && mv /etc/default/grub.bak /etc/default/grub + local grub_config_location="" + if [[ -f "/etc/default/grub" ]]; then + + grub_config_location="/etc/default/grub" + elif [[ -f "/etc/default/grub.d/kali-themes.cfg" ]]; then + + grub_config_location="/etc/default/grub.d/kali-themes.cfg" else - prompt -e "\n grub.bak not exist!" - exit 0 + + prompt -e "\nCannot find grub config file in default locations!" + prompt -e "\nPlease inform the developers by opening an issue on github." + prompt -e "\nExiting..." + exit 1 fi - # For Kali linux - if [[ -f "/etc/default/grub.d/kali-themes.cfg.bak" ]]; then - rm -rf /etc/default/grub.d/kali-themes.cfg && mv /etc/default/grub.d/kali-themes.cfg.bak /etc/default/grub.d/kali-themes.cfg + local current_theme="" # Declaration and assignment should be done seperately ==> https://github.com/koalaman/shellcheck/wiki/SC2155 + current_theme="$(grep 'GRUB_THEME=' $grub_config_location | grep -v \#)" + if [[ -n "$current_theme" ]]; then + + # Backup with --in-place option to grub.bak within the same directory; then remove the current theme. + sed --in-place='.bak' "s|$current_theme|#GRUB_THEME=|" "$grub_config_location" + + # Update grub config + prompt -s "\n Resetting grub theme...\n" + updating_grub + else + + prompt -e "\nNo active theme found." + prompt -e "\nExiting..." + exit 1 fi - # Update grub config - prompt -s "\n Resetting grub theme...\n" - - updating_grub - else #Check if password is cached (if cache timestamp not expired yet) if sudo -n true 2> /dev/null && echo; then