Skip to content

Instantly share code, notes, and snippets.

@vpetersson
Created March 30, 2016 11:21
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 vpetersson/6346d67c8466ed513c3adb716a054cdd to your computer and use it in GitHub Desktop.
Save vpetersson/6346d67c8466ed513c3adb716a054cdd to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
RELEASEPATH="$HOME/Downloads/"
SD_DISK=$(mount | grep msdos | awk {'print $1'})
DISK_NO=$(echo $SD_DISK | awk '{print substr($0,length-2,1)}')
RDISK_BASE="/dev/rdisk"
RDISK=$RDISK_BASE$DISK_NO
clear
# Make sure we're root
if [ "$(whoami)" != 'root' ]; then
echo "This script must run as root"
exit 1;
fi
if [ -z "$1" ]; then
echo "No parameter passed. Exiting."
fi
# Validate all strings
for i in "$RELEASEPATH" "$SD_DISK" "$DISK_NO"; do
if [ -z "$i" ]; then
echo "Found empty string base string. Aborting"
exit 1
fi
done
echo -e "Printing mounted disks:\n"
mount
echo -e "\n\nIs $SD_DISK ($RDISK) your SD card? (Y/N)"
read CONFIRM
if [[ "$CONFIRM" == "Y" ]]; then
echo "Unmounting $SD_DISK ..."
diskutil unmount $SD_DISK
cd "$RELEASEPATH"
LATEST_VERSION="$(ls -t *$1* | head -n 1)"
echo "Flashing out $LATEST_VERSION to $RDISK"
set -x
if [[ "$LATEST_VERSION" == *zip ]]; then
unzip -p "$RELEASEPATH/$LATEST_VERSION" | sudo dd bs=1m of=$RDISK
elif [[ "$LATEST_VERSION" == *gz ]]; then
gzip -d -c "$RELEASEPATH/$LATEST_VERSION" | sudo dd bs=1m of=$RDISK
elif [[ "$LATEST_VERSION" == *img ]]; then
dd if="$RELEASEPATH/$LATEST_VERSION" bs=1m of=$RDISK
fi
diskutil eject $SD_DISK
echo "Done."
else
echo "Aborting."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment