This repository has been archived by the owner on Jun 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
.bash_aliases
560 lines (494 loc) · 12 KB
/
.bash_aliases
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# enable color support
if [ -x /usr/bin/dircolors ]
then
if [ -r $HOME/.dircolors ]
then
eval "$(dircolors -b $HOME/.dircolors)"
else
eval "$(dircolors -b)"
fi
alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# some more ls aliases
alias l='ls' # Default
alias la='ls -hlA' # Full info
alias lsd='ls -hlt' # List sorted by modification time
# Update everything
upgrade()
{
sudo sh -c "apt-get update && apt-get -y dist-upgrade && apt-get -y autoremove && apt-get autoclean"
}
# Play ISO file of DVD
alias play-iso-dir='mplayer -dvd-device dvd:// -slang en' # dir
# Subversion
svndiff()
{
svn diff ${1+"$@"} | colordiff
}
cvsignore2svn()
{
# $@: Directories to search through recursively
local dir_path
while IFS= read -r -d '' -u 9
do
dir_path="$(dirname -- "$REPLY")x"
dir_path="${dir_path%x}"
svn propset svn:ignore -F "$REPLY" -- "$dir_path"
svn remove "$REPLY"
done 9< <( find ${1+"$@"} -name .cvsignore -print0 )
}
diff_ignore_moved_lines()
{
# @param $1: Diff file from either plain `diff` or
# `svn diff --diff-cmd diff -x -b`
while IFS= read -r -u 9
do
contents="${REPLY:2}"
if [[ "${REPLY:0:1}" =~ [\<\>] ]]
then
count1="$(grep -cFxe "< $contents" "$1")"
count2="$(grep -cFxe "> $contents" "$1")"
if [[ "$count1" -eq "$count2" ]]
then
# Line has been moved; skip it.
continue
fi
fi
printf %s "$REPLY"
printf '\n'
done 9< "$1"
}
# Diff
wdiffc()
{
if [ -z "${2-}" ]
then
return 2
fi
wdiff -w "$(tput bold;tput setaf 1)" -x "$(tput sgr0)" -y "$(tput bold;tput setaf 2)" -z "$(tput sgr0)" "$@"
}
# Git
mkgithub()
{
if [ ! -e "$1" ]
then
mkdir "$1"
fi
cd "$1"
if [ ! -e '.git' ]
then
git init
fi
local repo="$(basename -- "$1")"
git remote add origin "git@github.com:l0b0/${repo}.git"
git config push.default matching && \
git config branch.master.remote origin && \
git config branch.master.merge refs/heads/master && \
git config merge.conflictstyle diff3
}
gitar()
{
# @param $1: Commit-like
git archive --format=tar --prefix="${2}/" "$1" | gzip
}
git_send_commits()
{
# Send all commits ahead of origin
git format-patch -C -M origin
git send-email --confirm=always [0-9][0-9][0-9][0-9]-*.patch
}
# Find
find_date_sorted()
{
# Ascending, by ISO date
while IFS= read -r -d '' -u 9
do
cut -d ' ' -f 3- <<< "$REPLY"
done 9< <(find ${1+"$@"} -printf '%TY-%Tm-%Td %TH:%TM:%TS %p\0' | sort -z)
}
quote_shell()
{
# Ensure that the output is escaped so that each line corresponds to a NUL-
# separated entry
while IFS= read -r -d ''
do
printf %q "$REPLY"
printf '\n'
done
}
substring()
{
# Extract substring with positive or negative indexes
# @param $1: String
# @param $2: Start (default start of string)
# @param $3: Length (default until end of string)
local -i strlen="${#1}"
local -i start="${2-0}"
local -i length="${3-${#1}}"
if [[ "$start" -lt 0 ]]
then
let start+=$strlen
fi
if [[ "$length" -lt 0 ]]
then
let length+=$strlen
let length-=$start
fi
if [[ "$length" -lt 0 ]]
then
return
fi
printf %s "${1:$start:$length}"
}
find_grouped()
{
# Outputs files (not directories) recursively, with an empty line between
# directories.
while [ -n "${1-}" ]
do
target="${1%%/}/"
if [ ! -d "$target" ]
then
echo "${target}: No such directory"
return 1
fi
while IFS= read -rd '' dir
do
files_found=0
while IFS= read -rd '' path
do
files_found=1
ls -- "$path"
done < <(find "$dir" -maxdepth 1 \( -type f -o -type l \) -print0 | sort -z)
if [ $files_found -eq 1 ]
then
echo
fi
done < <(find "$target" -mindepth 1 -type d -print0 | sort -z)
shift
done
}
path_common()
{
if [ -z "${2-}" ]
then
return 2
fi
# Remove repeated slashes
for param
do
param=$(printf %s. "$1" | tr -s "/")
set -- "$@" "${param%.}"
shift
done
common_path=$1
shift
for param
do
while case ${param%/}/ in "${common_path%/}/"*) false;; esac; do
new_common_path=${common_path%/*}
if [ "$new_common_path" = "$common_path" ]
then
return 1 # Dead end
fi
common_path=$new_common_path
done
done
printf %s "$common_path"
}
compare_dirs()
{
# Shows which files and directories exist in one directory but not both
if [ $# -ne 2 ]
then
echo "Usage: compare_dirs dir1 dir2" >&2
return 2
fi
for path
do
if [ ! -d "$path" ]
then
echo "Not a directory: $path" >&2
return 1
fi
done
comm -3 \
<(cd -- "$1" && find . -printf '%P\0' | sort -z | quote_shell) \
<(cd -- "$2" && find . -printf '%P\0' | sort -z | quote_shell)
}
count_files()
{
local -i file_count=0
while IFS= read -r -d ''
do
let file_count++
done < <( find ${1+"$@"} -type f -print0 )
printf %d $file_count
}
# Bash
bash_timeout()
{
# Set the idle timeout before the shell will be closed automatically
local timeout=$1
timeout=${timeout//d/*24h}
timeout=${timeout//h/*60m}
timeout=${timeout//m/*60}
timeout=${timeout//s/}
export TMOUT=$(($timeout))
}
# Perl
perl_module_version()
{
local version
for module
do
version=$(perl -M${module} -e "print \$${module}::VERSION")
if [ -n "$version" ]
then
echo "$module $version"
else
echo "Unknown module $module" >&2
return 1
fi
done
}
perl_module_files()
{
perl -MFile::Find=find -MFile::Spec::Functions -Tlwe 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC'
}
perl_modules()
{
perl -MExtUtils::Installed -e'my $inst = ExtUtils::Installed->new();print $_, $/ for $inst->modules'
}
perl_modules_test()
{
local -i error=0
while IFS= read -r -u 9
do
perl_module_version "$REPLY" 2>&1 >/dev/null || let error=1
done 9< <( perl_modules )
return $error
}
# String handling
shortest()
{
# Print only the (first) shortest line
awk '(NR == 1 || length < length(line)) { line = $0 } END { print line }'
}
longest()
{
# Print only the (first) longest line
awk '(NR == 1 || length > length(line)) { line = $0 } END { print line }'
}
longer()
{
# Print any lines in $2... which are more characters than $1.
if [ $# -lt 2 ]
then
echo 'Synopsis: longer NUMBER FILE...' >&2
return 2
fi
local -ri length="$1"
shift
local -i line_number
for path
do
line_number=1
while IFS= read -r -d $'\n'
do
if [ ${#REPLY} -gt $length ]
then
printf %q $path : $line_number :... ${REPLY:${length}}
printf '\n'
fi
let line_number++
done < "$path"
done
}
# GNU Make
make_targets()
{
targets="$(make --print-data-base --question | grep '^[^.%][-A-Za-z0-9_]*:' | cut -d : -f 1 | sort -u)"
longest_line="$(longest <<< "$targets")"
columns=$(bc <<< "${COLUMNS-80} / (${#longest_line} + 1)")
pr --omit-pagination --width=${COLUMNS-80} --columns=$columns <<< "$targets"
}
locale_value()
{
# Return the highest priority language variable available
# Based on http://mywiki.wooledge.org/BashFAQ/098
# To print which variable was used, uncomment the `echo` line
# $1: Optional locale category variable name (see `man locale`)
local varname=LANG # Fallback
if [[ "${1-}" =~ ^LC_[A-Z]+$ ]] && declare -p "${1-}" >/dev/null 2>&1
then
varname="$1"
fi
if [ -n "${LC_ALL+defined}" ]
then
varname=LC_ALL
fi
if [ -n "${LANGUAGE+defined}" -a "${LANG-}" != C ]
then
varname=LANGUAGE
fi
#echo "$varname" >&2
printf "${!varname}"
}
schroedinger()
{
# Succeed or fail randomly
return $((RANDOM%2))
}
ltrim()
{
# Left-trim $IFS from stdin as a single line
# $1: Line separator (default NUL)
local trimmed
while IFS= read -r -d "${1-}" -u 9
do
if [ -n "${trimmed+defined}" ]
then
printf %s "$REPLY"
else
printf %s "${REPLY#"${REPLY%%[!$IFS]*}"}"
fi
printf "${1-\x00}"
trimmed=true
done 9<&0
if [[ $REPLY ]]
then
# No delimiter at last line
if [ -n "${trimmed+defined}" ]
then
printf %s "$REPLY"
else
printf %s "${REPLY#"${REPLY%%[!$IFS]*}"}"
fi
fi
}
rtrim()
{
# Right-trim $IFS from stdin as a single line
# $1: Line separator (default NUL)
local previous last
while IFS= read -r -d "${1-}" -u 9
do
if [ -n "${previous+defined}" ]
then
printf %s "$previous"
printf "${1-\x00}"
fi
previous="$REPLY"
done 9<&0
if [[ $REPLY ]]
then
# No delimiter at last line
last="$REPLY"
printf %s "$previous"
if [ -n "${previous+defined}" ]
then
printf "${1-\x00}"
fi
else
last="$previous"
fi
right_whitespace="${last##*[!$IFS]}"
printf %s "${last%$right_whitespace}"
}
trim()
{
# Trim $IFS from individual lines
# $1: Line separator (default NUL)
ltrim ${1+"$@"} | rtrim ${1+"$@"}
}
trs()
{
# Translate strings
# $1: Original string
# $2, $4, ...: Search strings
# $3, $5, ...: Replacement strings
local string=$1
shift
while [ $# -gt 0 ]
do
string="$(sed -e "s/$1/$2/g" <<< "$string")"
shift 2
done
printf "$string"
}
zsed()
{
# Replace within tarballs
# $1: Replacement string
# $2...: tar gzip files
if [[ "${2+defined}" != defined ]]
then
return 2
fi
local replacement="$1"
shift
local -i exit_code
for path
do
if [[ ! -r "${path}" ]]
then
exit_code=2
continue
fi
full_pathx="$(readlink -fn -- "$path"; echo x)"
full_path="${full_pathx%x}"
tmp="$(mktemp -d)"
cd "$tmp"
tar -xzf "$full_path"
while IFS= read -r -d '' -u 9
do
sed -i -e "$replacement" "$REPLY"
done 9< <( find . -type f -print0 )
tar -czf "$full_path" .
cd - >/dev/null
done
return ${exit_code-0}
}
collapse()
{
# Collapse lines based on first column
local name old_name value
local first=1
while read -r name value
do
[ "$name" != "${old_name-}" -a "${first+defined}" != defined ] && printf "${IFS: -1}"
[ "$name" != "${old_name-}" ] && printf %s "$name"
[ -n "$value" ] && printf %s "${IFS:0:1}"
printf %s "$value"
old_name="$name"
unset first
done
if [[ $name ]]
then
[ "$name" != "${old_name-}" -a "${first+defined}" != defined ] && printf "${IFS: -1}"
[ "$name" != "${old_name-}" ] && printf %s "$name"
[ -n "$value" ] && printf %s "${IFS:0:1}"
printf %s "$value"
else
[ "${first+defined}" != defined ] && printf "${IFS: -1}"
fi
}
jail()
{
# @param $1: Chroot jail directory (default /var/jail/*user*)
# @param $2: Login (default current user)
local -r login="${2-$USER}"
local -a default_path=("/var/jail/*${login}*")
local -r path=${1-${default_path[0]}}
unset default_path
sudo chroot "$path" su - "$login"
}
if [ -r "$HOME/.bash_aliases_local" ]
then
source "$HOME/.bash_aliases_local"
fi
true