I'm looking for a way of processing a shell script to determine:
- which commands, scripts or functions are called in the script.
- which files are accessed by the script (r or w) .
It doesn't need to recurse down through the dependencies, just list what it runs directly. I could probably write something that does this myself but it must have been done before ... I'm just not finding it.
You just cannot do that in such a dynamic language, your static analysis tool will not be reliable and miss a number of dependencies. Consider the following code:
This is not a convoluted example; I recently added to a production environment a more elaborated version of this with parameters.
If you really need static analysis then you should not be using a dynamic language in the first place, they are simply incompatible with each other. The same holds for every other functional language where functions are passed as arguments: static analysis cannot practically predict the values of arguments.
Even in a very static and simple language like Java you can still dodge static analysis if you try hard and you use reflection. However reflection is cumbersome by design and not in wide use, so analysis of Java is very useful in practice: see Eclipse etc..
It will be a feature of the Loker project which I currently develop. For now, the parser is almost complete and you may implement a reasonable approximation of what you want on top of it. However, in general this task is very complex, because the name of the command may result from variable expansion, field splitting etc.
If you describe what do you need this for and what kind of scripts are you going to parse I will be able to say how much of your needs Loker can satisfy by now.
As alternative option, some versions of bash have
--rpm-requires
option, which also does something similar.You can use 'strace' to run a script and see everything the script and its subprocesses do, including looking for and opening files. For example:
I've trimmed a lot of the other activity going on there (opening system libraries; looking up locale data; and much more). Check out 'man strace' for details on what the options mean; -f, -o, and -e are the ones I use most often.