shorten one-command if/else statements

This commit is contained in:
i-c-u-p 2022-06-30 15:46:11 -05:00
parent 84767c0907
commit 7029daddb5
No known key found for this signature in database
GPG Key ID: ABB80C12170FC0D1
6 changed files with 29 additions and 62 deletions

View File

@ -26,9 +26,7 @@ echo "
source $CONFIGS_DIR/setup.conf source $CONFIGS_DIR/setup.conf
( bash $SCRIPT_DIR/scripts/0-preinstall.sh )|& tee 0-preinstall.log ( bash $SCRIPT_DIR/scripts/0-preinstall.sh )|& tee 0-preinstall.log
( arch-chroot /mnt $HOME/ArchTitus/scripts/1-setup.sh )|& tee 1-setup.log ( arch-chroot /mnt $HOME/ArchTitus/scripts/1-setup.sh )|& tee 1-setup.log
if [[ ! $DESKTOP_ENV == server ]]; then [[ $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 /usr/bin/runuser -u $USERNAME -- /home/$USERNAME/ArchTitus/scripts/2-user.sh )|& tee 2-user.log
fi
( arch-chroot /mnt $HOME/ArchTitus/scripts/3-post-setup.sh )|& tee 3-post-setup.log ( arch-chroot /mnt $HOME/ArchTitus/scripts/3-post-setup.sh )|& tee 3-post-setup.log
cp -v *.log /mnt/home/$USERNAME cp -v *.log /mnt/home/$USERNAME

View File

@ -160,11 +160,7 @@ echo "
------------------------------------------------------------------------- -------------------------------------------------------------------------
GRUB BIOS Bootloader Install & Check GRUB BIOS Bootloader Install & Check
-------------------------------------------------------------------------" -------------------------------------------------------------------------"
if [[ ! -d "/sys/firmware/efi" ]]; then [[ -d "/sys/firmware/efi" ]] || grub-install --boot-directory=/mnt/boot ${DISK} && pacstrap /mnt efibootmgr --noconfirm --needed
grub-install --boot-directory=/mnt/boot ${DISK}
else
pacstrap /mnt efibootmgr --noconfirm --needed
fi
echo " echo "
------------------------------------------------------------------------- -------------------------------------------------------------------------
Checking for low memory systems <8G Checking for low memory systems <8G

View File

@ -73,10 +73,7 @@ echo "
# stop the script and move on, not installing any more packages below that line # stop the script and move on, not installing any more packages below that line
if [[ ! $DESKTOP_ENV == server ]]; then if [[ ! $DESKTOP_ENV == server ]]; then
sed -n '/'$INSTALL_TYPE'/q;p' $HOME/ArchTitus/pkg-files/pacman-pkgs.txt | while read line; do sed -n '/'$INSTALL_TYPE'/q;p' $HOME/ArchTitus/pkg-files/pacman-pkgs.txt | while read line; do
if [[ ${line} == '--END OF MINIMAL INSTALL--' ]]; then [[ ${line} == '--END OF MINIMAL INSTALL--' ]] && continue # If selected installation type is FULL, skip the --END OF THE MINIMAL INSTALLATION-- line
# If selected installation type is FULL, skip the --END OF THE MINIMAL INSTALLATION-- line
continue
fi
echo "INSTALLING: ${line}" echo "INSTALLING: ${line}"
sudo pacman -S --noconfirm --needed ${line} sudo pacman -S --noconfirm --needed ${line}
done done
@ -118,9 +115,7 @@ if ! source $HOME/ArchTitus/configs/setup.conf; then
read -p "Please enter username:" username 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 # 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 # lowercase the username to test regex
if [[ "${username,,}" =~ ^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\$)$ ]]; then [[ "${username,,}" =~ ^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\$)$ ]] && break
break
fi
echo "Incorrect username." echo "Incorrect username."
done done
# convert name to lowercase before saving to setup.conf # convert name to lowercase before saving to setup.conf
@ -134,14 +129,10 @@ echo "password=${password,,}" >> ${HOME}/ArchTitus/configs/setup.conf
while true; do while true; do
read -p "Please name your machine:" name_of_machine read -p "Please name your machine:" name_of_machine
# hostname regex (!!couldn't find spec for computer name!!) # hostname regex (!!couldn't find spec for computer name!!)
if [[ "${name_of_machine,,}" =~ ^[a-z][a-z0-9_.-]{0,62}[a-z0-9]$ ]]; then [[ "${name_of_machine,,}" =~ ^[a-z][a-z0-9_.-]{0,62}[a-z0-9]$ ]] && break
break
fi
# if validation fails allow the user to force saving of the hostname # 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 read -p "Hostname doesn't seem correct. Do you still want to save it? (y/n)" force
if [[ "${force,,}" = "y" ]]; then [[ "${force,,}" = "y" ]] && break
break
fi
done done
echo "NAME_OF_MACHINE=${name_of_machine,,}" >> ${HOME}/ArchTitus/configs/setup.conf echo "NAME_OF_MACHINE=${name_of_machine,,}" >> ${HOME}/ArchTitus/configs/setup.conf

