forked from jimlawton/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions
339 lines (298 loc) · 8.1 KB
/
functions
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/bin/sh
# Shell Functions.
function killxscreensaver()
{
pkill xscreensaver
echo ""
return
}
function col {
awk '{print $'$(echo $* | sed -e 's/ /,$/g')'}';
}
# Awk calculator.
function calc {
awk "BEGIN { print $* }" ;
}
# Add a path to the global PATH.
function pathmunge {
if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
fi
}
# .. - Does a 'cd ..'
# .. 3 - Does a 'cd ../../..'
#
# Usage .. [n]
# Go up n-levels.
# i.e.: .. 3 will go up 3 levels
function .. {
local arg=${1:-1};
local dir=""
while [ $arg -gt 0 ]; do
dir="../$dir"
arg=$(($arg - 1));
done
cd $dir >&/dev/null
}
# Usage ... Thing/Some
# Go up until you encounter Thing/Some, then go there
# i.e.: I'm in /usr/share/X11
# ... src will go up to /usr, then change to /usr/src
function ... {
if [ -z "$1" ]; then
return
fi
local maxlvl=16
local dir=$1
while [ $maxlvl -gt 0 ]; do
dir="../$dir"
maxlvl=$(($maxlvl - 1));
if [ -d "$dir" ]; then
cd $dir >&/dev/null
fi
done
}
# Change extensions of specified files.
function chext {
local fname
local new_ext="$1"
shift
IFS=$'\n'
for fname in $@
do
mv "$fname" "${fname%.*}.$new_ext"
done
}
# Execute a command via SSH on a set of machines.
function cluster {
for mach in $CLUSTER; do
(echo -n "$mach: "; ssh -q $mach $*)
done
}
# Yum update a set of hosts.
function clupdate {
for mach in $CLUSTER; do
echo -n "$mach: "
ssh -q -t $mach sudo yum update -q -y
if [ $? -ne 0 ]; then
break
fi
done
}
function tools {
pushd . >/dev/null
brtop
if [ -d $(pwd)/tools ]; then
PATH=${PATH/$TOOLS:/}
TOOLS=$(pwd)/tools/cm
pathmunge ${TOOLS}
#PATH=${TOOLS}:${PATH}
echo $TOOLS
fi
popd >/dev/null
}
# Determine if proxy is needed.
function checkproxy {
NETDEV=`netstat -nr | grep '^0.0.0.0' | awk '{print $8}'`
#IP_ADDR=`ip addr | grep inet | egrep '(eth|em)[0-9]' | awk '{print $2}' | awk -F/ '{print $1}'`
for netdev in $NETDEV; do
IP_ADDR=`/sbin/ifconfig $netdev | grep inet | grep -v inet6 | awk '{print $2}' | sed 's/addr://'`
SUBNET=`echo ${IP_ADDR} | awk -F. '{printf("%s.%s.%s", $1, $2, $3);}'`
if [ "$SUBNET" == "193.120.91" -o "$SUBNET" == "192.168.91" -o "$SUBNET" == "192.168.92" -o "$SUBNET" == "192.168.93" ]; then
proxyon
else
proxyoff
fi
done
}
# Gnome: set proxy.
# Args:
# 1: mode - 'none', 'manual', 'auto'.
# 2: mode=none: ignored
# mode=manual: host
# mode=auto: url
# 3: mode=manual: port
function gnomeproxy {
if [ -z "$DISPLAY" ]; then
return
fi
machineid=$(dbus-uuidgen --get)
export $(DISPLAY=$DISPLAY dbus-launch --autolaunch="$machineid")
ignorelist=`echo $no_proxy | sed 's/^\[\(.*\)\]$/\1/'`
quoted_ignorelist=`echo $ignorelist | sed "s/[^,]\+/'\0'/g"`
gnome3_ignorelist="[${quoted_ignorelist}]"
gsettings set org.gnome.system.proxy ignore-hosts "${gnome3_ignorelist}"
if [ "$1" == 'auto' ]; then
#echo "Setting auto proxy url: $2"
gsettings set org.gnome.system.proxy autoconfig-url "$2"
gsettings set org.gnome.system.proxy.http enabled 'true'
gsettings set org.gnome.system.proxy mode 'auto'
elif [ "$1" == 'manual' ]; then
#echo "Setting manual proxy: $2 $3"
gsettings set org.gnome.system.proxy.http host "$2"
gsettings set org.gnome.system.proxy.http port "$3"
gsettings set org.gnome.system.proxy use-same-proxy "true"
gsettings set org.gnome.system.proxy.https host "$2"
gsettings set org.gnome.system.proxy.https port "$3"
gsettings set org.gnome.system.proxy.ftp host "$2"
gsettings set org.gnome.system.proxy.ftp port "$3"
gsettings set org.gnome.system.proxy.socks host "$2"
gsettings set org.gnome.system.proxy.socks port "$3"
gsettings set org.gnome.system.proxy.http enabled 'true'
gsettings set org.gnome.system.proxy mode 'manual'
else
gsettings set org.gnome.system.proxy.http enabled 'false'
gsettings set org.gnome.system.proxy mode 'none'
fi
}
# Turn on proxy.
function proxyon {
export http_proxy="${S3_PROXY}"
export https_proxy="${S3_PROXY}"
export no_proxy="${S3_NO_PROXY}"
rm -f ~/.subversion/servers ~/.subversion/config
if [ -f ~/.subversion/servers.proxy ]; then
ln -sf ~/.subversion/servers.proxy ~/.subversion/servers
fi
if [ -f ~/.subversion/config.work ]; then
ln -sf ~/.subversion/config.work ~/.subversion/config
fi
~/dotfiles/sublime-text/sublime-set-proxy.py $http_proxy
~/dotfiles/atom/atom-set-proxy.py $https_proxy
#gnomeproxy "auto" "${S3_AUTO_PROXY}"
#gnomeproxy "manual" "${S3_PROXY_HOST}" "${S3_PROXY_PORT}"
# echo "Proxy enabled."
}
# Turn off proxy.
function proxyoff {
unset https_proxy
unset http_proxy
unset no_proxy
unset ftp_proxy
unset all_proxy
unset HTTPS_PROXY
unset HTTP_PROXY
unset NO_PROXY
unset FTP_PROXY
unset ALL_PROXY
rm -f ~/.subversion/servers ~/.subversion/config
if [ -f ~/.subversion/servers.noproxy ]; then
ln -sf ~/.subversion/servers.noproxy ~/.subversion/servers
fi
if [ -f ~/.subversion/config.home ]; then
ln -sf ~/.subversion/config.home ~/.subversion/config
fi
~/dotfiles/sublime-text/sublime-set-proxy.py
~/dotfiles/atom/atom-set-proxy.py
#gnomeproxy "none"
# echo "Proxy disabled."
}
# Toggle proxy.
function proxy {
if [ -z "$http_proxy" ]; then
proxyon
else
proxyoff
fi
}
# Create a new git repo with one README commit and cd into it
function git.nr() {
mkdir $1
cd $1
git init
touch README
git add README
git commit -m "First commit."
}
function find.name() {
wild=\'\*$1\*\'
sh -c "find . -iname $wild"
}
# Function that takes 2 args, a list and a string,
# and returns 0 if the string is in the list, or
# 1 otherwise.
function contains() {
for item in $1; do
if [ "$item" = "$2" ]; then
return 0
fi
done
return 1
}
# From Eli Bendersky
# http://eli.thegreenplace.net/2013/06/11/keeping-persistent-history-in-bash/
function log_bash_persistent_history() {
local rc=$?
[[ $(history 1) =~ ^\ *[0-9]+\ +([^\ ]+\ [^\ ]+)\ +(.*)$ ]]
local date_part="${BASH_REMATCH[1]}"
local command_part="${BASH_REMATCH[2]}"
# Try to skip usernames/passwords.
if contains "$command_part" "$USER"; then
return
fi
for username in $_USERNAMES; do
if contains "$command_part" "$username"; then
return
fi
done
if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]; then
echo "$HOSTNAME | $date_part | $command_part" >> ~/.persistent_history
export PERSISTENT_HISTORY_LAST="$command_part"
fi
}
# gitignore.io
# http://www.gitignore.io/cli
function gi() {
curl http://www.gitignore.io/api/$@ ;
}
# CMF Git wrapper
function cmf() {
if [ -z "$1" -o -z "$2" ]; then
echo "usage: cmf <gitcmd> <path>"
return
fi
local baseurl="https://code.rdkcentral.com/r"
git $1 ${baseurl}/$2 $3 $4 $5 $6 $7 $8 $9
}
# CCP Git wrapper
function ccp() {
if [ -z "$1" -o -z "$2" ]; then
echo "usage: cmf <gitcmd> <path>"
return
fi
local baseurl="https://gerrit.teamccp.com/rdk"
git $1 ${baseurl}/$2 $3 $4 $5 $6 $7 $8 $9
}
# Linux?
function isLinux {
if [ "$OS" = "Linux" ]; then
return 0
fi
return 1
}
# Mac?
function isMac {
if [ "$OS" = "Darwin" ]; then
return 0
fi
return 1
}
# iTerm2 tab titles
function title {
if [ ! -e "${HOME}/.iterm2_shell_integration.bash" ]; then
return
fi
if [ "$1" ]; then
# Manually specify the title.
echo -ne "\033]0;${*}\007"
else
# This shows the path from ~ in the title.
#echo -ne "\033]0;${PWD/#$HOME/~}\007"
# This shows the last part of the path in the title.
echo -ne "\033]0;${PWD##*/}\007"
fi
}