Witam. Mam problem z funkcją read_choice
. Funkcja ma prosić użytkownika o podanie liczby odpowiadającej elementowi dostępnemu w liście opcji do wyboru. Problem polega na tym, że zmienna answer
zamiast mieć przypisaną liczbę otrzymuje pustą wartość.
read_num() {
local question=$1
local -n answer=$2
local default=${3:-}
if [[ $default ]]; then
question="$question (leave blank for $default): "
else
question="$question: "
fi
local re='^[0-9]+$'
until
read -rp "$question" answer
[[ "$answer" =~ $re ]]
do
printf "%s: not a number.\n" "$answer" >&2
done
}
read_choice() {
local question=$1
local -n answer=$2
local choices=( "$@" )
choices=( "${choices[@]:1}" )
local user_choice
while true; do
printf "%s\n" "$question"
for (( i = 0; i < ${#choices[@]}; ++i )); do
printf "%d) %s\n" "$i" "${choices[$i]}"
done
read_num "Your choice" user_choice
if [ "$user_choice" -ge 0 ] && [ "$user_choice" -lt ${#choices[@]} ]; then
answer=$user_choice
break
else
printf "Invalid choice!\n\n"
fi
done
}
user_choice=""
choices=( "Single data HDD" "ZFS on root" "ZFS on root + RAID 1" )
read_choice "Select desired setup" user_choice "${choices[@]}"
root_dev=""
case $user_choice in
"Single data HDD")
storage_dev=""
read_device "Enter storage device path" storage_dev
create_data_pool "$storage_dev"
;;
"ZFS on root")
read_device "Enter root device path" root_dev
create_partitions "$root_dev"
create_rootfs "$root_dev"
;;
"ZFS on root + RAID 1")
read_devices "Enter target devices separated by spaces" root_dev
create_partitions "${root_dev[@]}"
create_rootfs "${root_dev[@]}"
;;
*)
die "invalid choice '$user_choice'"
esac