Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine function and file dependency of a shell script?

Tags:

shell

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.

like image 314
mathtick Avatar asked Oct 13 '10 18:10

mathtick


People also ask

What is PID of shell script?

As a child process of the main shell, a subshell executes a list of commands in a shell script as a batch (so-called "batch processing"). In some cases, you may want to know the process ID (PID) of the subshell where your shell script is running. This PID information can be used under different circumstances.

What is #$ in shell script?

This answer is not useful. Show activity on this post. #$ does "nothing", as # is starting comment and everything behind it on the same line is ignored (with the notable exception of the "shebang"). $# , as you had it, prints the number of arguments passed to a shell script (like $* prints all arguments).

How do I view the contents of a .sh file?

There are many ways to display a text file in a shell script. You can simply use the cat command and display back output on screen. Another option is to read a text file line by line and display back the output. In some cases you may need to store output to a variable and later display back on screen.

How function works shell script?

A function is a block of code that carries out a certain activity. A function can be called and reused. In shell scripting, functions are analogous to other programming languages' subroutines, procedures, and functions. Your function's name is function_name , and you'll call it by that name throughout your scripts.


1 Answers

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:

$ cat foo.sh
#!/usr/bin/env bash

touch /tmp/foon
$ chmod +x foo.sh
$ strace -f -e execve,access,open,stat -o foo.trace ./foo.sh
$ cat foo.trace
32176 execve("./foo.sh", ["./foo.sh"], [/* 42 vars */]) = 0
32176 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
32176 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
32176 open("/usr/local/lib/tls/x86_64/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
...
32176 execve("/bin/bash", ["bash", "./foo.sh"], [/* 42 vars */]) = 0
...
32177 execve("/usr/bin/touch", ["touch", "/tmp/foon"], [/* 41 vars */]) = 0
32177 open("/tmp/foon", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3
...
32176 --- SIGCHLD (Child exited) @ 0 (0) ---
$ 

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.

like image 67
Jim Blandy Avatar answered Nov 15 '22 17:11

Jim Blandy