Making a trace for a script

Hello experts,

I'm trying to make a trace (unix log) in hope to see why I have differences in some bases :

I putted at the first { and in the last line } > $DATA_SAS 2>&1

Is it a right command ? Do you have another solution ?

thank you,
regards,

how about execute the script with -x option ?

bash -x script >/tmp/out.txt 2>&1

I tried with set -x but the script is to big, I can't see the whole execution on putty, for this I'm trying with a trace

Thank you for your advice,

Regards,

I'm a little confused as to what this means. Can you post your code (wrapped in CODE tags) then show us the input you have and the expected output & errors you are getting (all wrapped in CODE tags too)

The more you can explain your issue, the more likely we are to be able to help.

Kind regards,
Robin

1 Like
#! /usr/bin/ksh -p


# 0          INPUT PARAMETER
###############################################################

set -x



{


    TMPDIR=/data/work/tmp/PROU
    export TMPDIR

    xYYYYMMDD=$1
    if [ -s ./LOG/history_launch.txt ] ; then
        echo "-----------------------------------------------------------------------------------------------------------------"
        echo "LAST LAUNCH:"
        echo "-----------------------------------------------------------------------------------------------------------------"
        cat ./LOG/history_launch.txt | head -1 
        cat ./LOG/history_launch.txt | tail -1
        echo "-----------------------------------------------------------------------------------------------------------------"
    fi

    if [ ${xYYYYMMDD} -gt 0 ] ; then
        YYYYMMDD=${xYYYYMMDD}
    else
        echo " ENTER OBSERVATION DATE (YYYYMMDD) ? "
        #YYYYMMDD=20160805
        read YYYYMMDD
        echo "-----------------------------------------------------------------------------------------------------------------"
    fi

    # 1          TOOL PARAMETERS
    ###############################################################

    # 1.1        DATES  
    ###############################################################
    DD=`echo ${YYYYMMDD} | extraire -c 7-8`
    MM=`echo ${YYYYMMDD} | extraire -c 5-6`
    YYYY=`echo ${YYYYMMDD} | extraire -c 1-4`
    YYYYMM=${YYYY}${MM}

    # 1.2        LOADING PREDEFINED PARAMETERS
    ###############################################################
    . ./PARAMETERS/parameters

    # 1.3        EXPORT VARIABLES TO SAS
    ###############################################################
    export PROGRAMS=${PROGRAMS}
    export YYYYMMDD=${YYYYMMDD}
    export YYYYMM=${YYYYMM}
    export BASES_DIR=${BASES_DIR}
    export DATA_TMP=${DATA_TMP}
    export DATA_SAS=${DATA_SAS}
    export DATA_TXT=${DATA_TXT}
    export OUTPUTS=${OUTPUTS}
    export SASOUT=${SAS_OUT} 

    # 2          CHECKING MOUCHARD EXISTANCE  
    ###############################################################
    if [ -s ${MOUCHARD}/COMSE.${YYYYMMDD}.Z ] ; then


        # 2.1        MOUCHARD EXISTS
        ###############################################################

        # 2.1.1      CHECKING PHASE 1 FILE(S) EXISTANCE 
        ###############################################################

        if [ -s ${DATA_SAS}/f1_??????_????????.sas7bdat ] ; then

            # 2.1.1.1    PHASE 1 FILE(S) EXISTS
            ###############################################################

            # 2.1.1.1.1  MANAGING PREVIOUS F1 DATE 
            ###############################################################


            cd ${DATA_SAS}
            YYYYMMDDb=`ls -1 f1_??????_????????.sas7bdat | tail -1 | extraire -c 11-18`
            YYYYMMb=`ls -1 f1_??????_????????.sas7bdat | tail -1 | extraire -c 4-9`
            cd ${TOOL_DIR}
            export YYYYMMDDb=${YYYYMMDDb}
            export YYYYMMb=${YYYYMMb}


} > $DATA_SAS 2>&1

There is nothing in your script that assigns a value to the variable DATA_SAS . The command:

export DATA_SAS=${DATA_SAS}

does absolutely nothing if DATA_SAS is a variable defined in your environment when your script starts (exported variables remain exported). If DATA_SAS is not an environment variable that is exported to your script, that command sets DATA_SAS to an empty string and exports it.

If we assume that the above command is a no-op and that $DATA_SAS expands to a string that is a pathname of a file, then at least one of the following commands in your script:

cd ${DATA_SAS}
YYYYMMDDb=`ls -1 f1_??????_????????.sas7bdat | tail -1 | extraire -c 11-18`
YYYYMMb=`ls -1 f1_??????_????????.sas7bdat | tail -1 | extraire -c 4-9`
cd ${TOOL_DIR}
export YYYYMMDDb=${YYYYMMDDb}
export YYYYMMb=${YYYYMMb}


} > $DATA_SAS 2>&1

has to fail. You can't change directory to a regular file; only to a directory. And you can't redirect output to a directory; only to a non-directory file.

If $DATA_SAS expands to an empty string, the redirection should fail with a syntax error.

If $DATA_SAS expands to a pathname of a directory, the redirection should fail.

If $DATA_SAS expands to a pathname of an existing regular file to which you have write permission or a non-existent file in a directory in which you have write permission, you should have gotten the output you wanted, but the attempt to change directory would have failed. Otherwise the redirection would have failed.

If we assume that $DATA_SAS does expand to the pathname of a directory, maybe you could get what you want if you change the line in your script:

} > $DATA_SAS 2>&1

to:

} > "$DATA_SAS"/logfile_name 2>&1
1 Like

After formatting and indenting your script, there also seems to be missing 2 times fi to close the opening if .
Having a glance at your script I wonder if you ever tested any parts of it when writing it or did you just type if down all the way?

1 Like