Shell Script Dependency/Tracer


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Shell Script Dependency/Tracer
# 1  
Old 03-10-2008
Shell Script Dependency/Tracer

Hello All,

We have a very old system at hand in which there are hundreds of shell scripts that use other shell scripts, all on the same server. There are several that are not used at all as well. In short, it's an unmanaged system thats been lying around for many years, and it needs to be cleaned up.

One of the ways I've been thinking of is to build a script that shows the plan of execution, given a seed script. For example, if a script A calls B and C and B calls M and N, then the given a seed of A, the script produces something like:

A
++B
++++M
++++N
++C

The intent of course is to identify which scripts are in use, and which ones are not.

Alternatively, I have considered checking the access timestamp using the -a option of ls to identify the script not being used for a long time. However, it does not give me the results I need.

Of course, I welcome other solutions to this problem and your experience if you've faced similar situations.
# 2  
Old 03-10-2008
Create an exported variable in the top level script A and in every script that A calls expand that variable by appending a plus to it. Echo that variable in every called script to display its level in the call sequence. Repeat this process for every called script.

A
Code:
#!/usr/bin/ksh

export NUM_OF_PLUS="+"

# script A calls scripts B and C at this step
B
C

B
Code:
#!/usr/bin/ksh

export NUM_OF_PLUS="+"

# display the called level of this script
echo ${NPLUS}$0

# script B calls scripts M and N at this step
M
N

C
Code:
#!/usr/bin/ksh

export NUM_OF_PLUS="+"

# display the called level of this script
echo ${NPLUS}$0

M
Code:
#!/usr/bin/ksh

export NUM_OF_PLUS="+"

# display the called level of this script
echo ${NPLUS}$0

N
Code:
#!/usr/bin/ksh

export NUM_OF_PLUS="+"

# display the called level of this script
echo ${NPLUS}$0

Now executing A displays the calling heirarchy of the scripts.
$ A
A
++B
+++M
+++N
++C
# 3  
Old 03-11-2008
Thanks shamrock..

The basic objective here is to find out which scripts are being used and which ones are not, by having a trace of execution without actually executing (bypassing the control structures as well). With the solution you suggest, I need to have a prior knowledge of the same, to modify the scripts accordingly. Morever, there are hundreds fo scripts that are actually executed, and several hundred just lying around, with no purpose, which I want to weed out.

What I'm thinking is to build a recursive parser for shell script, but since I'm not at a liberty of time, I need to explore other solutions, even if they produce approximate results.
# 4  
Old 03-11-2008
Can you write a single script that will examine all of your scripts one at a time and create an output file where each record has two fields, the first field is the name of the script, and the second is the name of the called script. So that if script A calls B and C, the output would look like:
A B
A C

When you are finished you can sort by field two and get a where used list.
Second thought.
Copy all the scripts into a single directory:

list=`ls *`
for entry in $list
do
grep $entry * >>output
done

I've been working all night so if it doesn't work oh well.
# 5  
Old 03-11-2008
How about this:

Add the following line to every single shell script (at the top, right after the shebang line, in case the script halts execution somewhere due to a conditional):

Code:
echo -e "$0\t$(date)" >> scripts.log

This way, you'll end up with a log file which will show you which scripts were executed and when. This doesn't directly address your problem of finding out which scripts call which, but you can do that with some fancy grep work. And in any case, I believe the whole point of that was to make sure you weren't leaving unused scripts out there, and this will identify all used scripts.

Note: the $0 in a bash script will be replaced with the name of the current command. If you run that from the command line (outside of a script), it will simply return "bash."

ShawnMilo
# 6  
Old 03-12-2008
Thanks jgt and ShawnMilo.

jgt: he solution will work, and I'm exploring this option.

ShawnMilo: Your solution is similar to shamrock's conceptually. I'm slightly wary of this solution because it'll involve a lot of manual work. Thanks anyway!

At this stage I'm also exploring the option of actually executing the code and capturing trace (only to EXEC and FORKs) on unix level, to see what scripts get exec'd, which will in effect tell me what commands got executed by shell(s)

I again thank everyone for reading and specially to those who took out time to reply.
# 7  
Old 03-12-2008
Quote:
Originally Posted by ag79
Thanks jgt and ShawnMilo.

jgt: he solution will work, and I'm exploring this option.

ShawnMilo: Your solution is similar to shamrock's conceptually. I'm slightly wary of this solution because it'll involve a lot of manual work. Thanks anyway!

At this stage I'm also exploring the option of actually executing the code and capturing trace (only to EXEC and FORKs) on unix level, to see what scripts get exec'd, which will in effect tell me what commands got executed by shell(s)

I again thank everyone for reading and specially to those who took out time to reply.
Have a custom script call all the others. Then continuously track the process tree of the custom script.
Check man ps for tree options. Minimally have something like
Code:
   ps -ejH
   ps axjf

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Processing Dependency Printing

Gurus, I came across a typical requirement where the input is like- TRANS FIRM DEPT CUST TRANS CUST TRANS DEPT FIRM CUST & the expected Output is- CUST DEPT FIRM TRANS This is basically a dependency list for processing the tables where FIRM is feeding data to TRANS... (3 Replies)
Discussion started by: kapeeljoshi
3 Replies

2. HP-UX

Library dependency coming out of nowhere

I'm building on a HP-UX ia64 system. During building, I'm getting an error ld: Can't find dependent library "libnnz10.so" Nowhere in my makefile do I link with this library. This library is there in the directory /databases/oracle10.2.0_64BIT/lib. I'm having two source files ora8ibulk.cpp... (1 Reply)
Discussion started by: old_as_a_fossil
1 Replies

3. UNIX for Dummies Questions & Answers

make -j dependency

Hi, I had a make file, something like that, all: cd dir1; make cd dir2; make the problem is the makefile under dir2 need some objs from dir1, so I need to set some dependency let dir2 run only after the dir1 run is done. how to set the dependency? Thanks. peter (2 Replies)
Discussion started by: laopi
2 Replies

4. Programming

Fortran dependency checking

Hello, I'm creating an add-on for a large piece of software written in Fortran. In my directory I reference subroutines in other directories, which in turn reference more subroutines. I'm running into trouble because on occasion the developers of the main software change some sub calls. ... (2 Replies)
Discussion started by: kudude
2 Replies

5. Post Here to Contact Site Administrators and Moderators

Tynt Tracer Must Die

First of all, I want to thank everyone who runs this forum for the fine job they've done. While I myself have not yet had any need for help, I have enjoyed and learned while helping others. Due diligence disclaimer: I searched for a discussion on this issue, using "tynt" and "copy paste", but... (11 Replies)
Discussion started by: alister
11 Replies

6. Infrastructure Monitoring

Weird dependency problem!

Hi, I want to install net-snmp-devel package but i have following dependecy problem. It's very odd, i don't get it. One of packages is depended on the other one, the other one is depended on the previous one as well. :S :S Could you help me please? Here are the steps: # ls -l total... (4 Replies)
Discussion started by: oduth
4 Replies

7. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

8. Solaris

Dependency problem

Hi all am new to solaris ............ i installed amanda client pkg that time am getting lots of dependency problem.......... is there any Yum server like things in solaris Regards ' prAn (8 Replies)
Discussion started by: pran
8 Replies

9. UNIX for Dummies Questions & Answers

process dependency

how to find all the processes associated with a given process in unix,is there any command for this. (3 Replies)
Discussion started by: laddu
3 Replies
Login or Register to Ask a Question