From de14c072f7519cec7454b1c03b4f8921c88e6484 Mon Sep 17 00:00:00 2001 From: Tristan Ancelet Date: Sat, 18 Nov 2023 12:41:35 -0600 Subject: [PATCH] Added new helper functions to make script more interactive, and implemented them. --- bare-arch-install.sh | 106 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 99 insertions(+), 7 deletions(-) diff --git a/bare-arch-install.sh b/bare-arch-install.sh index b39b3a7..1d9e1c5 100755 --- a/bare-arch-install.sh +++ b/bare-arch-install.sh @@ -1,22 +1,114 @@ #!/usr/bin/bash +# BEGIN: Helper Functions + +get_choices () { + local PROMPT="$1" + shift + local -n OUTPUT_VAR="$2" + shift + local -a OPTIONS=( $@ ) + + select item in ${OPTIONS[@]} quit; do + case $item in + quit) + return 0 + ;; + ?) + continue + ;; + *) + if [[ "$item" ]]; then + OUTPUT_VAR+=( "$item" ) + fi + ;; + esac + done + +} +get_choice () { + local PROMPT="$1" + shift + local -n OUTPUT_VAR="$2" + shift + local -a OPTIONS=( $@ ) + + select item in ${OPTIONS[@]} quit; do + case $item in + quit) + return 0 + ;; + ?) + continue + ;; + + *) + if [[ "$item" ]]; then + OUTPUT_VAR="$item" + return 0 + fi + ;; + esac + done + +} + +get_yes_no () { + local PROMPT="${1:?"get_yes_no: Prompt was not provided"} " + local CHOICE_REGEX='(y|n|yes|no)' + local ANSWER + while true; do + read -p "$PROMPT" ANSWER + ANSWER="${ANSWER,,}" + + if [[ "$ANSWER" =~ ^$CHOICE_REGEX$ ]]; then + case $ANSWER in + y | yes) + return 0 + ;; + n | no) + return 1 + ;; + esac + else + echo "$ANSWER is not acceptible, please try again" + fi + done +} + +get_answer () { + local PROMPT="${1:?"get_answer: Prompt was not provided"}" + local -n OUTER_VAR="${2:?"get_answer: Return variable not provided"}" + local ANSWER + + while true; do + read -p "$PROMPT" ANSWER + + if get_yes_no "Are you sure $ANSWER is what you want? (y/n): "; then + OUTER_VAR="$ANSWER" + return + fi + done + +} + +# END: Helper Functions # BEGIN: Variables -HOSTNAME="ExampleHostname" -BOOT_METHOD="${1:?"Boot method was not provided. (BIOS, EFI)"}" -BOOT_METHOD="${BOOT_METHOD^^}" +HOSTNAME="" +get_answer "What is the hostname of this machine?" HOSTNAME -[[ ! "$BOOT_METHOD" =~ ^(EFI|BIOS)$ ]] && { - echo "Your boot method $BOOT_METHOD, is not acceptible. Please provide a vaild one" - exit -} +BOOT_METHOD="" +get_choice "What is your boot method?" BOOT_METHOD BIOS EFI +exit DISK="${2:?"Disk was not provided"}" [[ ! -b $DISK ]] && { echo "Your disk ($DISK) does not exist. Please provide a valid one" exit } + EFI_PARTITION=${DISK}1 SWAP_PARTITION=${DISK}2 ROOT_PARTITION=${DISK}3