Shirt Pocket Discussions  
    Home netTunes launchTunes SuperDuper! Buy Now Support Discussions About Shirt Pocket    

Go Back   Shirt Pocket Discussions > SuperDuper! > General
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 05-11-2007, 12:56 AM
afragen afragen is offline
Registered User
 
Join Date: May 2007
Location: Southern California
Posts: 13
Send a message via AIM to afragen
Quote:
Originally Posted by dnanian View Post
and assumes that there's a single network volume attached.!
This will/should work for multiple attached network AFP mounts. I'm sure something similar would work for any other mount type.

Code:
#!/usr/bin/env bash

sleep 45

if [[ `mount | awk '/^afp_/'` != "" ]]; then
  num_mounts=`mount | grep -c ^afp`
  for (( i = 0; i < $num_mounts; i++ )); do
    afp=`mount | grep ^afp_ | awk {'print$3'} | sed -n '1p;1q'`
    umount -f $afp
  done
fi
Andy

Last edited by afragen; 05-11-2007 at 02:19 AM.
Reply With Quote
  #2  
Old 05-11-2007, 04:12 AM
chris_johnsen chris_johnsen is offline
Registered User
 
Join Date: Jan 2007
Posts: 79
concise shell script examples

(The shell script code fragments below are untested since I don't have any AFP volumes, but people developing shell scripts might find the techniques useful.)

Here are some variations that I find a bit more natural for shell scripting:

Code:
mount | awk '/^afp_/ {print $3}' | xargs -n 1 umount -f
The -n 1 might not be needed on the xargs, but it results in the same umount invocations as the previous script (one per volume). Leaving the -n 1 off would put all the volumes into one umount invocation.

Of course, if you really want to always unmount all AFP volumes, something like this might work, too:

Code:
umount -Aft afp
That last variation is probably OK for AFP, but it is inappropriate for NFS since there are several NFS mediated mounts (automount) that are present on most systems (/Network, /Network/Servers, etc.). Changing afp to nfs in that last command would unmount these normal "volumes", too, which probably is not what the user really wants. Something like the first example with an extra pattern in the awk and/or an extra grep before the awk to select just the target volumes would probably be better for NFS volumes.

BTW, head -1 has the same result as sed -n '1p;1q', and is usually much easier to understand for those not versed in sed/ex/vi.
Reply With Quote
  #3  
Old 05-11-2007, 11:15 AM
afragen afragen is offline
Registered User
 
Join Date: May 2007
Location: Southern California
Posts: 13
Send a message via AIM to afragen
Chris,

Thanks for the refinements and examples. I'm really just a shell scripting newbie. I love the ability to do so much with a small amount of code.

Though as I've just discovered all this code doesn't work if any of the mounts has a space in the name like "Macintosh HD". :-(

Andy

Last edited by afragen; 05-11-2007 at 11:24 AM.
Reply With Quote
  #4  
Old 05-11-2007, 04:03 PM
chris_johnsen chris_johnsen is offline
Registered User
 
Join Date: Jan 2007
Posts: 79
(As before, I don't have any AFP volumes to test this with, but I did test the "space, single-quotes, and/or parentheses in the volume name" aspect with disk images.)

Quote:
Originally Posted by afragen View Post
Though as I've just discovered all this code doesn't work if any of the mounts has a space in the name like "Macintosh HD". :-(

Andy
Yes, that's one of the problems with parsing the under-specified and largely undelimited output that the bare mount command produces (it is normally fine for human consumption, but not great for programs that want to do something with the data).

I worked up this hack to handle volume names with spaces (and single quotes, too):
Code:
mount | grep ^afp_ \
| sed -E -e 's/^.* on //;s/( \([^()]*\))?$//;'"s/'/'\\\\''/g;s/^/'/;s/\$/'/" \
| xargs umount -f
It could all be done in awk, but it looked ugly, so I reverted back to grep and sed. The idea is that the mount point probably directly follows " on " in the output from the mount command, and that the mount point name extends to the end of the line or to just before a space and a parenthesized string at the end of the line. By extracting the volume name using only what comes before and after it, we avoid getting tangled up with the spaces. There still some pitfalls:
  • if the volume name contains the string " on "
  • if the parenthesized expression that the OS adds contains parenthesis itself
If either of these come to pass, this code will fail to correctly extract the volume mount point. The first problem could be mostly alleviated if Perl-compatible regular expressions were available. The rest of the sed commands are difficult to read because of all the contortions required by the shell to get a couple of backslashes to sed. The end result is to put the entire line in single quotes (the last two commands), and escape any previously existing single quotes. This is done so that xargs will treat the volume name as one argument, whether it has spaces, single quotes, or anything else.

OK, here it is with Perl, to better handle the problem with " on " in volume names:
Code:
mount | grep ^afp_ \
| perl -pe 's/.*? on //;s/( \([^)]*\))?$//;'"s/'/'\\\\''/;s/^/'/;s/\$/'/" \
| xargs umount -f
The difference is the extra question mark before " on ". That modifies the star to do a non-greedy match. Don't expect all sed scripts to transfer that easily to Perl, but it was greatly influenced by sed, awk, and other tools, so I often jump into Perl when the job is just a bit too tough for grep, sed, awk, et al. That non-greedy match was the only thing I needed in this instance, though the functionality of the whole script (running mount, selecting and extracting the mount points, running umount) could be done in Perl if too many other tools also started falling short.

This Perl version still has the problem with parentheses in the parenthesized expression that follows the volume name. Is it safer to assume that users won't put parentheses in their volume names, or that Apple won't put them in the mount options list? If both are possible (and they definitely and probably are, respectively) then I don't see a clear way to distinguish between the volume name and the mount options (unless you assume the mount options will always have balanced parentheses, which can't be matched by regular expressions, so it would take a fancier approach anyway).

Oh, and both of these scripts still have a problem if the string " on " appears in the device part of the mount command's output. I haven't seen that happen, but I wouldn't rule it out (maybe AFP or NFS mounts already do that, I don't have anything to test with, but I'd guess AFP uses a URI-like device name and NFS uses the traditional machine:export device name). This is another intractable problem that stems from trying to parse the undelimited format of the output from the mount command. There has to be a better way to get at all this information (either through some delimited output, or an API: mount does it, Disk Utility does it, something else should be able to also), but it may be beyond what is reasonable in a simple little script.
Reply With Quote
  #5  
Old 05-11-2007, 06:18 PM
afragen afragen is offline
Registered User
 
Join Date: May 2007
Location: Southern California
Posts: 13
Send a message via AIM to afragen
Chris,

Your hacks are beautiful. In some preliminary testing here with multiple AFP mounts, the perl version (the only one I'm testing), works great. Just so you know here's what some of my afp mount points look like from the mount command.

Quote:
afp_003Js12XIRWg001Eic06SdO0-1.2d000054 on /Volumes/Backup (nodev, nosuid, mounted by afragen)
afp_003JrO1u5JvO001Eic06SdO0-1.2d000055 on /Volumes/AJF Photo (nodev, nosuid, mounted by afragen)
I was also trying to come up with a solution and think I've found another, less elegant way.

Code:
  if [[ `mount | awk '/^afp_/'` != "" ]]; then
    mount | awk '/^afp_/ {print $3}' | xargs -n 1 umount -f
    num_mounts=`mount | grep -c ^afp`
    for (( i = 0; i < $num_mounts; i++ )); do
      mountpt=`mount | grep ^afp | grep -o '/.* (' | sed 's_/Volumes/__g' | sed 's_ (_:_g' | head -1`
      cmd='tell application "Finder" to eject '\"$mountpt\"
      osascript -e "$cmd"
      sleep 2
    done
  fi
As I'm not nearly as adept I found that mixing in a little AppleScript to eject the Volumes with spaces works too. I too made the assumption about parens, but my assumption was that parens wouldn't be in a volume name and would be immediately after the mount point listed in the mount command. Probably not very good assumptions on the whole.

I really hope this gets indexed by Google as I've been trying to find a way to automatically unmount any connected drives when I switch locations on my laptop. BTW, I did figure that out. It involves a small shell script and a launchd process.

Of course, if there was some way to get the mount point from SD then we wouldn't have to go through all these contortions. But the exercise still helps me for the other script.

Andy
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Smart Update copying files that haven't been changed? salparadi General 3 11-05-2006 11:40 AM
Copying newer files across volumes. chip General 1 08-09-2006 08:07 PM
Long Hang While Copying BackerUpper General 4 06-12-2006 08:26 AM
SuperDuper! Erasing Audio Files? Emerson General 3 06-22-2005 01:41 PM
Smart update copying all files each time fabius General 15 05-29-2005 09:29 PM


All times are GMT -4. The time now is 08:59 PM.


Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2024, vBulletin Solutions, Inc.