forked from HariSekhon/DevOps-Bash-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_bash_duplicate_defs.sh
executable file
·121 lines (108 loc) · 3.38 KB
/
check_bash_duplicate_defs.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
#!/usr/bin/env bash
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2019-08-02 13:30:09 +0100 (Fri, 02 Aug 2019)
#
# https://github.com/harisekhon/bash-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/harisekhon
#
# Checks for duplicate function or alias definitions within a given collection of bash scripts - useful for checking across many .bash.d/ includes
#
# This isn't caught by shellcheck - for example it's common to alias 'p' to ping, but I also do this for 'kubectl get pods'
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
srcdir="$(dirname "$0")"
# shellcheck disable=SC1090
. "$srcdir/lib/utils.sh"
section "Checking for Bash duplicate definitions (functions, aliases)"
start_time="$(start_timer)"
check_duplicate_defs(){
echo "Checking for duplicate definitions in the following files:"
echo
for x in "$@"; do
echo "$x"
done
echo
check_duplicate_functions "$@"
check_duplicate_aliases "$@"
check_duplicate_aliases_vs_functions "$@"
}
get_functions(){
grep -Eh '^[[:space:]]*(function[[:space:]]+)?[[:alnum:]-]+[[:space:]]*\(' "$@" 2>/dev/null |
grep -Ev '^[[:space:]]*for[[:space:]]*\(\(' |
sed 's/^[[:space:]]*\(function[[:space:]]*\)*//; s/[[:space:]]*(.*//' |
sort
}
get_aliases(){
grep -Eho '^[[:space:]]*alias[[:space:]]+[[:alnum:]]+=' "$@" 2>/dev/null |
sed 's/^[[:space:]]*alias[[:space:]]*//; s/=$//' |
sort
}
check_duplicate_functions(){
echo "* Checking for duplicate function definitions"
echo
local function_dups
set +o pipefail
function_dups="$(get_functions "$@" | uniq -d)"
if [ -n "$function_dups" ]; then
echo "Duplicate functions detected across input files!"
echo
for x in $function_dups; do
grep -Eno "^[[:space:]]*(function[[:space:]]+)?${x}[[:space:]]*\\(" "$@" 2>/dev/null || :
done
echo
exit 1
fi
}
check_duplicate_aliases(){
echo "* Checking for duplicate alias definitions"
echo
local alias_dups
set +o pipefail
alias_dups="$(get_aliases "$@" | uniq -d)"
if [ -n "$alias_dups" ]; then
echo "Duplicate aliases detected across input files!"
echo
for x in $alias_dups; do
grep -Eno "^[[:space:]]*alias[[:space:]]+${x}=" "$@" 2>/dev/null || :
done
echo
echo
exit 1
fi
}
check_duplicate_aliases_vs_functions(){
echo "* Checking for duplicate alias vs function definitions"
echo
local defs
local duplicate_defs
defs="$(get_functions "$@")
$(get_aliases "$@")"
duplicate_defs="$(sort <<< "$defs" | uniq -d)"
if [ -n "$duplicate_defs" ]; then
echo "Duplicate function vs alias definitions detected across input files!"
echo
for x in $duplicate_defs; do
grep -Eno -e "^[[:space:]]*alias[[:space:]]+${x}=" \
-e "^[[:space:]]*(function[[:space:]]+)?${x}[[:space:]]*\\(" \
"$@" 2>/dev/null || :
done
echo
echo
exit 1
fi
}
if [ $# -gt 0 ]; then
check_duplicate_defs "$@"
else
check_duplicate_defs "$srcdir/.bashrc" "$srcdir"/.bash.d/*.sh
fi
time_taken "$start_time"
section2 "No duplicate bash definitions found"
echo