-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-to-path
executable file
·91 lines (78 loc) · 2 KB
/
add-to-path
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
83
84
85
86
87
88
89
90
91
#!/usr/bin/env bash
dir=`pwd`
function show_help {
echo "Add a binary file to PATH"
echo "This must be run from the directory where the binary exists"
echo "Usage: $0 [options] [filename]"
echo "Options:"
echo -e "-h\tShow this help"
}
ask() {
# https://djm.me/ask
local prompt default reply
while true; do
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
# Ask the question (not using "read -p" as it uses stderr not stdout)
echo -n "$1 [$prompt] "
# Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
read reply </dev/tty
# Default?
if [ -z "$reply" ]; then
reply=$default
fi
# Check if the reply is valid
case "$reply" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
if [ -n "$1" ]
then
binary="$1"
else
echo "ERROR: filename must be provided"
show_help
exit 1
fi
while getopts "hd" opt; do
case "$opt" in
h)
show_help
exit 0
;;
esac
done
echo "Adding '$binary' to PATH..."
if ask "This will add $binary to PATH, continue?" Y
then
echo "Adding '$binary' to PATH..."
if [ -h "/usr/local/bin/$binary" ]
then
echo -e "\n************"
eval "ls -al /usr/local/bin/$binary"
if yes_no_question "$binary link already exists, remove it?"
then
echo "Deleting link /usr/local/bin/$binary..."
eval "sudo rm /usr/local/bin/$binary"
else
echo "Exiting, no changes made..."
exit 0
fi
echo -e "************\n"
fi
echo "Creating symlink /usr/bin/$binary..."
eval "sudo ln -s $dir/$binary /usr/local/bin/$binary"
eval "ls -al /usr/local/bin/$binary"
else
echo "Cancelling..."
fi