tags:

views:

27

answers:

1

I'm looking for a way of processing a shell script to determine:

  1. which commands, scripts or functions are called in the script.
  2. 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.

A: 

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.

Roman Cheplyaka
Very interesting. I will have a better look at Loker when I have time. Properly parsing code for files and executables is quite complex indeed ... I was fantasizing that might be some way to do this by running the code using UNIX to track file access. I don't really know anything about this ... hence the fantasy.
mathtick
@mathtick "I was fantasizing that might be some way to do this by running the code using UNIX to track file access." This is definitely possible, use `strace` for this. The problem is you need to actually run code (dynamic vs static analysis) and you won't see all the possible commands/files -- only those which were executed/accessed during this particular run.
Roman Cheplyaka
@Roman Thanks, that's looks like a good starting point. Also some combination of ptree and pfiles may be a useful starting point for dynamic analysis. The issue of testing all cases (rather than only those that happen to run in a particular instance) is an important one for the code I'm looking at right now.
mathtick