-
Notifications
You must be signed in to change notification settings - Fork 0
/
trash
executable file
·82 lines (74 loc) · 2.67 KB
/
trash
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
79
80
81
82
#!/bin/bash
# MIT License
#
# Copyright (c) 2016 Hiroyuki Matsuo
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Abort this script if error happens
set -e
# Get this command's name
CMD_NAME=`basename $0`
# Check whether the OS is macOS or not
if [ "$(uname)" != 'Darwin' ]; then
echo "${CMD_NAME}: You can execute this command only on macOS." 1>&2
exit 1
fi
# Parse specified options
FLAG_R=false
while getopts :rR OPT; do
case $OPT in
r) FLAG_R=true;;
R) FLAG_R=true;;
\?) echo "${CMD_NAME}: ${OPTARG}: Illegal option." 1>&2;
exit 1;;
esac
done
# Shift specified options
shift $((OPTIND - 1))
# Check the number of command line args
if [ "${#}" -lt 1 ]; then
echo "Usage: ${CMD_NAME} [-r] <file>..." 1>&2
exit 1
fi
# Move specified files / directories to the trash box
for NAME in "${@}"; do
# Don't remove "." and ".."
if [ "${NAME: -1}" == "." -o "${NAME: -1}" == ".." ]; then
echo "${CMD_NAME}: \".\" and \"..\" may not be removed." 1>&2
continue
fi
# Check whether specified file / directory exists
if [ ! -e "${NAME}" ]; then
echo "${CMD_NAME}: ${NAME}: No such file or directory." 1>&2
continue
fi
# Require "-r" option when remove directory
if [ -d "${NAME}" -a "${FLAG_R}" != true ]; then
echo "${CMD_NAME}: ${NAME}: Require \"-r\" option when remove directory." 1>&2
continue
fi
# Calculate the absolute path
TARGET=$(cd `dirname "${NAME}"` && pwd)"/"`basename "${NAME}"`
# Remove specified file / directory
osascript -e """
tell application \"Finder\"
move POSIX file \"${TARGET}\" to trash
end tell
""" > /dev/null
done