views:

67

answers:

6

This would be part of a reverse-engineering project.

To determine and document what a shell script (ksh, bash, sh) does, it is comfortable, if you have information about what other programs/scripts it calls.

How could one automate this task? Do you know any program or framework that can parse a shell script? This way for instance, I could recognize external command calls -- a step to the right direction.

+1  A: 

For bash/sh/ksh, I think you can easily modify their source to log what has been executed. That would be a solution.

Francis
+1  A: 

strace is your friend.

Alex Brown
A: 

bash -v script.sh ?

tomala
A: 

Bash's xtrace is your friend.

You can invoke it with:

  • set -x at the top of your script,
  • by calling your script with bash -x (or even bash --debugger -x),
  • or recursively by doing (set -x; export SHELLOPTS; your-script; )
Tobu
A: 

If you can't actually run the script, try loading it into a text editor that supports syntax highlighting for Bash. It will color-code all of the text and should help indicate what is a reserved word, variable, external command, etc.

bta
+1  A: 

How about: 1. Get a list of distinct words in that script 2. Search $PATH to find a hit for each ?

Shiva