Added new helper functions to make script more interactive, and implemented them.

This commit is contained in:
Tristan Ancelet 2023-11-18 12:41:35 -06:00
parent 2437e4a869
commit de14c072f7

View File

@ -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=""
get_choice "What is your boot method?" BOOT_METHOD BIOS EFI
[[ ! "$BOOT_METHOD" =~ ^(EFI|BIOS)$ ]] && {
echo "Your boot method $BOOT_METHOD, is not acceptible. Please provide a vaild one"
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