Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > How to run parent shell script in -x ~debug mode and all child scripts that it calls

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 08-03-2007   #1 (permalink)
Just Joined!
 
Sangal-Arun's Avatar
 
Join Date: May 2006
Location: Gurgaon, India + Denver Colorado USA
Posts: 97
Send a message via Yahoo to Sangal-Arun
How to run parent shell script in -x ~debug mode and all child scripts that it calls

Hi All,

I have created a shell simple script, but when I'm running it in debug mode say, $ sh -x <mainscript>.sh ....the debugging is done only for this script and not cascading further if this script calls another scripts within its body.

I don't want to put "sh -x" in front of each sub-script that is being called from this main script. Also, I don't want to mention #!/bin/bash -x inside the child/sub scripts that are being called by the main script within its body.

Is this POSSIBLE, if I give sh -x <mainscript>.sh ...and system will automatically cascade the debug behaviour to all sub/child scripts that are called within this mainscript....kindly confirm back...if this is even possible or not.

I'm also trying to figure out whan command can I use to find out ...what all commands (lines) inside a script are called during a script execution and lines inside the sub/child scripts that is called from the mainscript...even if it calls other scripts is same shell or child shell.

Any helps...pls feel free if you want me to clarify my Q? again.
thanks-
/Arun


Script example is given below: where sh-x.sh is the mainscript, it calls sh-x1.sh and sh-x2.sh sub scripts. See the "cat" command output for these 3 files and output which I'm getting while executing "sh-x.sh" with -x option on.

================= thanks==================

Code:
[user@build]$ cat sh-x1.sh
#!/bin/bash

echo "script sh-x1.sh has been called ---!"
[user@build]$
[user@build]$ cat sh-x2.sh
#!/bin/bash

echo "script sh-x2.sh has been called ..after ./sh-x1.sh at the same shell level"
[user@build]$
[user@build]$ cat sh-x.sh
#!/bin/bash

echo "arun K sangal"
echo "-------------"
./sh-x1.sh
. ./sh-x2.sh
echo -e "\nDone"

[user@build]$ chmod 700 sh-x.sh sh-x1.sh sh-x2.sh
[user@build]$ ll -tr sh-x*
-rwx------    1 stpuser  webgroup       97 Aug  3 16:53 sh-x.sh
-rwx------    1 stpuser  webgroup       57 Aug  3 17:14 sh-x1.sh
-rwx------    1 stpuser  webgroup       95 Aug  3 17:15 sh-x2.sh
[user@build]$
[user@build]$ ./sh-x.sh
arun K sangal
-------------
script sh-x1.sh has been called ---!
script sh-x2.sh has been called ..after ./sh-x1.sh at the same shell level

Done
[user@build]$ sh -x ./sh-x.sh
+ echo 'arun K sangal'
arun K sangal
+ echo -------------
-------------
+ ./sh-x1.sh
script sh-x1.sh has been called ---!
+ . ./sh-x2.sh
++ echo 'script sh-x2.sh has been called ..after ./sh-x1.sh at the same shell level'
script sh-x2.sh has been called ..after ./sh-x1.sh at the same shell level
+ echo -e '\nDone'

Done
[user@build]$
Sangal-Arun is offline  


Reply With Quote
Old 08-03-2007   #2 (permalink)
Just Joined!
 
Sangal-Arun's Avatar
 
Join Date: May 2006
Location: Gurgaon, India + Denver Colorado USA
Posts: 97
Send a message via Yahoo to Sangal-Arun
Looks like when I'm running the mainscript "sh-x.sh" with -x and -v option ...I'm getting the desired output for "sh-x2.sh" as it is being called in the same parent shell level.

/Arun
Sangal-Arun is offline   Reply With Quote
Old 08-04-2007   #3 (permalink)
drl
Linux Engineer
 
drl's Avatar
 
Join Date: Apr 2006
Location: Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
Posts: 814
Hi.

It looks like you can do this if you cause the variable SHELLOPTS to be in the environment (export), and use set -x (xtrace on). For example:
Code:
#!/bin/sh

# @(#) s1       Demonstrate feature.

set -o nounset
echo
echo "GNU bash $BASH_VERSION" >&2
echo

cat >sub1 <<EOF
#!/bin/bash
echo " Hello from sub1."
echo " shellopts sub1 $SHELLOPTS"
exit 0
EOF

cat >sub2 <<EOF
#!/bin/bash
echo " Hello from sub2."
echo " shellopts sub2 $SHELLOPTS"
exit 0
EOF

chmod +x sub1 sub2

echo
echo " SHELLOPTS in main $SHELLOPTS"

echo
echo " Running sub1."
./sub1

echo
echo " Setting xtrace on."
set -x

echo
echo " SHELLOPTS in main $SHELLOPTS"

echo
echo " Running sub2."
./sub2

export SHELLOPTS
echo
echo " Running sub1 again with SHELLOPTS in environment."
./sub1

exit 0
which produces the cascade when the first script is re-run:
Code:
% ./s1

GNU bash 2.05b.0(1)-release


 SHELLOPTS in main braceexpand:hashall:interactive-comments:nounset:posix

 Running sub1.
 Hello from sub1.
 shellopts sub1 braceexpand:hashall:interactive-comments:nounset:posix

 Setting xtrace on.
+ echo

+ echo ' SHELLOPTS in main braceexpand:hashall:interactive-comments:nounset:posix:xtrace'
 SHELLOPTS in main braceexpand:hashall:interactive-comments:nounset:posix:xtrace
+ echo

+ echo ' Running sub2.'
 Running sub2.
+ ./sub2
 Hello from sub2.
 shellopts sub2 braceexpand:hashall:interactive-comments:nounset:posix
+ export SHELLOPTS
+ echo

+ echo ' Running sub1 again with SHELLOPTS in environment.'
 Running sub1 again with SHELLOPTS in environment.
+ ./sub1
+ echo ' Hello from sub1.'
 Hello from sub1.
+ echo ' shellopts sub1 braceexpand:hashall:interactive-comments:nounset:posix'
 shellopts sub1 braceexpand:hashall:interactive-comments:nounset:posix
+ exit 0
+ exit 0
However, at a quick glance, it does not seem that the SHELLOPTS value is propagated to the child itself, which means that you'd need to do the same thing in the child to get its children to trace. You could set up an auxiliary script to set debug on / off, and run that in every script of interest.

Perhaps someone will stop by with a more clever answer to this.

See man bash for details ... cheers, drl
__________________
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
drl is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 12:52 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2