2023-03-31 23:48:43 +00:00
|
|
|
#!/bin/bash
|
|
|
|
function chooseDisk() {
|
2023-04-01 11:40:52 +00:00
|
|
|
local -n result="$1";
|
|
|
|
local message="$2";
|
2023-03-31 23:48:43 +00:00
|
|
|
local choice;
|
|
|
|
local disk;
|
|
|
|
local -a disks;
|
|
|
|
local i;
|
|
|
|
disks=();
|
|
|
|
|
|
|
|
while read disk
|
|
|
|
do
|
2023-04-01 00:49:48 +00:00
|
|
|
local -a diskInfo;
|
|
|
|
diskInfo=($disk);
|
|
|
|
|
|
|
|
if [ "${diskInfo[2]}" == "TYPE" ] || [ "${diskInfo[2]}" == "disk" ]
|
|
|
|
then
|
|
|
|
disks+=("$disk");
|
|
|
|
fi;
|
2023-03-31 23:48:43 +00:00
|
|
|
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]});
|
2023-04-01 11:40:52 +00:00
|
|
|
result="/dev/${disk[0]}";
|
2023-03-31 23:48:43 +00:00
|
|
|
return;
|
|
|
|
else
|
|
|
|
>&2 echo "The specified choice \"$choice\" is invalid!";
|
|
|
|
fi;
|
|
|
|
done;
|
|
|
|
else
|
|
|
|
>&2 echo "No valid disk found!";
|
|
|
|
fi;
|
|
|
|
}
|