Created
May 16, 2016 04:25
-
-
Save yrps/ca98086409836e4a9d4826cb8752ed52 to your computer and use it in GitHub Desktop.
Helper script to mount/unmount mtp (Android et. al.) devices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| set -euo pipefail | |
| usage() { | |
| local basename | |
| basename="$(basename "$0")" | |
| cat <<DOC | |
| Usage: | |
| $basename [options] [mount|unmount] [mtp_device_number] | |
| Mount an mtp device using simple-mtpfs, or unmount with fusermount. | |
| If no command is given, available device numbers and names are listed. | |
| If no number is given, all devices are mounted or unmounted. | |
| Mount points are created in \$HOME/Desktop and named after the device. | |
| If a device name has multiple words, only the first word is used. | |
| Options: | |
| -h, --help Display this help and exit. | |
| -b dirname, --basedir dirname Use dirname as the base of mountpoints. | |
| See simple-mtpfs (1), fusermount (1), mountpoint (1) | |
| DOC | |
| exit 127 | |
| } | |
| basedir="$HOME/Desktop" | |
| case ${1:-} in | |
| -h | --help) | |
| usage | |
| ;; | |
| -b | --basedir) | |
| basedir="${2:-}" | |
| if [ -z "$basedir" ]; then | |
| printf %s\\n "Parameter required for basedir argument." >&2 | |
| exit 1 | |
| fi | |
| shift 2 | |
| ;; | |
| esac | |
| dev=$(simple-mtpfs --list-devices) | |
| if [ ! $? ]; then | |
| # Something went wrong with the device query. | |
| printf %s\\n "$dev" >&2 | |
| exit 1 | |
| elif [ -z "${1:-}" ]; then | |
| # No command; list devices and quit. | |
| printf %s\\n "$dev" | |
| exit 0 | |
| fi | |
| mode="${1:-}" | |
| dev_num="${2:-}" | |
| errors= | |
| dev_name_from_num() { | |
| # Pass the device number; return the first word of the device name. | |
| local dev_name="$1" | |
| printf %s\\n "$dev" | sed -nr -e "/^$dev_name:\s+(\S+).*/{s//\1/p;q}" | |
| } | |
| do_mount() { | |
| local dev_num="$1" | |
| local mountpoint="$basedir/$2" | |
| if [ -z "$2" ]; then | |
| printf %s\\n "There is no device $1 to mount." >&2 | |
| errors=1 | |
| return | |
| fi | |
| if mountpoint --quiet "$mountpoint"; then | |
| printf %s\\n "$mountpoint is already a mountpoint." >&2 | |
| errors=1 | |
| return | |
| fi | |
| [ ! -d "$mountpoint" ] && mkdir "$mountpoint" | |
| if simple-mtpfs --device "$dev_num" "$mountpoint"; then | |
| printf %s\\n "Mounted $mountpoint" | |
| else | |
| printf %s\\n "Failed to mount $mountpoint" >&2 | |
| fi | |
| } | |
| do_unmount() { | |
| local dev_num="$1" | |
| local mountpoint="$basedir/$2" | |
| if [ -z "$2" ]; then | |
| printf %s\\n "There is no device $dev_num to unmount." >&2 | |
| errors=1 | |
| return | |
| fi | |
| if ! mountpoint --quiet "$mountpoint"; then | |
| printf %s\\n "$mountpoint is not a mountpoint." >&2 | |
| errors=1 | |
| return | |
| fi | |
| if fusermount -u "$mountpoint"; then | |
| printf %s\\n "Unmounted $mountpoint" | |
| rmdir "$mountpoint" | |
| else | |
| printf %s\\n "Failed to unmount $mountpoint" >&2 | |
| fi | |
| } | |
| case "$mode" in | |
| mount) | |
| do_cmd=do_mount | |
| ;; | |
| unmount|umount) | |
| do_cmd=do_unmount | |
| ;; | |
| *) | |
| printf %s\\n "Unknown mode '$mode'; use 'mount' or 'unmount'" >&2 | |
| exit 1 | |
| ;; | |
| esac | |
| if [ -z "$dev_num" ]; then | |
| # No number given; (un)mount all devices that were found earlier. | |
| while IFS=': ' read -r num name _; do | |
| $do_cmd "$num" "$name" | |
| done <<DEVS | |
| $dev | |
| DEVS | |
| else | |
| # (Un)mount the given device number. | |
| $do_cmd "$dev_num" "$(dev_name_from_num "$dev_num")" | |
| fi | |
| # Somthing went wrong on at least one (un)mount attempt. | |
| [ -n "$errors" ] && exit 2 | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment