Hi, all: 

I don't know if this is the right list to ask this question. But since I didn't 
find a bash script mail list and you guys are always so helpful, then...

Here are an excerpt of a bash script: 

-------------------------------------------------------

#!/bin/bash
# saveLogs.sh - Bourne Again Shell script
...
#
# FindAndSaveCoreFiles()
#       Search the entire filesystem for corefiles, saving each in
#       $SAVE_DIR. On Linux, we only need to search in ESS_LOG_DIR.
#
CollectCoreFiles()
{
        local NEWNAME=""
        local NCOREFILES=0

        local OS=`uname`
        if [ "$OS" == "Linux" ]; then
                ...
        else
                #
                # find each corefile, record in $CORELOG, and move to a uniquely
                # named file in $SAVE_DIR
                #
                # Look for files named "core" (Lynx default)
                #
                find / -type f -name core -print | while read COREFILE ; do
                        NCOREFILES=$[ $NCOREFILES + 1 ] # a bit strange - xq
                        echo $NCOREFILES # xq

                        NEWNAME="${HOSTNAME}esscore${NCOREFILES}_${TIMESTAMP}"
                        # record mapping so people can go back and figure out
                        # where they came from
                        echo -e $NEWNAME "    was    " `ls -l $COREFILE` >> 
$SAVE_DIR/$CORELOG
                        mv $COREFILE $SAVE_DIR/$NEWNAME

                        echo "There are $NCOREFILES core files." # xq
                done

        fi

        # What confused me most is the value $NCOREFILES outside 
        # the do-while loop (but still in this function) reverted 
        # back to its initial value, which seems contradictory to 
        # our concept of local variables. - xq
        #echo $NCOREFILES 

        return 0
}

-------------------------------------------------------

The purpose of this script is to find the core files in the system and move 
them to elsewhere for later analysis. 

I am confused about the following issues: 

1. The way of incrementing the variable NCOREFILES. Why does it use the formula 
of "NCOREFILES=$[ $NCOREFILES + 1 ]", 
and not the direct way of "NCOREFILES=$NCOREFILES+1"?

2. What confused me most is the value of the variable NCOREFILES ($NCOREFILES). 
Say there is just 1 core file, then because the initial value of NCOREFILES is 
0, it will be incremented to 1. Yes, this is the value in the do-while loop. 
But outside the loop and if-block, the value of NCOREFILES is reverted back to 
0 - its initial value. 

It is a local variable, so any modification to it should be valid as long as we 
are still in the scope of the function, right? I am really lossed at this 
phenomenon. 

Looking forward to any possible help, 

thanks, 
Xu Qiang


_______________________________________________
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"

Reply via email to