Skip to content

Instantly share code, notes, and snippets.

@MarcelFox
Last active January 17, 2018 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcelFox/0bb532b3cdb4e9729219cc4ecb054c71 to your computer and use it in GitHub Desktop.
Save MarcelFox/0bb532b3cdb4e9729219cc4ecb054c71 to your computer and use it in GitHub Desktop.
format_flash_disk.sh
#!/bin/bash
# It formats interactively your last_attached flash_disk.
#
# WARNING: It's under construction, this is the first working script
# There may be issues and improvements to be made. Any help will be appreciated.
#
# By MarcelFox
last_flash_disk=$(sudo dmesg -T | grep removable | tail -n1 | awk '{print $8"1"}' | sed 's/\[//g; s/\]//g')
detailed_flash_disk=$(sudo fdisk -l | grep $last_flash_disk)
fsck_warn="$(sudo dmesg | tail -n 10 | grep $last_flash_disk | grep fsck)"
its_mounted="$(mount | grep $last_flash_disk)"
mount_point="$(mount | grep $last_flash_disk | awk '{print $3}')"
if [[ -z $detailed_flash_disk ]]; then
printf "\nThere's no attached devices.\n"
else
printf "The last disk attached was:\n"
echo "$detailed_flash_disk"
if [[ ! -z $its_mounted ]]; then
printf "\nYour device is mounted at '$mount_point'."
printf "\nTo proceed, you must umount the device\n"
printf "Umount the device? (y/n): "
read check
if [ $check == "y" ] || [ $check == "Y" ]; then
sudo umount /dev/$last_flash_disk
elif [ $check == "n" ] || [ $check == "N" ]; then
echo "Ok!"
else
echo "Please inform only 'y' or 'n'"
fi
fi
if [[ ! -z $fsck_warn ]] && [[ ! -z $detailed_flash_disk ]]; then
printf "\nThe last fsck warn on the device, please check the date:\n"
printf "$fsck_warn\n"
printf "\nRun fsck on the device? (y/n): "
read check
if [ $check == "y" ] || [ $check == "Y" ]; then
sudo fsck /dev/$last_flash_disk
elif [ $check == "n" ] || [ $check == "N" ]; then
echo "Ok!"
else
echo "Please inform only 'y' or 'n'"
fi
fi
printf "\nDo you want to format your $last_flash_disk using fat32? (y/n): "
read check
if [ $check == "y" ] || [ $check == "Y" ]; then
printf "\nPlease inform the new label of your flash disk: ";
read new_disk_label
sudo mkfs.vfat -n $new_disk_label /dev/$last_flash_disk;
elif [ $check == "n" ] || [ $check == "N" ]; then
echo "Not formatting..."
else
echo "Please inform only 'y' or 'n'"
fi
fi
@MarcelFox
Copy link
Author

MarcelFox commented Jan 17, 2018

It uses sudo!

You can run directly with:
bash <(curl -ks https://gist.githubusercontent.com/MarcelFox/0bb532b3cdb4e9729219cc4ecb054c71/raw/bac89a44acd34e10a34cda68c95176aa9d6e2ee4/format_flash_disk.sh)

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