Mic*_*sch 7 shell scripting graph-visualization
我已经交了一个项目,包括几十个(可能超过100个,我没有计算)bash脚本.大多数脚本至少调用另一个脚本.我想得到一个调用图的等价物,其中节点是脚本而不是函数.
有没有现成的软件可以做到这一点?
如果没有,是否有人对如何做到这一点有聪明的想法?
我能想出的最佳计划是枚举脚本并检查基本名称是否唯一(它们跨越多个目录).如果有重复的基本名称,则哭,因为脚本路径通常以变量名称保存,因此您可能无法消除歧义.如果它们是唯一的,那么grep脚本中的名称并使用这些结果来构建图形.使用一些工具(建议?)来可视化图形.
建议?
这就是我最终的做法(免责声明:其中很多都是黑客行为,所以如果您打算长期使用它,您可能需要清理)...
假设: - 当前目录包含所有有问题的脚本/二进制文件。- 用于构建图表的文件位于子目录 call_graph 中。
创建脚本 call_graph/make_tgf.sh:
#!/bin/bash
# Run from dir with scripts and subdir call_graph
# Parameters:
# $1 = sources (default is call_graph/sources.txt)
# $2 = targets (default is call_graph/targets.txt)
SOURCES=$1
if [ "$SOURCES" == "" ]; then SOURCES=call_graph/sources.txt; fi
TARGETS=$2
if [ "$TARGETS" == "" ]; then TARGETS=call_graph/targets.txt; fi
if [ ! -d call_graph ]; then echo "Run from parent dir of call_graph" >&2; exit 1; fi
(
# cat call_graph/targets.txt
for file in `cat $SOURCES `
do
for target in `grep -v -E '^ *#' $file | grep -o -F -w -f $TARGETS | grep -v -w $file | sort | uniq`
do echo $file $target
done
done
)
Run Code Online (Sandbox Code Playgroud)
然后,我运行了以下命令(我最终执行了仅脚本版本):
cat /dev/null | tee call_graph/sources.txt > call_graph/targets.txt
for file in *
do
if [ -d "$file" ]; then continue; fi
echo $file >> call_graph/targets.txt
if file $file | grep text >/dev/null; then echo $file >> call_graph/sources.txt; fi
done
# For scripts only:
bash call_graph/make_tgf.sh call_graph/sources.txt call_graph/sources.txt > call_graph/scripts.tgf
# For scripts + binaries (binaries will be leaf nodes):
bash call_graph/make_tgf.sh > call_graph/scripts_and_bin.tgf
Run Code Online (Sandbox Code Playgroud)
然后我在yEd中打开生成的 tgf 文件,并让 yEd 进行布局(布局 -> 分层)。我另存为 graphml 以将可手动编辑的文件与自动生成的文件分开。
我发现图中某些节点没有帮助,例如到处调用的实用程序脚本/二进制文件。因此,我从源/目标文件中删除了它们,并根据需要重新生成,直到我喜欢该节点集。
希望这对某人有帮助...