From 7029daddb538430acd059bc269029e2f5dfc8fe9 Mon Sep 17 00:00:00 2001 From: i-c-u-p <96894903+i-c-u-p@users.noreply.github.com> Date: Thu, 30 Jun 2022 15:46:11 -0500 Subject: [PATCH] shorten one-command if/else statements --- archtitus.sh | 4 +--- scripts/0-preinstall.sh | 6 +----- scripts/1-setup.sh | 17 ++++----------- scripts/2-user.sh | 10 ++------- scripts/3-post-setup.sh | 8 ++----- scripts/startup.sh | 46 +++++++++++++++++------------------------ 6 files changed, 29 insertions(+), 62 deletions(-) diff --git a/archtitus.sh b/archtitus.sh index 7f1e0dc..1d5711e 100755 --- a/archtitus.sh +++ b/archtitus.sh @@ -26,9 +26,7 @@ echo " source $CONFIGS_DIR/setup.conf ( bash $SCRIPT_DIR/scripts/0-preinstall.sh )|& tee 0-preinstall.log ( arch-chroot /mnt $HOME/ArchTitus/scripts/1-setup.sh )|& tee 1-setup.log - if [[ ! $DESKTOP_ENV == server ]]; then - ( arch-chroot /mnt /usr/bin/runuser -u $USERNAME -- /home/$USERNAME/ArchTitus/scripts/2-user.sh )|& tee 2-user.log - fi + [[ $DESKTOP_ENV == server ]] || ( arch-chroot /mnt /usr/bin/runuser -u $USERNAME -- /home/$USERNAME/ArchTitus/scripts/2-user.sh )|& tee 2-user.log ( arch-chroot /mnt $HOME/ArchTitus/scripts/3-post-setup.sh )|& tee 3-post-setup.log cp -v *.log /mnt/home/$USERNAME diff --git a/scripts/0-preinstall.sh b/scripts/0-preinstall.sh index 1f42c59..571ab99 100755 --- a/scripts/0-preinstall.sh +++ b/scripts/0-preinstall.sh @@ -160,11 +160,7 @@ echo " ------------------------------------------------------------------------- GRUB BIOS Bootloader Install & Check -------------------------------------------------------------------------" -if [[ ! -d "/sys/firmware/efi" ]]; then - grub-install --boot-directory=/mnt/boot ${DISK} -else - pacstrap /mnt efibootmgr --noconfirm --needed -fi +[[ -d "/sys/firmware/efi" ]] || grub-install --boot-directory=/mnt/boot ${DISK} && pacstrap /mnt efibootmgr --noconfirm --needed echo " ------------------------------------------------------------------------- Checking for low memory systems <8G diff --git a/scripts/1-setup.sh b/scripts/1-setup.sh index 641ca38..a3a9fc0 100755 --- a/scripts/1-setup.sh +++ b/scripts/1-setup.sh @@ -73,10 +73,7 @@ echo " # stop the script and move on, not installing any more packages below that line if [[ ! $DESKTOP_ENV == server ]]; then sed -n '/'$INSTALL_TYPE'/q;p' $HOME/ArchTitus/pkg-files/pacman-pkgs.txt | while read line; do - if [[ ${line} == '--END OF MINIMAL INSTALL--' ]]; then - # If selected installation type is FULL, skip the --END OF THE MINIMAL INSTALLATION-- line - continue - fi + [[ ${line} == '--END OF MINIMAL INSTALL--' ]] && continue # If selected installation type is FULL, skip the --END OF THE MINIMAL INSTALLATION-- line echo "INSTALLING: ${line}" sudo pacman -S --noconfirm --needed ${line} done @@ -118,9 +115,7 @@ if ! source $HOME/ArchTitus/configs/setup.conf; then read -p "Please enter username:" username # username regex per response here https://unix.stackexchange.com/questions/157426/what-is-the-regex-to-validate-linux-users # lowercase the username to test regex - if [[ "${username,,}" =~ ^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\$)$ ]]; then - break - fi + [[ "${username,,}" =~ ^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\$)$ ]] && break echo "Incorrect username." done # convert name to lowercase before saving to setup.conf @@ -134,14 +129,10 @@ echo "password=${password,,}" >> ${HOME}/ArchTitus/configs/setup.conf while true; do read -p "Please name your machine:" name_of_machine # hostname regex (!!couldn't find spec for computer name!!) - if [[ "${name_of_machine,,}" =~ ^[a-z][a-z0-9_.-]{0,62}[a-z0-9]$ ]]; then - break - fi + [[ "${name_of_machine,,}" =~ ^[a-z][a-z0-9_.-]{0,62}[a-z0-9]$ ]] && break # if validation fails allow the user to force saving of the hostname read -p "Hostname doesn't seem correct. Do you still want to save it? (y/n)" force - if [[ "${force,,}" = "y" ]]; then - break - fi + [[ "${force,,}" = "y" ]] && break done echo "NAME_OF_MACHINE=${name_of_machine,,}" >> ${HOME}/ArchTitus/configs/setup.conf diff --git a/scripts/2-user.sh b/scripts/2-user.sh index c5d5d93..9e57d3e 100755 --- a/scripts/2-user.sh +++ b/scripts/2-user.sh @@ -27,10 +27,7 @@ source $HOME/ArchTitus/configs/setup.conf ln -s "~/zsh/.zshrc" ~/.zshrc sed -n '/'$INSTALL_TYPE'/q;p' ~/ArchTitus/pkg-files/${DESKTOP_ENV}.txt | while read line; do - if [[ ${line} == '--END OF MINIMAL INSTALL--' ]]; then - # If selected installation type is FULL, skip the --END OF THE MINIMAL INSTALLATION-- line - continue - fi + [[ ${line} == '--END OF MINIMAL INSTALL--' ]] && continue # If selected installation type is FULL, skip the --END OF THE MINIMAL INSTALLATION-- line echo "INSTALLING: ${line}" sudo pacman -S --noconfirm --needed ${line} done @@ -44,10 +41,7 @@ if [[ ! $AUR_HELPER == none ]]; then # sed $INSTALL_TYPE is using install type to check for MINIMAL installation, if it's true, stop # stop the script and move on, not installing any more packages below that line sed -n '/'$INSTALL_TYPE'/q;p' ~/ArchTitus/pkg-files/aur-pkgs.txt | while read line; do - if [[ ${line} == '--END OF MINIMAL INSTALL--' ]]; then - # If selected installation type is FULL, skip the --END OF THE MINIMAL INSTALLATION-- line - continue - fi + [[ ${line} == '--END OF MINIMAL INSTALL--' ]] && continue # If selected installation type is FULL, skip the --END OF THE MINIMAL INSTALLATION-- line echo "INSTALLING: ${line}" $AUR_HELPER -S --noconfirm --needed ${line} done diff --git a/scripts/3-post-setup.sh b/scripts/3-post-setup.sh index 6703548..2f1ee4d 100755 --- a/scripts/3-post-setup.sh +++ b/scripts/3-post-setup.sh @@ -20,18 +20,14 @@ Final Setup and Configurations GRUB EFI Bootloader Install & Check" source ${HOME}/ArchTitus/configs/setup.conf -if [[ -d "/sys/firmware/efi" ]]; then - grub-install --efi-directory=/boot ${DISK} -fi +[[ -d "/sys/firmware/efi" ]] && grub-install --efi-directory=/boot ${DISK} echo " ------------------------------------------------------------------------- Creating (and Theming) Grub Boot Menu -------------------------------------------------------------------------" # set kernel parameter for decrypting the drive -if [[ "${FS}" == "luks" ]]; then -sed -i "s%GRUB_CMDLINE_LINUX_DEFAULT=\"%&cryptdevice=UUID=${ENCRYPTED_PARTITION_UUID}:ROOT root=/dev/mapper/ROOT %g" /etc/default/grub -fi +[[ "${FS}" == "luks" ]] && sed -i "s%GRUB_CMDLINE_LINUX_DEFAULT=\"%&cryptdevice=UUID=${ENCRYPTED_PARTITION_UUID}:ROOT root=/dev/mapper/ROOT %g" /etc/default/grub # set kernel parameter for adding splash screen sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="[^"]*/& splash /' /etc/default/grub diff --git a/scripts/startup.sh b/scripts/startup.sh index ecea1bf..6d96ea5 100755 --- a/scripts/startup.sh +++ b/scripts/startup.sh @@ -15,17 +15,13 @@ # @setting-header General Settings # @setting CONFIG_FILE string[$CONFIGS_DIR/setup.conf] Location of setup.conf to be used by set_option and all subsequent scripts. CONFIG_FILE=$CONFIGS_DIR/setup.conf -if [ ! -f $CONFIG_FILE ]; then # check if file exists - touch -f $CONFIG_FILE # create file if not exists -fi +[ -f $CONFIG_FILE ] || touch -f $CONFIG_FILE # create $CONFIG_FILE if it doesn't exist # @description set options in setup.conf # @arg $1 string Configuration variable. # @arg $2 string Configuration value. set_option() { - if grep -Eq "^${1}.*" $CONFIG_FILE; then # check if option exists - sed -i "/^${1}.*/d" $CONFIG_FILE # delete option if exists - fi + grep -Eq "^${1}.*" $CONFIG_FILE && sed -i "/^${1}.*/d" $CONFIG_FILE # delete option if exists echo "${1}=${2}" >>$CONFIG_FILE # add option } # @description Renders a text based list of options that can be selected by the @@ -48,20 +44,20 @@ select_option() { key_input() { local key IFS= read -rsn1 key 2>/dev/null >&2 - if [[ $key = "" ]]; then echo enter; fi; - if [[ $key = $'\x20' ]]; then echo space; fi; - if [[ $key = "k" ]]; then echo up; fi; - if [[ $key = "j" ]]; then echo down; fi; - if [[ $key = "h" ]]; then echo left; fi; - if [[ $key = "l" ]]; then echo right; fi; - if [[ $key = "a" ]]; then echo all; fi; - if [[ $key = "n" ]]; then echo none; fi; + [[ $key = "" ]] && echo enter + [[ $key = $'\x20' ]] && echo space + [[ $key = "k" ]] && echo up + [[ $key = "j" ]] && echo down + [[ $key = "h" ]] && echo left + [[ $key = "l" ]] && echo right + [[ $key = "a" ]] && echo all + [[ $key = "n" ]] && echo none if [[ $key = $'\x1b' ]]; then read -rsn2 key - if [[ $key = [A || $key = k ]]; then echo up; fi; - if [[ $key = [B || $key = j ]]; then echo down; fi; - if [[ $key = [C || $key = l ]]; then echo right; fi; - if [[ $key = [D || $key = h ]]; then echo left; fi; + [[ $key = [A || $key = k ]] && echo up + [[ $key = [B || $key = j ]] && echo down + [[ $key = [C || $key = l ]] && echo right + [[ $key = [D || $key = h ]] && echo left fi } print_options_multicol() { @@ -82,11 +78,7 @@ select_option() { col=$(( $idx - $row * $colmax )) cursor_to $(( $startrow + $row + 1)) $(( $offset * $col + 1)) - if [ $idx -eq $curr_idx ]; then - print_selected "$option" - else - print_option "$option" - fi + [ $idx -eq $curr_idx ] && print_selected "$option" || print_option "$option" ((idx++)) done } @@ -120,13 +112,13 @@ select_option() { case `key_input` in enter) break;; up) ((active_row--)); - if [ $active_row -lt 0 ]; then active_row=0; fi;; + [ $active_row -lt 0 ] && active_row=0;; down) ((active_row++)); - if [ $active_row -ge $(( ${#options[@]} / $colmax )) ]; then active_row=$(( ${#options[@]} / $colmax )); fi;; + [ $active_row -ge $(( ${#options[@]} / $colmax )) ] && active_row=$(( ${#options[@]} / $colmax ));; left) ((active_col=$active_col - 1)); - if [ $active_col -lt 0 ]; then active_col=0; fi;; + [ $active_col -lt 0 ] && active_col=0;; right) ((active_col=$active_col + 1)); - if [ $active_col -ge $colmax ]; then active_col=$(( $colmax - 1 )) ; fi;; + [ $active_col -ge $colmax ] && active_col=$(( $colmax - 1 )) esac done