-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetgo
More file actions
301 lines (272 loc) · 6.92 KB
/
Copy pathsetgo
File metadata and controls
301 lines (272 loc) · 6.92 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/bin/bash
#
# setgo - Quick directory change helper
#
# USAGE
# See usage() function below.
#
# FILES
# ~/.go
#
# LICENSE
# None. This script is free, gratis, pass it around.
#
# WARRANTY
# Absolutely none. If you break it you own both parts.
# There is no guarantee this script will do anything
# useful, correctly, or as anyone might expect.
#
# 08/23/1991 original on AT&T 3B2-1200
# 09/02/1991 Added -d delete option
# 09/09/1991 Added -f find option
# 05/26/1992 Ported to Alpha SysV
# 08/12/1993 Ported to Ultrix
# 10/03/1994 Ported to HP-UX
# 02/12/1995 Ported to Solaris
# 08/15/1997 Ported to NCR 8105R
# 03/13/1998 Ported to DEC UNIX
# 01/28/2001 Ported to Redhat Linux 6.1.2
# 02/22/2001 Added -? check
# 07/15/2005 Added better usage information
# 10/26/2005 Added -e edit option. EDITOR coded in this script.
# 07/25/2007 3.0, added typing shortcut when a '.' (dot) is specified
# the current directory will be used instead.
# Added the delgo construct.
# 08/17/2007 Fixed-up usage info
# 3/18/1996 Ported to RedHat bash
# 2/20/2004 Ported to CentOS
# 3/23/2016 Ported to Linux Mint
#######
EDITOR=vim
version='3.1'
doAdd ()
{
echo "alias -- -$C='cd $D'" >> ~/.go
}
doEdit ()
{
if [ ! -e ~/.go ]; then
echo "alias -- -.='source ~/.go'" >~/.go
fi
if [ -w ~/.go ]; then
$EDITOR ~/.go
exit
fi
echo -e "\nFAILURE : File ~/.go not found or not writable\n\n"
exit 1
}
doFind ()
{
fgrep -- "-$C=" ~/.go >>/dev/null
result=$? # 1=not found, 0=found
}
doDelete ()
{
fgrep -v -- "-$C=" ~/.go > ~/.go-tmp
mv -f ~/.go-tmp ~/.go
}
doInternals ()
{
fgrep 'alias -- -.' ~/.go >/dev/null 2>&1
if [ $? -ne 0 ]; then
dt=`date`
echo "# created by setgo ${version}, $dt" >> ~/.go
echo "# Maintained by setgo, DO NOT EDIT" >> ~/.go
echo "alias -- -.='source ~/.go;echo Go ok'" >> ~/.go
fi
fgrep 'alias -- rego' ~/.go >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "alias -- rego='source ~/.go;echo Go ok'" >> ~/.go
fi
fgrep 'delgo ()' ~/.go >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo -e 'delgo ()\n{\n unalias -- `setgo -dS "$@"` >/dev/null 2>&1\n echo "Go removed"\n}' >>~/.go
fi
}
usage ()
{
echo ''
echo "setgo v${version} - Quick directory change helper"
echo ''
echo 'Simple script to maintain a file of "Go" commands...aliases used to'
echo 'quickly change directories with a simple name. For example, if you'
echo 'are currently in /etc/init.d and run the command:'
echo ''
echo ' setgo init'
echo ''
echo 'a line is added to the file ~/.go that will contain:'
echo ''
echo " alias -- -init='cd /etc/init.d'"
echo ''
echo 'which says "add an alias called -init which, when typed at the'
echo 'command line, will execute the command: cd /etc/init.d".'
echo ''
echo 'The file ~/.go is maintained with a series of aliases that all'
echo 'have a leading - (dash). This file should be "sourced" in your'
echo '.bashrc, .bash_user, .profile or whatever login script file so'
echo 'the aliases will be included in your environment.'
echo ' Example: source ~/.go or . ~/.go'
echo ''
echo 'The ~/.go file should also contain the ADDITIONAL COMMANDS.'
echo ''
echo 'USAGE: setgo [-d][-e][-f] name'
echo ''
echo ' Options:'
echo ' -d delete name from ~/.go file.'
echo ' Does not unalias Go command, see ADDITIONAL COMMANDS.'
echo ' -e edit the ~/.go file. EDITOR is coded in this script.'
echo ' -f find all go entries containing name. If name is not specified'
echo ' then use the current directory name.'
echo ' name logical "Go" name for the current directory. If "." (dot) is'
echo ' specified then the name of the current directory is used.'
echo ''
echo ' If neither -d or -f are used then a new go name is added.'
echo ''
echo ' To use a "Go" command type: -name'
echo ''
echo ' Examples:'
echo ' setgo work creates new go name: -work'
echo ' setgo -f find all entries containing the current directory'
echo ' setgo -f work find all go entries containing: work'
echo ' setgo -d work delete go entry: -work'
echo ''
echo 'ADDITIONAL COMMANDS:'
echo ' These commands are automagically added:'
echo ' -. re-sources the ~/.go file. REQUIRED to use new entries.'
echo ' rego alias for -. (dash dot).'
echo ' delgo name delete entry "name". Use "." for current directory name.'
echo ' This command also does an unalias of the entry.'
echo ''
exit 1 # exit
}
result=0
# get the current directory
D=$(pwd)
if [ -z "$D" ]; then
D=`pwd`
fi
#
# validate arguments
if [ $# -lt 1 ]; then
usage
fi
# check for edit option
if [ "$1" == "-e" ]; then
doEdit
exit
fi
# check for delete option
forceDelete=0
noecho=0
if [ "$1" == "-d" ]; then
forceDelete=1
shift
fi
if [ "$1" == "-dS" ]; then
forceDelete=1
noecho=1
shift
fi
# create a ~/.go file if one does not exist
if [ ! -f ~/.go ]; then
echo -n '' >~/.go
fi
# make sure the other Internal commands exist
doInternals
# perform find-directory option
if [ "$1" == "-f" ]; then
shift
if [ $# -gt 0 ]; then
D="$1"
fi
if [ "$D" == "." ]; then
D=`pwd`
D=`basename "$D"`
fi
echo ''
echo 'setgo: Findings ...'
fgrep -- "$D" ~/.go
if [ $? -eq 1 ]; then
echo "setgo: keyword..... $D not found"
fi
echo ''
exit 0 # exit
fi
if [ "$1" == "?" -o "$1" == "-h" -o "$1" == "--help" ]; then
usage
fi
if [ $# -gt 1 ]; then
echo ''
echo 'setgo error: keyword may not contain spaces'
usage
fi
# strip any leading dash (-)
if [ "${1:0:1}" == "-" ]; then
C=${1:1}
else
C=$1
fi
expanded=''
if [ "$C" == "." ]; then
C=`pwd`
C=`basename "$C"`
expanded=' (expanded from ".")'
fi
# check the keyword again
if [ -z "$C" ]; then
echo ''
echo 'setgo error: keyword required for current directory, '.' (dot) for the current directory'
usage
fi
if [ "$C" == "?" ]; then
usage
fi
# perform delete option
if [ $forceDelete -eq 1 ]; then
doFind
if [ $result -eq 1 ]; then
if [ $noecho -eq 0 ]; then
echo ''
echo "setgo: keyword..... ${C}${expanded} not found"
echo ''
fi
else
doDelete
if [ $noecho -eq 0 ]; then
echo ''
echo "setgo: keyword..... ${C}${expanded} DELETED"
echo ''
fi
fi
if [ $noecho -eq 1 ]; then
echo "-${C}"
fi
exit 0 # exit
fi
echo ''
echo "setgo: keyword..... $C${expanded}"
echo " directory... $D"
# does it already exist?
E=$( fgrep -- "-$C=" ~/.go )
if [ $? == 0 ]; then
echo ''
echo "$C already exists as:"
echo -e " $E\n"
read -p "Do you wish to replace it? " R
R=${R:0:1}
if [ "$R" == 'y' -o "$R" == 'Y' ]; then
doDelete
doAdd
else
echo 'cancelled'
echo ''
exit 1 # exit
fi
else
echo " $C is new"
doAdd
fi
echo ''
echo 'Execute the command: rego or the alias -. to enable the changes.'
echo ''
unset C D E R result expanded