Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Here's a perl script to mount and unmount by volume name' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Here's a perl script to mount and unmount by volume name
Authored by: fix on Apr 08, '04 01:18:02AM

This script combines a couple of the great ideas above into one perl script that will mount and unmount a drive based on the that name that it appears as on the desktop (i.e. no /dev stuff). It also uses touch to write a file before unmounting.

Like the others, I use it to mount my firewire drive before doing a backup, and then I unmount it so it'll spin down.
----------
Usage:

Mount a drive:
hd -m driveName

Unmount a drive:
hd -u driveName
-------------
To use this on your machine (just like the other perl example above)

1. Copy the text into a new text file called 'hd'
2. >chmod +x hd
3. Copy the file somewhere into your path
4. >rehash
--------------------------------- Perl script starts at next line:<code>
#!/usr/bin/perl

use warnings;
use strict;
use Getopt::Std;

our $OPTIONS = {};
getopts('mu',$OPTIONS);

our $vol_name = $ARGV[0] || 'zbackup';
our $vol_regex = "volName\\s*=\\s*'$vol_name'";


our @entries = grep(/$vol_regex/o, `disktool -l`);

if (! @entries) {
die "No such volume '$vol_name'";
} elsif ($#entries > 0) {
warn("More than one mountable partition matching $vol_name, mounting the first one.");
}

if ($entries[0] =~ m|\('([a-zA-Z0-9]+)'\s*,\s*Mountpoint\s*=\s*'(.*?)'\s*,\s*fsType\s*=\s*'(\S*?)'|) {
my $disk = $1;
my $mount = $2;
my $fs = $3;
if ($OPTIONS->{u}) {
if ($mount) {
`touch $mount/.dummy`;
`disktool -u $disk`;
}
} elsif (! $mount) { #if you pass no flags it'll also mount
`disktool -m $disk`;
}
} else {
die("Entry did not match expectations: $entries[0]");
}


exit;

__END__
</code>



[ Reply to This | # ]