-
Notifications
You must be signed in to change notification settings - Fork 3
/
mntmtp
executable file
·79 lines (67 loc) · 1.71 KB
/
mntmtp
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
78
#!/usr/bin/env bash
#
# File:
# mntmtp
#
# Description:
# Mount and unmount an MTP device.
#
# Usage:
# mntmtp <action>
#
# <action>:
# "mount" or "m" to mount
# "umount" or "u" to unmount
#
# Dependencies:
# jmtpfs
#
# Set up:
# Create user group "fuse" and add the current system user to it by running
# the following commands:
# sudo groupadd fuse
# sudo gpasswd -a "${USER}" fuse
#
# Note:
# This has only been tested with Android devices.
#
# ======= CONFIGURATIONS ==============
# Mountpoint path of MTP device
MNTPNT_PATH='/mnt/mtp'
# ======= ! CONFIGURATIONS ==============
if [ "$#" -eq 0 ]; then
echo 'mntmtp: no arguments: "mount", "unmount", "m" or "u" required' 1>&2
exit 1
fi
case "${1}" in
'mount' | 'm')
if [ ! -d "${MNTPNT_PATH}" ]; then
mkdir "${MNTPNT_PATH}"
fi
if ! mount | grep "${MNTPNT_PATH}" >/dev/null; then
jmtpfs "${MNTPNT_PATH}"
if mount | grep "${MNTPNT_PATH}" >/dev/null; then
# newline prepended to separate message from jmtpfs output
echo -e "\nmntmtp: successfully mounted device on ${MNTPNT_PATH}"
fi
fi
;;
'unmount' | 'u')
if ! mountpoint "${MNTPNT_PATH}" >/dev/null; then
echo "mntmtp: device not mounted on ${MNTPNT_PATH}" 1>&2
exit 1
fi
if [[ "$PWD" = "${MNTPNT_PATH}"* ]]; then
echo "mntmtp: must leave mountpoint directory" 1>&2
exit 1
fi
fusermount -u "${MNTPNT_PATH}"
if [ "$?" -eq 0 ] && ! mount | grep "${MNTPNT_PATH}" >/dev/null; then
echo "mntmtp: successfully unmounted device from ${MNTPNT_PATH}"
fi
;;
*)
echo 'mntmtp: action unknown: "mount", "unmount", "m" or "u" required' 1>&2
exit 1
;;
esac