Skip to content

Instantly share code, notes, and snippets.

@qtaped
Last active June 17, 2024 14:18
Show Gist options
  • Save qtaped/c9156e903b5dc0dc76fcd64013d689e6 to your computer and use it in GitHub Desktop.
Save qtaped/c9156e903b5dc0dc76fcd64013d689e6 to your computer and use it in GitHub Desktop.
mountDisk simple script
#!/bin/bash
# SETTINGS
excludeDisks='mmcblk0..|zram.'
defaultDisk=$(lsblk -nro NAME,TYPE | grep -v -E ${excludeDisks} | grep 'part' | head -n 1 | awk '{print $1}')
echo " _ "
echo " _ __ ___ ___ _ _ _ __ | |_ "
echo " | _ _ \ / _ \| | | | _ \| __| "
echo " | | | | | | (_) | |_| | | | | |_ _ "
echo " |_| |_| |_|\___/ \__,_|_| |_|\__(_)"
echo
listParts() {
checkParts=$(lsblk -n | grep -v -wE ${excludeDisks});
if [ -z "$checkParts" ]; then
echo "Nothing to mount."
echo
exit 2
fi
lsblk -o NAME,SIZE,LABEL,MOUNTPOINT | grep -v -wE ${excludeDisks};
echo
}
listParts
read -p ":: Disk partition to mount? [default: ${defaultDisk}] " disk
test -z "${disk}" && disk=${defaultDisk}
echo
until lsblk -nro NAME,TYPE | grep -v -E ${excludeDisks} | grep -w 'part' | grep -qw "${disk}"; do
echo "ERROR: /dev/${disk} is not a valid partition."
echo
listParts
read -p ":: Enter a valid partition: " disk
echo
done
if lsblk -nro NAME,TYPE | grep -v -E ${excludeDisks} | grep -w 'part' | grep -qw "${disk}"; then
mntpoint=$(lsblk -o NAME,MOUNTPOINT | grep -w ${disk} | awk '{print $2}')
if [ -n "$mntpoint" ]; then
echo "/dev/${disk} already mount: $mntpoint"
read -p "→ Do you want to unmount it? [y/N] " -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
udisksctl unmount -b /dev/${disk}
exit 0
fi
else
read -p "→ Do you want to mount ${disk}? [Y/n] " -r
if [[ $REPLY =~ ^[Nn]$ ]]
then
echo "Canceled."
exit 2
fi
echo "Mounting partition /dev/${disk}..."
udisksctl mount -b /dev/${disk}
fi
echo
echo "To unmount disk, use this command:"
echo "udisksctl unmount -b /dev/${disk}"
echo
exit 0
fi
@qtaped
Copy link
Author

qtaped commented May 27, 2024

chmod +x mountDisk.sh
./mountDisk.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment