我写了一个BASH脚本,目的是在USB驱动器中安装Porteus ISO。首先,用户可以在对话框中的菜单中选择驱动器。 其次,脚本卸载所选驱动器中的所有分区。这是我的问题。
#!/bin/bash
#SCRIPT PARA INSTALAR PORTEUS EN USB BOOTEABLE
if [ "$(id -u)" -ne 0 ]; then
echo "ERROR: Se debe ejecutar como ROOT"
exit 1
fi
# get device
device="$2"
if [ -z "$device" ]; then
echo "Adquiriendo dispositivos..."
devs="$(find /dev/disk/by-path | grep -- '-usb-' | grep -v -- '-part[0-9]*$' || true)"
if [ -z "$devs" ]; then
echo "ERROR: no se encontro ningun USB"
exit 2
fi
devs="$(readlink -f $devs)"
dialogdevs=""
dialogmodel=""
for dialogdev in $devs; do
dialogmodel="$(lsblk -ndo model "$dialogdev")"
dialogdevs="$dialogdevs $dialogdev '$dialogmodel' off"
done
unset dialogdev
unset dialogmodel
while [ -z "$device" ]; do
device="$(eval "dialog --stdout --radiolist 'Seleccionar usb' 12 40 5 $dialogdevs")"
if [ "$?" -ne "0" ]; then
exit
fi
done
echo ok $device
unset dialogdevs
unset devs
fi
typeset partition
mapfile -t devicePartitions < <(grep -oP "^\\K$device\\S*" /proc/mounts)
for partition in "${devicePartitions[@]}"; do
echo $partition
if ! umount "$partition" >/dev/null; then
echo "Failed to unmount $partition. It's likely that partition is busy."
exit
fi
done
[...] 其余代码无关紧要。 请注意,问题出在“类型分区”之后,并且$ partition似乎没有收到正确的值。