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

Click here to return to the 'This script is flawed!' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
This script is flawed!
Authored by: llee on May 09, '12 01:08:59PM

The only way that I could replicate your results, I guess, would be to try to create mountable volumes with all those disk names. But I did considerable testing on the script I submitted with quite a few different volumes mounted. The list you include first (yours is generated by "df -k") when generated by my script does not include all the volumes that yours includes because it isn't "df -k", it's "df -k | awk -F/ '/disk*/ {print $3}'", and it returns a listing for only the items appearing in your second list. My observation is that because the order of the listed items doesn't change between the filtering of the constructs based on "df -k" and "df -hlg", the script that filters out ineligible members still draws the correct association of items in the two lists. Can you store the output of the two initial commands from my script ("df -k | awk -F/ '/disk*/ {print $3}'", and "df -hlg | awk -F/ '/disk*/ {print $5}'") on your system so they can be compared line for line with the output you already included for "df -hlg"?

I know you already developed your own version which I find also appears to work for me, but it occurred to me that a refinement of the grep construct might provide both lists from "df -hlg" instead of one from "df -hlg" and the other from "df -k", so making that assumption, please see if the following change allows my script work for you.

In the original script, change

"df -k | awk -F/ '/disk*/ {print $3}'"


"df -hlg | grep -o 'disk[0-9][a-z][0-9]*'"

Based on that, I may be able to work out a version which doesn't involve creating the hash table (matching items of two lists).

Edited on May 09, '12 01:30:41PM by llee

[ Reply to This | # ]
This script is flawed!
Authored by: llee on May 09, '12 08:22:33PM

Hmm, well, could it be as short and sweet as this?

set alldisks to paragraphs of (do shell script "df -hlg | awk -F/ '/disk*/ {print $5}'")
set nonbootnumber to (count of alldisks)
set alldisks to items 2 thru nonbootnumber of alldisks
set your_selected_device_id to choose from list alldisks with prompt "Please choose one or more volumes to be unmounted." with multiple selections allowed
repeat with the_Item in your_selected_device_id
set the_ID to (do shell script "df -hlg | grep -m 1" & space & quoted form of the_Item & space & "| grep -o 'disk[0-9][a-z][0-9]*'")
do shell script "diskutil unmount /dev/" & the_ID
on error the error_message number the error_number
display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try
end repeat
on error the error_message number the error_number
if the error_number is -128 or the error_number is -1708 then
display dialog "There are no unmountable volumes." buttons {"OK"} default button 1
end if
end try

Edited on May 09, '12 08:55:12PM by llee

[ Reply to This | # ]
I like it (shorter and simpler)
Authored by: vczilla on May 10, '12 09:32:23AM

I tried your shorter version and indeed it works here too. I think the key to this story was to have just one list, the one generated by 'df -l'.

Since you asked me (and I know that doesn't really matter anymore),
here is the output of 'df -k | awk -F/ '/disk*/ {print $3}' on my machine:

% df -hlg | awk -F/ '/disk*/ {print $5}'


and 'df -k | awk -F/ '/disk*/ {print $3}':

 df -k | awk -F/ '/disk*/ {print $3}'
disk0s2                   79203656  51490116  27457540    66%    
disk2s2                  365784156 332114676  33669480    91%    
disk1s1                   66559996  65618364    941632    99%    
disk1s2                   36141052  35825216    315836   100%    
disk1s3                   53588152  48312940   5275212    91%    
disk3s1                  256702603 251655316   5047286    99%    
disk3s2                  306239060 303196908   3042152   100%    
disk3s3                   35929372  34222232   1707140    96%   

That's why it didn't work here.

As a side note, there's a slight typo in the awk script regex, which should be /disk.*/ and not /disk*/ (luckily it doesn't change anything for the intended purpose).

I'm glad you didn't take it the wrong way and that you came up with a really short solution, and I hope I didn't came off as being too pedantic. ;-)

[ Reply to This | # ]