-
Notifications
You must be signed in to change notification settings - Fork 15
/
files
executable file
·66 lines (57 loc) · 1.64 KB
/
files
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
#!/usr/bin/env bash
# Assign help text to variable for user later
HELP="Usage: files [PATH]"
# Ensure that only a single parameter was passed
# Display help text if more than 1 parameter was supplied
if [ "$#" -ne 1 ]; then
echo "Error: Incorrect number of parameters; please supply only a valid path"
echo
echo $HELP
echo
exit 1
fi
# Assign CWD variable to the parameter supplied by the user;
# if no parameter was supplied, use the current working directory
if [ "$#" -eq 0 ]; then
CWD=$(pwd)
else
CWD="$1"
fi
# If the user supplied "--help", then provide help text
if [ "$CWD" = "--help" ]; then
echo $HELP
echo
exit 0
fi
# Try to catch special instances
case "$CWD" in
'.') # User supplies "." to open current directory
CWD=$(pwd)
;;
'..') # User supplies ".." to open parent directory
CWD=$(dirname $(pwd))
;;
esac
# Check for case where user supplies a subdirectory of the current directory
# (if parameter specified doesn't start with a slash it is assumed to be a
# subdirectory of the current directory)
testchar=$(echo "$CWD" | cut -c 1 -)
if [ ! $testchar = '/' ]; then
CWD="$(pwd)/$CWD"
fi
# Check that the path supplied as a parameter is valid
if [ ! -d "$CWD" ]; then
echo "Invalid path specified; please provide a valid path"
echo
exit 1
fi
# Debugging
#echo "$CWD"
# Open a file manager window at the specified directory
# Checks for macOS (Darwin) and uses open command;
# otherwise, assumes Linux/GNOME and presence of xdg-open command
if [[ $(uname -a) == *Darwin* ]]; then
/usr/bin/open "$CWD"
else
/usr/bin/xdg-open "$CWD" 2>/dev/null 1>/dev/null
fi