PortValhalla/scripts/Scripts/choose-disk.sh

53 lines
1.2 KiB
Bash
Executable file

#!/bin/bash
function chooseDisk() {
local -n result="$1" 2>&1;
local message="$2";
local choice;
local disk;
local -a disks;
local i;
disks=();
while read disk
do
disks+=("$disk");
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;
}