Scripting the parallel execution of a tedious task
December 12, 2006 12:03 PM

Calling all shell script gurus! Help me write a command that performs an action on items in a list in parallel (as opposed to sequentially).

I am trying to write a command that zeroes out the data on any number of attached disks simultanously (or nearly so). I started with the basics and have written the script below that accomplishes the task sequentially. (This is in Mac OS X, bash shell):

diskutil list | grep /disk | grep -v disk0 | sed 's|/dev/||g' | \
while read DISK; do
diskutil zeroDisk $DISK
done

The first line returns a list of attached disks:
disk1
disk2
disk3
etc... all except disk0.

Nothing I care about resides on any disk but disk0, which is why it's excluded. The while loop executes the 'diskutil zerodisk' command on each item in the list. I can manually start this command on each disk and the computer will happily perform the tasks in parallel. How can I script the parallel execution of this task?
posted by pmbuko to Computers & Internet (7 answers total) 1 user marked this as a favorite
try either
"bg diskutil zeroDisk $DISK"
or
"diskutil zeroDisk $DISK &"
posted by Good Brain at 12:09 PM on December 12, 2006


the & gives me an error. I should have mentioned that. It works fine outside a script, though.
posted by pmbuko at 12:20 PM on December 12, 2006


pmbuko: What error?

Also, did you try using bg instead as Good Brain suggested?
posted by grouse at 12:28 PM on December 12, 2006


on the mac, bg can be used on a job that has just been stopped, not while invoking a command.

The error I get by having the ampersand in the script is:

-bash: syntax error near unexpected token `;'

The ampersand immediately precedes the ;
posted by pmbuko at 12:31 PM on December 12, 2006


ooops! My bad. The & works like a charm. It's the extraneous ; I added that was the problem.
posted by pmbuko at 12:33 PM on December 12, 2006


You can skip the whole while/read thing also.

for D in $(diskutil list | grep /disk | grep -v disk0 | sed 's|/dev/||g'); do diskutil zeroDisk $D & done
posted by Rhomboid at 12:39 PM on December 12, 2006


gotta love unix.
posted by pmbuko at 6:29 PM on December 12, 2006


« Older Make love, start a revolution?   |   zip+4 to lat/lon? Where? & precision necessary... Newer »
This thread is closed to new comments.