-
Notifications
You must be signed in to change notification settings - Fork 0
/
eject
executable file
·77 lines (72 loc) · 2.28 KB
/
eject
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
#
# eject -
#
# v0.1 2007-05-21 - Morgan Aldridge <[email protected]>
# Initial version.
# v0.2 2010-10-24 - Morgan Aldridge
# Minor cleanup. Automatically prepend /Volumes/ to volume name if omitted.
# v0.3 2010-12-07 - Morgan Aldridge
# Added "-f" option to force a disk to eject. Now supports ejecting of
# network volumes.
#
# TO DO:
# - Verbose doesn't currently do anything.
# global variables
force=false
verbose=false
help=false
# print usage instructions (help)
function usage() {
printf "Usage: eject [options] volume ...\n"
printf " -f force volume to be ejected, even if files/directories are open"
printf " -v verbose output\n"
printf " -h print these usage instructions\n"
}
# see if any arguments were passed in
if [ $# -gt 0 ]; then
# if so, step through them all and process them
while [ $# -gt 0 ]; do
# see if the user intended us to run in verbose mode
if [ "$1" = "-v" ]; then
shift
verbose=true
# see if the user intended us to force the action
elif [ "$1" = "-f" ]; then
shift
force=true
# see if the user requested help
elif [ "$1" = "-h" ]; then
shift
help=true
usage
else
# don't bother doing anything if the user requested help
if ! $help; then
if $force; then f="force"; fi
# try to detect whether the user prepended "/Volumes/" to the volume name
if [ "${1:0:9}" = "/Volumes/" ]; then
vol="$1"
else
vol="/Volumes/$1"
fi
# determine the type of filesystem of the mount so we can unmount network volumes appropriately
mnt_info=$(mount | grep "$vol")
IFS=\(, read -r -d '' _ fs_type _ <<< "$mnt_info"
if [[ ( $fs_type == "afpfs" ) || ( $fs_type == "smbfs" ) || ( $fs_type == "webdav" ) ]]; then
diskutil unmount $f "$vol"
else
if $force; then
diskutil unmountDisk $f "$vol"
else
diskutil eject "$vol"
fi
fi
fi
shift
fi
done
else
printf "No volumes were specified to be ejected.\n\n"
usage
fi