0

I need to create an apple script to perform DD Commands in sequence, to replicate the same .img file to multiple USB flash drives

The context: the computer is connected to a powered USB hub with 7 usb flash drives connected and unmounted by diskutil. No other USB devices or disk are connected to the computer (so the USB Sticks will be assign a disk1 - disk7 resource mapping)

The steps

  • a file named "source.img" will be prepared and put on the desktop (say an .img of a bootable OS)

  • terminal must be asked to change directory to the desktop

  • with administrative privileges (the password can be inserted into the script, or asked once to the user) it must be asked to perform:

"sudo dd if=source.img of=/dev/rdisk1 bs=1m"

  • the script must wait until the operation is concluded, then ask the terminal to perform another DD, to disk2 this time:

"sudo dd if=source.img of=/dev/rdisk2 bs=1m"

and again, wait until the operation is concluded, and then ask:

"sudo dd if=source.img of=/dev/rdisk3 bs=1m"

and so on with rdisk4, rdisk5, rdisk6, rdisk7.

  • at the end of the disk7 operation , the script can shut down the terminal and send a finder message to the user (or an audio notification) that the USB Duplication process to the 7 flash drives is concluded.

It's a way to create a "USB duplicator on the cheap" for bootable images to be put on multiple sticks, I need it for a school project to my students.

Anybody can help ? I am a zero with Applescript. And this thing will be useful for many.

Thank you ! Alberto

1 Answers1

0

Why do you need to do it using Applescript? Why not a simple shell script like this?

#!/bin/bash
cd ~/Desktop
for i in {1..7}; do
   echo "Duplicate to /dev/rdisk${i} (y/n) ?"
   read r
   [[ "$r" == "y" ]] && echo sudo dd if=source.img of=/dev/rdisk${i} bs=1m
done
say "Duplication complete"

Save it on your Desktop as "Duplicator" and then start a Terminal and in the new Terminal window type:

chmod +x Desktop/Duplicator

to make it executable. Then you can run it by double-clicking the "Duplicator" icon on your Desktop.

Note: At the moment, it just echoes the "dd" command, run it and see if you like it then remove the "echo" if all looks correct.

Note: This works on Mavericks and Mountain Lion at least, on some older versions of OSX you may need to name the script "Duplicator.command" and/or associate the script with the Terminal app. See here.

Note: You might try adding a "&" after the "bs=1m" so that the 7 "dd" commands run in parallel, and then adding a new line that says "wait" between the lines "done" and "say" to wait for all "dd" commands to complete. This will mean that you can answer the 7 questions immediately one after the other instead of having to sit there and watch and do them one at a time. It may also make the "dd" commands run faster or slower - depending on the relative performance of your memory sticks and USB bus. It will also avoid problems with "sudo" timing out - though these could be solved by changing the timeout in the file /etc/sudoers.

Community
  • 1
  • 1
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432