Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Burn the Raspbian OS image to an SD card.
#!/bin/bash -e
# Goal:
# Burn the Raspbian OS image to an SD card.
# Prerequisites:
# 1. Download image from
# 2. Unzip the image.
# 3. Set the following variables:
echo "Are we superuser?"
[[ $EUID -eq 0 ]]
echo "OK"
echo "Do we have image $RPI_IMAGE?"
file "$RPI_IMAGE" | grep --quiet "DOS/MBR boot sector"
echo "OK"
echo "Is card $SD_CARD plugged in?"
[[ -e "$SD_CARD" ]]
echo "OK"
echo "Is card $SD_CARD not mounted?"
if df | grep --quiet "$SD_CARD"
echo "No: umount all partitions that belong to $SD_CARD."
df --exclude-type=tmpfs | awk --assign=PATTERN="$SD_CARD" '$1 ~ PATTERN {system("umount " $1)}'
# --exclude-type to shorten list
# --assign to pass SD_CARD from bash into awk script
# $1 ~ PATTERN act only on lines that contain SD_CARD, i.e. use awk to simulate grep
echo "OK"
echo "!!!!!About to DESTROY ALL DATA ON CARD $SD_CARD!!!!!"
echo "There is no progress indicator, and this could run for several minutes."
read -n1 -r -p "Press any key to continue, Ctrl-C to abort..." ignore
printf "\nRunning...\n"
# use the more conservative 1M blocks - takes more time tho'
dd bs=1M if="$RPI_IMAGE" of="$SD_CARD"
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.