View File

@ -27,10 +27,7 @@ source $HOME/ArchTitus/configs/setup.conf
ln -s "~/zsh/.zshrc" ~/.zshrc ln -s "~/zsh/.zshrc" ~/.zshrc
sed -n '/'$INSTALL_TYPE'/q;p' ~/ArchTitus/pkg-files/${DESKTOP_ENV}.txt | while read line; do sed -n '/'$INSTALL_TYPE'/q;p' ~/ArchTitus/pkg-files/${DESKTOP_ENV}.txt | while read line; do
if [[ ${line} == '--END OF MINIMAL INSTALL--' ]]; then [[ ${line} == '--END OF MINIMAL INSTALL--' ]] && continue # If selected installation type is FULL, skip the --END OF THE MINIMAL INSTALLATION-- line
# If selected installation type is FULL, skip the --END OF THE MINIMAL INSTALLATION-- line
continue
fi
echo "INSTALLING: ${line}" echo "INSTALLING: ${line}"
sudo pacman -S --noconfirm --needed ${line} sudo pacman -S --noconfirm --needed ${line}
done 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 # 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 # 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 sed -n '/'$INSTALL_TYPE'/q;p' ~/ArchTitus/pkg-files/aur-pkgs.txt | while read line; do
if [[ ${line} == '--END OF MINIMAL INSTALL--' ]]; then [[ ${line} == '--END OF MINIMAL INSTALL--' ]] && continue # If selected installation type is FULL, skip the --END OF THE MINIMAL INSTALLATION-- line
# If selected installation type is FULL, skip the --END OF THE MINIMAL INSTALLATION-- line
continue
fi
echo "INSTALLING: ${line}" echo "INSTALLING: ${line}"
$AUR_HELPER -S --noconfirm --needed ${line} $AUR_HELPER -S --noconfirm --needed ${line}
done done

View File

@ -20,18 +20,14 @@ Final Setup and Configurations
GRUB EFI Bootloader Install & Check" GRUB EFI Bootloader Install & Check"
source ${HOME}/ArchTitus/configs/setup.conf source ${HOME}/ArchTitus/configs/setup.conf
if [[ -d "/sys/firmware/efi" ]]; then [[ -d "/sys/firmware/efi" ]] && grub-install --efi-directory=/boot ${DISK}
grub-install --efi-directory=/boot ${DISK}
fi
echo " echo "
------------------------------------------------------------------------- -------------------------------------------------------------------------
Creating (and Theming) Grub Boot Menu Creating (and Theming) Grub Boot Menu
-------------------------------------------------------------------------" -------------------------------------------------------------------------"
# set kernel parameter for decrypting the drive # set kernel parameter for decrypting the drive
if [[ "${FS}" == "luks" ]]; then [[ "${FS}" == "luks" ]] && sed -i "s%GRUB_CMDLINE_LINUX_DEFAULT=\"%&cryptdevice=UUID=${ENCRYPTED_PARTITION_UUID}:ROOT root=/dev/mapper/ROOT %g" /etc/default/grub
sed -i "s%GRUB_CMDLINE_LINUX_DEFAULT=\"%&cryptdevice=UUID=${ENCRYPTED_PARTITION_UUID}:ROOT root=/dev/mapper/ROOT %g" /etc/default/grub
fi
# set kernel parameter for adding splash screen # set kernel parameter for adding splash screen
sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="[^"]*/& splash /' /etc/default/grub sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="[^"]*/& splash /' /etc/default/grub

View File

@ -15,17 +15,13 @@
# @setting-header General Settings # @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. # @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 CONFIG_FILE=$CONFIGS_DIR/setup.conf
if [ ! -f $CONFIG_FILE ]; then # check if file exists [ -f $CONFIG_FILE ] || touch -f $CONFIG_FILE # create $CONFIG_FILE if it doesn't exist
touch -f $CONFIG_FILE # create file if not exists
fi
# @description set options in setup.conf # @description set options in setup.conf
# @arg $1 string Configuration variable. # @arg $1 string Configuration variable.
# @arg $2 string Configuration value. # @arg $2 string Configuration value.
set_option() { set_option() {
if grep -Eq "^${1}.*" $CONFIG_FILE; then # check if option exists grep -Eq "^${1}.*" $CONFIG_FILE && sed -i "/^${1}.*/d" $CONFIG_FILE # delete option if exists
sed -i "/^${1}.*/d" $CONFIG_FILE # delete option if exists
fi
echo "${1}=${2}" >>$CONFIG_FILE # add option echo "${1}=${2}" >>$CONFIG_FILE # add option
} }
# @description Renders a text based list of options that can be selected by the # @description Renders a text based list of options that can be selected by the
@ -48,20 +44,20 @@ select_option() {
key_input() { key_input() {
local key local key
IFS= read -rsn1 key 2>/dev/null >&2 IFS= read -rsn1 key 2>/dev/null >&2
if [[ $key = "" ]]; then echo enter; fi; [[ $key = "" ]] && echo enter
if [[ $key = $'\x20' ]]; then echo space; fi; [[ $key = $'\x20' ]] && echo space
if [[ $key = "k" ]]; then echo up; fi; [[ $key = "k" ]] && echo up
if [[ $key = "j" ]]; then echo down; fi; [[ $key = "j" ]] && echo down
if [[ $key = "h" ]]; then echo left; fi; [[ $key = "h" ]] && echo left
if [[ $key = "l" ]]; then echo right; fi; [[ $key = "l" ]] && echo right
if [[ $key = "a" ]]; then echo all; fi; [[ $key = "a" ]] && echo all
if [[ $key = "n" ]]; then echo none; fi; [[ $key = "n" ]] && echo none
if [[ $key = $'\x1b' ]]; then if [[ $key = $'\x1b' ]]; then
read -rsn2 key read -rsn2 key
if [[ $key = [A || $key = k ]]; then echo up; fi; [[ $key = [A || $key = k ]] && echo up
if [[ $key = [B || $key = j ]]; then echo down; fi; [[ $key = [B || $key = j ]] && echo down
if [[ $key = [C || $key = l ]]; then echo right; fi; [[ $key = [C || $key = l ]] && echo right
if [[ $key = [D || $key = h ]]; then echo left; fi; [[ $key = [D || $key = h ]] && echo left
fi fi
} }
print_options_multicol() { print_options_multicol() {
@ -82,11 +78,7 @@ select_option() {
col=$(( $idx - $row * $colmax )) col=$(( $idx - $row * $colmax ))
cursor_to $(( $startrow + $row + 1)) $(( $offset * $col + 1)) cursor_to $(( $startrow + $row + 1)) $(( $offset * $col + 1))
if [ $idx -eq $curr_idx ]; then [ $idx -eq $curr_idx ] && print_selected "$option" || print_option "$option"
print_selected "$option"
else
print_option "$option"
fi
((idx++)) ((idx++))
done done
} }
@ -120,13 +112,13 @@ select_option() {
case `key_input` in case `key_input` in
enter) break;; enter) break;;
up) ((active_row--)); up) ((active_row--));
if [ $active_row -lt 0 ]; then active_row=0; fi;; [ $active_row -lt 0 ] && active_row=0;;
down) ((active_row++)); 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)); 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)); 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 esac
done done