bash - Unmounting all partitions on user-selected drive

409 views Asked by At

I wrote a BASH script aimed to install a Porteus ISO in a USB drive. First, the user can select the drive within a menu made in dialog. Second, the script unmount all partitions in the selected drive. Right here is my problem.

#!/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

[...] The rest of the code doesn't matter. Please note that the problem is right after the "typeset partition" and $partition doesn't seems to receive the right values.

0

There are 0 answers