PortValhalla/scripts/Common/Scripts/choose-disk.sh

60 lines
1.4 KiB
Bash
Executable file

#!/bin/bash
function chooseDisk() {
local -n result="$1";
local message="$2";
local choice;
local disk;
local -a disks;
local i;
disks=();
while read disk
do
local -a diskInfo;
diskInfo=($disk);
if [ "${diskInfo[2]}" == "TYPE" ] || [ "${diskInfo[2]}" == "disk" ]
then
disks+=("$disk");
fi;
done < <(lsblk -do NAME,SIZE,TYPE);
diskCount="$(expr "${#disks[@]}" - 1)";
padding="${#diskCount}";
if [ "$diskCount" -gt 0 ]
then
while true
do
echo "$message";
for i in $(seq 0 "$(expr "$diskCount")")
do
local index;
if [ "$i" -eq 0 ]
then
index="";
else
index="$i:";
fi;
printf "%$(expr "${diskCount}" + 1)s ${disks[$i]}" "$index";
echo "";
done;
read -p "Your choice: " choice;
if [ "$choice" -ge 1 ] && [ "$choice" -le "$diskCount" ]
then
disk=(${disks[$choice]});
result="/dev/${disk[0]}";
return;
else
>&2 echo "The specified choice \"$choice\" is invalid!";
fi;
done;
else
>&2 echo "No valid disk found!";
fi;
}