How to get all the tools used in a shell script

I have a bunch of shell scripts that used some commands and other tools.

So, is there a way to list all the programs that use shell scripts? Type of method for obtaining dependencies on source code.

+7
source share
4 answers

Uses sed to translate pipes and $( to new lines, then uses awk to output the first word of the line if it can be a command. Pipes in which to find the potiential command words in PATH:

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

One way to do this is at run time. You can run the bash script in with the -x option and then parse its output. All executed commands plus their arguments will be printed to standard output.

+2
source

As long as I don't have a general solution, you can try two approaches:

  • You can use strace to see which programs have been executed by your script.
  • You can run your program in the pbuilder environment and see which packages are missing.
+1
source

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

For example:

 TASK="cc foo.c" time $TASK 

It will be very difficult to determine without starting up that cc was called even in such a trivial example as above.

At runtime, you can check the debug output of sh -x myscript , as indicated by titon (+1) and ks1322 (+1). You can also use the strace tool to find all exec() system calls.

0
source

All Articles