From c9b8743d18eede3b68ea5a6470cb32656606f595 Mon Sep 17 00:00:00 2001 From: Zeev Diukman Date: Sun, 18 Jan 2026 08:49:54 +0200 Subject: [PATCH] feat: Enhance disk selection with default pre-selection and input validation, and add a mock loop device. --- test_z.sh | 1 + z.sh | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/test_z.sh b/test_z.sh index d8b0876..9c868c6 100644 --- a/test_z.sh +++ b/test_z.sh @@ -16,6 +16,7 @@ echo "Using mock directory: $MOCK_DIR" cat > "$MOCK_BIN/lsblk" <<'EOF' #!/bin/bash if [[ "$*" == *"-p -dno NAME,SIZE,MODEL"* ]]; then + echo "/dev/loop0 1G Mock_Loop" echo "/dev/vda 100G Mock_Disk" echo "/dev/vdb 50G Mock_Secondary" elif [[ "$*" == *"-p -nlo NAME,SIZE,TYPE /dev/vda"* ]]; then diff --git a/z.sh b/z.sh index 5565748..c5fd8ae 100755 --- a/z.sh +++ b/z.sh @@ -3,6 +3,7 @@ set -e # Configuration variables (will be populated via user selection or defaults if user press enter for each input) +selected_disk="/dev/vda" seed_device="/dev/vda1" sprout_device="/dev/vda2" efi_device="/dev/vda3" @@ -20,9 +21,19 @@ for i in "${!disks[@]}"; do done while true; do - read -r -p "Select a disk to choose partitions from (default 1): " REPLY - REPLY=${REPLY:-1} - if [[ "$REPLY" -gt 0 && "$REPLY" -le "${#disks[@]}" ]]; then + def_idx=1 + if [[ -n "$selected_disk" ]]; then + for i in "${!disks[@]}"; do + if [[ "${disks[i]}" == "$selected_disk"* ]]; then + def_idx=$((i+1)) + break + fi + done + fi + + read -r -p "Select a disk to choose partitions from (default $def_idx): " REPLY + REPLY=${REPLY:-$def_idx} + if [[ "$REPLY" =~ ^[0-9]+$ ]] && [[ "$REPLY" -gt 0 && "$REPLY" -le "${#disks[@]}" ]]; then disk_info="${disks[$((REPLY-1))]}" selected_disk=$(echo "$disk_info" | awk '{print $1}') break