Find the answer to your Linux question:
Results 1 to 3 of 3
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 ...
  1. #1
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    99

    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]$

  2. #2
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    99
    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

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,065
    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 )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •