-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbash-function-tree
More file actions
executable file
·46 lines (40 loc) · 1.17 KB
/
bash-function-tree
File metadata and controls
executable file
·46 lines (40 loc) · 1.17 KB
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
#!/usr/bin/env bash
# This script will read a bash script and produce
# a list of functions and their dependencies
# in a tree-like list.
#
# Argument: name of the bash script you want to analyze.
#
# Limitations:
# * if you have non-static function names like
# myfunction${number}
# this script will not understand it.
# * It's not built for speed. Run time will increase
# quadratically over the number of functions.
# (40 functions may take ~10 seconds)
script="$1"
if [ ! -f "$script" ] ; then
echo 1>&2 "Please provide the filename of a bash script."
exit 1
fi
function_list=$(grep '()\s{' "$script" \
| sed -e 's/().*{.*//' \
| sed -e 's/\s*function\s*//' \
| egrep -v '\s*#' \
| grep -v '=' )
echo -n "Number of functions:"
# The grep is to filter out empty lines
echo "$function_list" | grep . | wc -l
echo
for caller in $function_list ; do
echo "$caller"
for called in $function_list ; do
if [[ $caller == $called ]] ; then
continue
fi
awk "/^$caller\\(\\)/ {flag=1} flag; /^\}$/ {flag=0}" "$script" \
| grep -o "$called" \
| sort | uniq \
| sed -e 's/^/ └─ /'
done
done