开发者

How to retrieve all tools used in shell script

开发者 https://www.devze.com 2023-04-07 08:37 出处:网络
I\'ve got 开发者_StackOverflowbunch of shell scripts that used some command and other tools. So is there a way I can list all programs that the shell scripts are using ?

I've got 开发者_StackOverflowbunch of shell scripts that used some command and other tools.

So is there a way I can list all programs that the shell scripts are using ? Kind of way to retrieve dependencies from the source code.


Uses sed to translate pipes and $( to newlines, then uses awk to output the first word of a line if it might be a command. The pipes into which to find potiential command words in the PATH:

sed 's/|\|\$(/\n/g' FILENAME | 
awk '$1~/^#/ {next} $1~/=/ {next} /^[[:space:]]*$/ {next} {print $1}' | 
sort -u | 
xargs which 2>/dev/null


One way you can do it is at run time. You can run bash script in debug mode with -x option and then parse it's output. All executed commands plus their arguments will be printed to standard output.


While I have no general solution, you could try two approaches:

  1. You might use strace to see which programs were executed by your script.
  2. You might run your program in a pbuilder environment and see which packages are missing.


Because of dynamic nature of the shell, you cannot do this without running a script.

For example:

TASK="cc foo.c"
time $TASK

This will be really hard to determine without running that cc was called even in such trivial example as above.

In a runtime, you can inspect debug output sh -x myscript as pointed out by thiton (+1) and ks1322 (+1). You can also you tool like strace to catch all exec() syscalls.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号