列出 shell 脚本使用的所有程序

bas*_*lab 7 shell bash audit

我试图找出一种方法来列出脚本在运行时将使用的所有程序,而无需实际运行它。

我写了这些快速而肮脏的单行:

# fill an array with all the useful words except variables, options, brackets, quotes
readarray -t list <<<$( grep -v '^#' script.sh | sed 's/[0-9a-zA-Z_\-]*=//g ; s/\${.*}//g ; s/\$(//g ; s/[)'\"\'\`']//g ; s/ --*.//g ' )

# for every word in array show info with `type' and clean the output again
for p in "${list[@]}" ; do type "${p}" ; done 2>&1 | grep -v -e '^bash:' -e 'shell keyword' -e 'shell builtin' | sort | uniq | sed 's/^.* //g ; s/[\(\)]//g'
Run Code Online (Sandbox Code Playgroud)

我认为存在的问题是:

  1. 如果程序没有安装,`type' 会失败
  2. 这里的文档可以包含可能是程序的关键字...
  3. 如果脚本写得不好,难度可能会增加(`shellcheck' 可能有用)
  4. 不跟踪外部配置文件和函数库(见ilkkachu注释)

有什么更好的解决办法吗?

drl*_*drl 1

在线程https://www.unix.com/shell-programming-and-scripting/268856-how-pre-check-scrutinize-all-my-shell-scripts.html的帖子 #16 中,我发布了 150 行 perl脚本p1.txt这可能是一个有用的起点。我还添加了一个指向更完整、更复杂的 shell 解析器的链接

最好查看整个线程——也许其他一些观点也可能令人感兴趣。

最美好的祝愿...干杯,drl