I'm trying to figure out a method to list all the programs that a script will use when it will be run, without actually running it.
I've written these quick and dirty oneliners:
# 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'
I think the problems are:
- If the program it is not installed, `type' will fail
- Here documents can contain keywords that could be programs...
- If the script is not well written, the difficulty could increase (`shellcheck' could be useful)
- External configuration files and function libraries are not tracked (see ilkkachu comment)
Any better solution?