Advertisement
Rockford

imageWriter

Mar 9th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.33 KB | None | 0 0
  1. #!/bin/bash                                                                                                                                                  
  2. # File: imageWriter
  3.  
  4. # Example usage:
  5. # imageWriter [image file [targetDevice]]
  6. # ... or much easier, just run without parameters from the same directory where the EspressoBIN.img is.
  7. # -Then insert the device and it should be detected automatically.
  8.  
  9. image="${1:-EspressoBIN.img}"
  10. dev="${dev:-$2}"
  11.  
  12. [ "$dev" ] || {
  13.   echo "Please insert target USB-stick or SD-card now (or unplug and re-insert)..."
  14.   n=`ls -d /dev/*`
  15.   while [ -z "$DEVICE" ]; do
  16.     sleep 0.1; o="$n"; n=`ls -d /dev/*`
  17.     DEVICES=(`diff -U0 <(echo "$o") <(echo "$n") | egrep "^\+\/dev\/(mmcblk|sd)[^\/]*\$"`)
  18.     for DEV in ${DEVICES[@]}; do
  19.       DEV=${DEV##*/}
  20.       BDEV=${DEV/[0-9]*}
  21.       echo "Found device named: $DEV (block device $BDEV)"
  22.       printf "Write image to this device ? [y/N/c] "
  23.       key=; while [[ "$key" != [YyNnCc] ]]; do read -n 1 key; printf "\b \b"; done
  24.       case $key in
  25.       [Yy]) echo "Yes"; DEVICE="$DEV"; BLKDEVICE="$BDEV"; break ;;
  26.       [Nn]) echo "No" ;;
  27.       [Cc]) echo "Cancel"; exit 1 ;;
  28.       esac
  29.     done
  30.   done
  31.   dev="/dev/$DEVICE"
  32. }
  33.  
  34. echo "dev=$dev"
  35. echo "image=$image"
  36. [ "$dev" ] && [ -f "$image" ] && echo "Writing image..." && sudo cp "$image" "$dev"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement