Last active
January 17, 2018 21:59
-
-
Save MarcelFox/0bb532b3cdb4e9729219cc4ecb054c71 to your computer and use it in GitHub Desktop.
format_flash_disk.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It uses sudo!
You can run directly with:
bash <(curl -ks https://gist.githubusercontent.com/MarcelFox/0bb532b3cdb4e9729219cc4ecb054c71/raw/bac89a44acd34e10a34cda68c95176aa9d6e2ee4/format_flash_disk.sh)