LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with getting array from function (bash) (https://www.linuxquestions.org/questions/programming-9/problem-with-getting-array-from-function-bash-756635/)

catkin 09-24-2009 03:11 AM

Quote:

Originally Posted by chrism01 (Post 3695030)
You could just put

set -xv

as the 2nd line of your code. It shows everything before and after substitutions etc as it goes.
Great for debugging.
:)

I used to use set -xv (and set +xv to turn it off) all the time but do so much less, now that I use $(<command> 2>&1) on almost all commands to capture their output for error trapping and reporting. With set -xv on, all those commands generate unexpected output!

Now I've reverted to echoing key variable names as required and sometimes to short set -xv blocks.

Using command output capture to tell the user about progress (on screen and log) identifies the part of the script that failed quite closely.

chrism01 09-25-2009 12:17 AM

Ah, but with your experience, you usually have a good idea which parts are (probably) going wrong. Initially, for new users or really obscure problems, one set -xv at the top is highly informative. You can always be more specific later.

DrLove73 09-25-2009 02:50 AM

Absolutely. Actually, day or two before I started this thread I tried "bash -x <scriptname>".

But frankly, I believe that nothing beats good echo command with custom text like:
Code:

echo "Variable K after the change is "$K
My current main script has ~950 lines, but around half of them are echo lines. Just last night I added another field into comma delimited text file I use, and something was disturbed by it. I just uncommented few key echo lines and was able to stop what exactly was the problem.
I know it is not really pretty, but my way of thinking allowed me to reuse those echo commands, and after year or two when I try to change something, expand that database text file maybe, I will be able to debug it with lesser output deciphering.

catkin 09-25-2009 03:48 AM

FWIW I also have a debugging function call trace in my big scripts
Code:

#--------------------------
# Name: fct
# Purpose: function call trace (for debugging)
# $1 - name of calling function
# $2 - message.  If it starts with "started" or "returning" then the output is prettily indented
#--------------------------
function fct {

    if [[ $debug = 'NO' ]]; then
        return 0
    fi

    fct_ident="${fct_indent:=}"

    case $2 in
        'started'* )
            fct_indent="$fct_indent  "
            call_msg 'I' "DEBUG: $fct_indent$1: $2"
            ;;
        'returning'* )
            call_msg 'I' "DEBUG: $fct_indent$1: $2"
            fct_indent="${fct_indent#  }"
            ;;
        * )
            call_msg 'I' "DEBUG: $fct_indent$1: $2"
    esac

}  # end of function fct

$debug is set by the -d option on the command line when the script is called
Code:

    debug='NO'
    while getopts c:dD:hl:r:Vw: opt 2>/dev/null
    do
        case $opt in
            {snip]
            d )
                debug='YES'
                ;;
          [snip]
        esac
    done

and call it with code like
Code:

fct "${FUNCNAME[ 0 ]}" "started with idx=$1"
and
Code:

fct "${FUNCNAME[ 0 ]}" "returning. \$dev_afn is '$dev_afn'"
Can't remember the last time I used it though!

DrLove73 09-25-2009 07:07 AM

Noted and saved to recently started bash code repository. Thanks.


All times are GMT -5. The time now is 12:35 PM.