Skip to content

Instantly share code, notes, and snippets.

@wathika-eng
Created March 30, 2025 19:42
Show Gist options
  • Select an option

  • Save wathika-eng/1fc40521bf1559d12567a33feea5bbee to your computer and use it in GitHub Desktop.

Select an option

Save wathika-eng/1fc40521bf1559d12567a33feea5bbee to your computer and use it in GitHub Desktop.
#!/bin/bash
# Ensure script runs as root
if [[ $EUID -ne 0 ]]; then
echo "❌ This script must be run as root. Try: sudo $0"
exit 1
fi
# Enable tab completion for file paths
iso_path=""
usb_device=""
# Function for reading input with tab completion
read_with_completion() {
local prompt="$1"
local var_name="$2"
# Enable readline support for tab completion
echo -n "$prompt"
read -e -r "$var_name"
}
# List available block devices
echo "πŸ” Available storage devices:"
lsblk
echo ""
# Ask user for the ISO file path with tab completion
read_with_completion "πŸ“ Enter the full path to the ISO file: " iso_path
# Verify if ISO file exists
if [[ ! -f "$iso_path" ]]; then
echo "❌ ISO file not found! Exiting..."
exit 1
fi
# Ask user for the target USB device
read_with_completion "πŸ’Ύ Enter the target USB device (e.g., /dev/sdb): " usb_device
# Verify if the device exists
if [[ ! -b "$usb_device" ]]; then
echo "❌ Invalid USB device! Exiting..."
exit 1
fi
# Ask for confirmation before proceeding
echo ""
echo "⚠ WARNING: This will ERASE all data on $usb_device!"
read -rp "❗ Are you sure? (type 'yes' to continue): " confirm
if [[ "$confirm" != "yes" ]]; then
echo "🚫 Operation cancelled!"
exit 1
fi
# Unmount any mounted partitions on the USB
echo "πŸ”„ Unmounting $usb_device..."
sudo umount "${usb_device}"* 2>/dev/null
# Write the ISO to USB using dd
echo "⏳ Writing ISO to $usb_device..."
sudo dd if="$iso_path" of="$usb_device" bs=4M status=progress conv=fdatasync
# Ensure all data is written before ejecting
sync
echo "βœ… ISO successfully written to $usb_device!"
echo "πŸ’‘ You can now safely remove the USB drive."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment