Add a script for choosing a block device

This commit is contained in:
Manuel Thalmann 2023-04-01 01:48:43 +02:00
parent cc07c770c8
commit eabee350a9

53
scripts/Scripts/choose-disk.sh Executable file
View file

@ -0,0 +1,53 @@
#!/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;
}