Help on bash script?

Xu Qiang Qiang.Xu at
Fri Aug 12 01:31:25 GMT 2005

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: 


# - 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.
	local NEWNAME=""

	local OS=`uname`
	if [ "$OS" == "Linux" ]; then
		# 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

			# record mapping so people can go back and figure out
			# where they came from
			echo -e $NEWNAME "    was    " `ls -l $COREFILE` >> $SAVE_DIR/$CORELOG

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


	# 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

	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, 

Xu Qiang

More information about the freebsd-questions mailing list