-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.sh
More file actions
executable file
·180 lines (154 loc) · 3.65 KB
/
Copy pathpath.sh
File metadata and controls
executable file
·180 lines (154 loc) · 3.65 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/bash
# store original IFS
original_ifs="$IFS";
# change IFS to colon as $PATH is colon seperated
IFS=":";
option="$1"
path="$2"
display_directories()
{
for dir in $PATH;
do
echo "$dir"
done
}
display_man_page()
{
echo "Usage: path [option]... [PATH/COMMAND]..."
echo "Add/Remove/Find PATH/COMMAND in \$PATH"
echo -e "\nOptions:"
echo " no option display all directories in \$PATH"
echo " -C, --check check if directory is present in \$PATH"
echo " -F, --find display path of command"
echo " -A, --add add directory to \$PATH"
echo " -R, --remove remove directory from \$PATH"
echo " -H, --help display help message"
echo -e "\nNote:"
echo " run the script as source to reflect modifications in current shell"
echo " add an alias to access from anywhere and to run as source"
echo " alias path=\"source <script path>/path.sh\""
echo -e "\nExamples:"
echo "root@kali:~# path"
echo "root@kali:~# path --check /bin"
echo "root@kali:~# path --find ls"
echo "root@kali:~# path --add /root/myscripts"
echo "root@kali:~# path --remove /root/myscripts"
echo "root@kali:~# path --help"
}
check_directory()
{
if [[ $PATH == *"$1"* ]]; then
return 1
else
return 0
fi
}
find_command_path()
{
for dir in $PATH;
do
if [ -e "$dir/$1" ]; then
echo "$dir/$1"
return
fi
done
echo "Command not found"
}
add_directory()
{
# check path already in $PATH
check_directory "$1"
if [ $? == 1 ]; then
echo "$1 already in \$PATH"
return
fi
if [ -z "$PATH" ]; then
PATH="$1" # first entry
else
PATH="${PATH}:$1" # subsequent entry
fi
echo "${PATH}"
}
remove_directory()
{
# check if path present in $PATH
check_directory "$1"
if [ $? == 0 ]; then
echo "$1 not found in \$PATH"
return
fi
original_path="${PATH}"
PATH=""
for dir in $original_path;
do
if [[ "$dir" != "$1" ]]; then
PATH+="$dir$IFS"
fi
done
# remove trailing $IFS
PATH=${PATH:0:-1}
echo "${PATH}"
}
handle_check_option()
{
if [ -z "$1" ]; then
echo "Enter a path to check"
else
check_directory "$1"
if [ $? == 1 ]; then
echo "Yes"
else
echo "No"
fi
fi
}
handle_find_option()
{
if [ -z "$1" ]; then
echo "Enter a command to find"
else
find_command_path "$1"
fi
}
handle_add_option()
{
if [ -z "$1" ]; then
echo "Enter a path to add"
else
add_directory "$1"
fi
}
handle_remove_option()
{
if [ -z "$path" ]; then
echo "Enter a path to remove"
else
remove_directory "$path"
fi
}
handle_invalid_option()
{
echo "path: invalid option -- '$1'"
echo "try 'path --help' for more information."
}
# remove trailing forward-slash(/)in path if any
if [ ! -z "$path" ] && [ "${path: -1}" == "/" ]; then
path="${path%?}"
fi
if [ -z "$option" ]; then
display_directories
elif [ "$option" == "-H" ] || [ "$option" == "--help" ]; then
display_man_page
elif [ "$option" == "-C" ] || [ "$option" == "--check" ]; then
handle_check_option "$path"
elif [ "$option" == "-F" ] || [ "$option" == "--find" ]; then
handle_find_option "$path"
elif [ "$option" == "-A" ] || [ "$option" == "--add" ]; then
handle_add_option "$path"
elif [ "$option" == "-R" ] || [ "$option" == "--remove" ]; then
handle_remove_option "$path"
else
handle_invalid_option "$option"
fi
# restore original IFS
IFS=$original_ifs;