-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions
395 lines (359 loc) · 12.7 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#################################################################################
# Everything below this line comes without any warranty. #
# Use these file at your own risk! #
#################################################################################
#
#
functions() {
# Usage: functions (without any argument)
IFS=$'\n' read -d "" -ra functions < <(declare -F)
printf '%s\n' "${functions[@]//declare -f }"
}
#
# function command-not-found-----------------------------------------------------
# call in /etc/bash.bashrc to have it available globally
#
# When command-not-found is installed, use it!
# Under Arch Linux comment in following line:
#source /usr/share/doc/pkgfile/command-not-found.bash
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then
command_not_found_handle() {
# check because c-n-f could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/bin/python /usr/lib/command-not-found -- "$1"
return $?
elif [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/bin/python /usr/share/command-not-found/command-not-found -- "$1"
return $?
else
printf "%s: Programm ist scheinbar nicht installiert oder defekt.\n" "$1" >&2
return 127
fi
}
fi
#
# function jobscount-------------------------------------------------------------
jobscount() {
local background=$(jobs -p | wc -l)
((background)) && echo -n "(${background}) "
}
#
# Find file----------------------------------------------------------------------
# Usage: ff <filename>
ff() {
find . -type f -iname '*'$*'*' -ls
}
#
# function extract---------------------------------------------------------------
# "extract <filename>" extracts all formats
# credits: urukrama, Ubuntuforums.org
extract() {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $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 ;;
*) echo "'$1' kann nicht mit extract entpackt werden!" ;;
esac
else
echo "'$1' ist keine gültige Datei!"
fi
}
#
# function __add_command_replace_alias-------------------------------------------
# This function checks if certain applications are installed and defines aliases in case of.
# This will not remove the application. But if you call it the usual way the more sophisticated version
# will be used, e.g. 'htop' instead of 'top'
# sauce: https://github.com/slomkowski/bash-full-of-colors/blob/master/bash_aliases.sh
#
__add_command_replace_alias() { if [ -x "`which $2 2>&1`" ]; then alias $1=$2; fi; }
if [ -x "`which most 2>&1`" ]; then alias less=most; export PAGER=most; fi
if [ -x "`which vim 2>&1`" ]; then export EDITOR=vim; fi
__add_command_replace_alias tail 'multitail'
__add_command_replace_alias df 'pydf'
__add_command_replace_alias traceroute 'mtr'
__add_command_replace_alias tracepath 'mtr'
__add_command_replace_alias top 'htop'
#
# function colors----------------------------------------------------------------
colors() {
local fgc fgc1 bgc bgc1 vals seq0 seq1
printf " Color escapes are %s\n" '\e[${value};...;${value}m'
printf " Values 30..37: \e[33mforeground colors\e[m\n"
printf " Values 40..47: \e[43mbackground colors\e[m\n\n"
printf " \e[36mDepending on the capability of your terminal, the value 1 gives a \e[36;1mbright or bold look\e[m\n\n"
printf " Example: \e[33;1m\\\e[33;1m = BOLD YELLOW\e[m - \e[33m\\\e[33m = NORMAL YELLOW\e[m\n\n"
# foreground colors
for fgc in {30..33}; do
# background colors
for bgc in {40..43}; do
fgc=${fgc#37} # white
bgc=${bgc#47} # black
vals="${fgc:+$fgc;}${bgc}"
vals=${vals%%;}
seq0="${vals:+\e[${vals}m}"
printf " %-9s" "${seq0:-(default)}"
printf " ${seq0}NORM\e[m"
printf " \e[${vals:+${vals+$vals;}}1mBOLD\e[m"
done
echo; echo
done
for fgc1 in {34..37}; do
for bgc1 in {44..47}; do
fgc1=${fgc1#37} # white
bgc1=${bgc1#47} # black
vals1="${fgc1:+$fgc1;}${bgc1}"
vals1=${vals1%%;}
seq1="${vals1:+\e[${vals1}m}"
printf " %-9s" "${seq1:-(default)}"
printf " ${seq1}NORM\e[m"
printf " \e[${vals1:+${vals1+$vals1;}}1mBOLD\e[m"
done
echo; echo
done
printf " \x1b[38;2;255;100;0mIf this text is brown, your terminal understands Xterm-style true-color escapes\x1b[0m\n"
}
# function colortest-------------------------------------------------------------
#
colortest() {
awk 'BEGIN{
s="/\\/\\/\\/\\Colortest/\\/\\/\\/\\"; s=s s s s s s s s;
for (colnum = 0; colnum<79; colnum++) {
r = 255-(colnum*255/79);
g = (colnum*510/79);
b = (colnum*255/79);
if (g>255) g = 510-g;
printf "\033[48;2;%d;%d;%dm", r,g,b;
printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b;
printf "%s\033[0m", substr(s,colnum+1,1);
}
printf "\n";
}'
printf " \x1b[38;2;255;100;0mIf the text is brown, your terminal understands Xterm-style true-color escapes\x1b[0m\n"
}
#
# function allcolors-------------------------------------------------------------
#
# credit to Oktay https://askubuntu.com/users/147615/oktay
# https://askubuntu.com/questions/27314/script-to-display-all-terminal-colors/279014#279014
#
allcolors() {
for x in 0 1 4 5 7 8; do for i in {30..37}; do for a in {40..47}; do echo -ne "\e[$x;$i;$a""m\\\e[$x;$i;$a""m\e[0;37;40m "; done; echo; done; done; echo "";
}
#
# Another solution is this: 'msgcat --color=test', but you need to install 'gettext': sudo apt install gettext
#
# function truecolors------------------------------------------------------------
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
truecolors() {
for fgbg in 38 48; do for color in {0..255}; do printf "\e[${fgbg};5;%sm %3s \e[0m" $color $color; if [ $((($color + 1) % 12)) == 4 ]; then echo; fi; done; echo; done; echo "";
}
#
# function showcolors256---------------------------------------------------------
#
showcolors256() {
local row col blockrow blockcol red green blue
local showcolor=_showcolor256_${1:-bg}
local white="\033[1;37m"
local reset="\033[0m"
echo -e "Set foreground color: \\\\033[38;5;${white}NNN${reset}m"
echo -e "Set background color: \\\\033[48;5;${white}NNN${reset}m"
echo -e "Reset color & style: \\\\033[0m"
echo
echo 16 standard color codes:
for row in {0..1}; do
for col in {0..7}; do
$showcolor $(( row*8 + col )) $row
done
echo
done
echo
echo 6·6·6 RGB color codes:
for blockrow in {0..2}; do
for red in {0..5}; do
for blockcol in {0..1}; do
green=$(( blockrow*2 + blockcol ))
for blue in {0..5}; do
$showcolor $(( red*36 + green*6 + blue + 16 )) $green
done
echo -n " "
done
echo
done
echo
done
echo 24 grayscale color codes:
for row in {0..1}; do
for col in {0..11}; do
$showcolor $(( row*12 + col + 232 )) $row
done
echo
done
echo
}
_showcolor256_fg() {
local code=$( printf %03d $1 )
echo -ne "\033[38;5;${code}m"
echo -nE " $code "
echo -ne "\033[0m"
}
_showcolor256_bg() {
if (( $2 % 2 == 0 )); then
echo -ne "\033[1;37m"
else
echo -ne "\033[0;30m"
fi
local code=$( printf %03d $1 )
echo -ne "\033[48;5;${code}m"
echo -nE " $code "
echo -ne "\033[0m"
}
#
#
# function wtfis-----------------------------------------------------------------
# wtfis: Shows what a given command really is. It is a combination of "type",
# "file" and "ls". Unlike "which", it does not only take $PATH into account.
# This means it works for aliases and hashes, too. (The name "whatis" was taken,
# and I did not want to overwrite "which", hence "wtfis".)
# The return value is the result of "type" for the last command specified.
#
# usage:
#
# wtfis man
# wtfis vi
#
# source: https://raw.githubusercontent.com/janmoesen/tilde/master/.bash/commands
#
wtfis()
{
local cmd=""
local type_tmp=""
local type_command=""
local i=1
local ret=0
if [ -n "$BASH_VERSION" ]; then
type_command="type -p"
else
type_command=( whence -p ) # changes variable type as well
fi
if [ $# -eq 0 ]; then
# Use "fc" to get the last command, and use that when no command
# was given as a parameter to "wtfis".
set -- $(fc -nl -1)
while [ $# -gt 0 -a '(' "sudo" = "$1" -o "-" = "${1:0:1}" ')' ]; do
# Ignore "sudo" and options ("-x" or "--bla").
shift
done
# Replace the positional parameter array with the last command name.
set -- "$1"
fi
for cmd; do
type_tmp="$(type "$cmd")"
ret=$?
if [ $ret -eq 0 ]; then
# Try to get the physical path. This works for hashes and
# "normal" binaries.
local path_tmp=$(${type_command} "$cmd" 2>/dev/null)
if [ $? -ne 0 ] || ! test -x "$path_tmp"; then
# Show the output from "type" without ANSI escapes.
echo "${type_tmp//$'\e'/\\033}"
case "$(command -v "$cmd")" in
'alias')
local alias_="$(alias "$cmd")"
# The output looks like "alias foo='bar'" so
# strip everything except the body.
alias_="${alias_#*\'}"
alias_="${alias_%\'}"
# Use "read" to process escapes. E.g. 'test\ it'
# will # be read as 'test it'. This allows for
# spaces inside command names.
read -d ' ' alias_ <<< "$alias_"
# Recurse and indent the output.
# TODO: prevent infinite recursion
wtfis "$alias_" 2>&2 | sed 's/^/ /'
;;
'keyword' | 'builtin')
# Get the one-line description from the built-in
# help, if available. Note that this does not
# guarantee anything useful, though. Look at the
# output for "help set", for instance.
help "$cmd" 2>/dev/null | {
local buf line
read -r line
while read -r line; do
buf="$buf${line/. */.} "
if [[ "$buf" =~ \.\ $ ]]; then
echo "$buf"
break
fi
done
}
;;
esac
else
# For physical paths, get some more info.
# First, get the one-line description from the man page.
# ("col -b" gets rid of the backspaces used by OS X's man
# to get a "bold" font.)
(COLUMNS=10000 man "$(basename "$path_tmp")" 2>/dev/null) | col -b | \
awk '/^NAME$/,/^$/' | {
local buf=""
local line=""
read -r line
while read -r line; do
buf="$buf${line/. */.} "
if [[ "$buf" =~ \.\ $ ]]; then
echo "$buf"
buf=''
break
fi
done
[ -n "$buf" ] && echo "$buf"
}
# Get the absolute path for the binary.
local full_path_tmp="$(
cd "$(dirname "$path_tmp")" \
&& echo "$PWD/$(basename "$path_tmp")" \
|| echo "$path_tmp"
)"
# Then, combine the output of "type" and "file".
local fileinfo="$(file "$full_path_tmp")"
echo "${type_tmp%$path_tmp}${fileinfo}"
# Finally, show it using "ls" and highlight the path.
# If the path is a symlink, keep going until we find the
# final destination. (This assumes there are no circular
# references.)
local paths_tmp=("$path_tmp")
local target_path_tmp="$path_tmp"
while [ -L "$target_path_tmp" ]; do
target_path_tmp="$(readlink "$target_path_tmp")"
paths_tmp+=("$(
# Do some relative path resolving for systems
# without readlink --canonicalize.
cd "$(dirname "$path_tmp")"
cd "$(dirname "$target_path_tmp")"
echo "$PWD/$(basename "$target_path_tmp")"
)")
done
local ls="$(command ls -fdalF "${paths_tmp[@]}")"
echo "${ls/$path_tmp/$'\e[7m'${path_tmp}$'\e[27m'}"
fi
fi
# Separate the output for all but the last command with blank lines.
[ $i -lt $# ] && echo
let i++
done
return $ret
}