-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.sh
executable file
·354 lines (310 loc) · 8.66 KB
/
core.sh
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# core things
# environment variables
[ -d ${HOME}/bin ] && export PATH="${PATH}:${HOME}/bin"
export MC_COLOR_TABLE=editnormal=:normal=
export LESSCHARSET=utf-8
export GREP_COLORS="mt=1;33"
# unlimited shell history
export HISTSIZE=-1
export HISTFILESIZE=-1
# aliases
alias grep='grep --color=auto'
alias ls='ls -G'
alias ll='ls -lh'
alias la='ll -a'
alias lll='ll -@'
alias pstree='pstree -g 2'
alias top='top -o cpu -O rsize'
alias scr='screen -r'
alias cdc='cd;clear'
alias grephash='grep -v -e "^\s*#"'
alias grephashempty='grep -v -e "^\s*#" -e "^$"'
alias 7zPPMd='7z a -t7z -mx=9 -m0=PPMd'
alias 7zUltra='7z a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on'
alias openPorts='lsof -P -iTCP -sTCP:LISTEN'
alias normalize='chown -R $USER:$MAIN_GROUP *; find . -type d -exec chmod 755 {} \;; find . -type f -exec chmod 644 {} \;'
alias make_sh_executable='find . -type f -iname "*.sh" -exec chmod 755 {} \;'
if [ "$USER" = "root" ]; then
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
fi
alias last5='ls -t | head -n 5'
alias last10='ls -t | head -n 10'
alias last15='ls -t | head -n 15'
alias lastx='ls -t | head -n'
# functions
bullet() {
if [ -t 1 ]; then
case "$1" in
red)
echo -e -n "\033[1;31m"
;;
green)
echo -e -n "\033[1;32m"
;;
yellow)
echo -e -n "\033[1;33m"
;;
esac
echo -e -n "●\033[0m "
else
echo -n "* "
fi
}
bullet_err() {
if [ -t 2 ]; then
case "$1" in
red)
echo -e -n "\033[1;31m"
;;
green)
echo -e -n "\033[1;32m"
;;
yellow)
echo -e -n "\033[1;33m"
;;
esac
echo -e -n "●\033[0m "
else
echo -n "* "
fi
}
hr() {
cols=$(($(tput cols)-1))
for ((i=0; i<$cols; i++)) {
echo -n "―"
}
echo
}
dus() {
du -sh "$@" | sort -h
}
sortedless() {
sort "$1" | less
}
ex() {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xvjf $1;;
*.tar.gz) tar xvzf $1;;
*.tar.xz) tar xvJf $1;;
*.tar.lzma) tar --lzma xvf $1;;
*.bz2) bunzip $1;;
*.rar) unrar $1;;
*.gz) gunzip $1;;
*.tar) tar xvf $1;;
*.tbz2) tar xvjf $1;;
*.tgz) tar xvzf $1;;
*.zip) unzip $1;;
*.Z) uncompress $1;;
*.7z) 7z x $1;;
*.dmg) hdiutul mount $1;; # mount OS X disk images
*) echo "'$1' cannot be extracted via >ex<";;
esac
else
echo "'$1' is not a valid file"
fi
}
mcd() {
mkdir -p "$1" && cd "$1";
}
md5() {
echo -n "$1" | openssl md5 /dev/stdin
}
sha1() {
echo -n "$1" | openssl sha1 /dev/stdin
}
sha256() {
echo -n "$1" | openssl dgst -sha256 /dev/stdin
}
sha512() {
echo -n "$1" | openssl dgst -sha512 /dev/stdin
}
rot13() {
echo "$1" | tr "A-Za-z" "N-ZA-Mn-za-m"
}
rot47() {
echo "$1" | tr "\!-~" "P-~\!-O"
}
urlencode() {
python3 -c "import sys, urllib.parse as ul; print(ul.quote(sys.argv[1]))" "$1"
}
urldecode() {
python3 -c "import sys, urllib.parse as ul; print(ul.unquote(sys.argv[1]))" "$1"
}
millis() {
python3 -c "import time; print(int(time.time()*1000))"
}
nanos() {
python3 -c "import time; print(int(time.time()*1000000000))"
}
weather() {
wget -q -O- http://wttr.in/Budapest
}
findAll() {
eval "find \"$1\" -iname '$2'"
}
findFile() {
eval "find \"$1\" -type f -iname '$2'"
}
findDir() {
eval "find \"$1\" -type d -iname '$2'"
}
withoutExt() {
echo "${1%.*}"
}
backup() {
cp "${1}" "${1}.bck-$(date '+%Y%m%d%H%M%S')"
}
# [-d days|-b rsa-bits] <path-base (will be appended by .key and .crt)>
create-openssl-key-and-cert() {
if [ $# -lt 1 ]; then
echo "Usage: create-openssl-key-and-cert [-d days|-b rsa-bits] <path-base (will be appended by .key and .crt)>"
return 1
fi
days=365; bits=2048; pathbase=""
while [ ! -z "$1" ]; do
param="$1"; shift
case "${param}" in
-d)
if [ -z "$1" ]; then echo "parameter -d (days) requires a value" >&2; return 2; fi
days=$1; shift
;;
-b)
if [ -z "$1" ]; then echo "parameter -b (rsa bits) requires a value" >&2; return 2; fi
bits=$1; shift
;;
*)
pathbase="${param}"
;;
esac
done
if [ -z "$pathbase" ]; then echo "base path required" >&2; return 3; fi
openssl req -x509 -nodes -days $days -newkey rsa:$bits -keyout "${pathbase}.key" -out "${pathbase}.crt"
echo ""
echo "nginx config example: "
echo -e "\tssl_certificate ${pathbase}.crt;"
echo -e "\tssl_certificate_key ${pathbase}.key;"
echo -e "\tssl_dhparam /etc/ssl/certs/dhparam.pem;"
echo -e "\t"
echo -e "\tssl on;"
echo -e "\tssl_session_cache builtin:1000 shared:SSL:10m;"
echo -e "\tssl_protocols TLSv1 TLSv1.1 TLSv1.2;"
echo -e "\tssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;"
echo -e "\tssl_prefer_server_ciphers on;"
echo ""
}
create-openssl-dh() {
if [ $# -lt 1 ]; then
echo "Usage: create-openssl-dh [-b bits] <path (ex: dh2048.pem)>"
return 1
fi
bits=2048; pathbase=""
while [ ! -z "$1" ]; do
param="$1"; shift
case "${param}" in
-b)
if [ -z "$1" ]; then echo "parameter -b (rsa bits) requires a value" >&2; return 2; fi
bits=$1; shift
;;
*)
pathbase="${param}"
;;
esac
done
if [ -z "$pathbase" ]; then echo "path required" >&2; return 3; fi
openssl dhparam -out "${pathbase}" $bits
echo ""
echo "nginx config example: "
echo -e "\tssl_dhparam\t\t\t${pathbase};"
echo ""
}
run-in() {
if [ "_$1" == "_-v" ]; then
verbose=1
shift
else
verbose=0
fi
target_dir="$1"
shift
run_command="$@"
if [ -z "$run_command" ]; then
echo "Run command has to be specified" >&2
return 2
fi
if [ ! -d "$target_dir" ]; then
echo "Target dir \"${target_dir}\" doestn't exists" >&2
return 1
fi
curdir="$(pwd)"
for run_in_dir in $(find "$target_dir" -type d -maxdepth 1 -not -name '.*'); do
cd "$run_in_dir"
if [ $? -ne 0 ]; then
cd "$curdir"
echo "Could not enter directory \"${run_in_dir}\"" >&2
return 1
else
[ ${verbose} -eq 1 ] && echo "Running in \"${run_in_dir}\"... "
eval ${run_command}
fi
cd "$curdir"
done
}
set-session-title() {
export SESSION_TITLE="$@"
}
unset-session-title() {
export SESSION_TITLE=""
}
link-rc-scripts() {
for RC in ${SCRIPT_BASE_DIR}/rc/*rc; do
HOMERCNAME="${HOME}/.$(basename ${RC})"
echo "Linking $RC to $HOMERCNAME ..."
if [ -f "${HOMERCNAME}" ]; then
rm -f "${HOMERCNAME}" 2>&1 >/dev/null
fi
ln -s "${RC}" "${HOMERCNAME}"
done
}
update-shellrc() {
VERSIONS="$(wget --no-cache -q -O- 'https://github.com/majk1/shellrc/releases' | grep "/majk1/shellrc/archive/.*\.tar\.gz" | sed '/\/majk1\/shellrc\/archive\/refs\/tags\/.*\.tar\.gz/s/.*href="\/majk1\/shellrc\/archive\/refs\/tags\/\([0-9\.]*\)\.tar\.gz".*/\1/' | sort -Vr)"
LATEST="$(echo "$VERSIONS" | head -n 1)"
if [ "$LATEST" != "$SHELLRC_VERSION" ]; then
echo -n "Upgrade shellrc-scripts to version ${LATEST} [y/n]? "
read ANSWER
if [ "$ANSWER" != "y" ]; then
echo "Stopping upgrade. Bye"
else
wget --no-cache -q -O- "https://majk1.github.io/shellrc/installer.sh" | bash -s -- -u
fi
else
if [ ! -z "$1" ]; then
if [ "$1" == "--force" ]; then
echo -n "Reinstall shellrc-scripts version ${LATEST} [y/n]? "
read ANSWER
if [ "$ANSWER" != "y" ]; then
echo "Stopping reinstall. Bye"
else
wget --no-cache -q -O- "https://majk1.github.io/shellrc/installer.sh" | bash -s -- -u
fi
fi
else
echo "You have the latest version (${LATEST})."
echo "To force reinstall, run update-shellrc --force"
fi
fi
}
if type -p source-highlight >/dev/null 2>&1; then
alias colorcat='source-highlight -n -f esc -i '
fi
pure() {
sh -c "$*"
}
# includes
source $SCRIPT_BASE_DIR/idea.sh
source $SCRIPT_BASE_DIR/git.sh
source $SCRIPT_BASE_DIR/java.sh
source $SCRIPT_BASE_DIR/mvn.sh
source $SCRIPT_BASE_DIR/docker.sh