Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 12 Aug 2005 09:33:54 +0800
From:      Xu Qiang <Qiang.Xu@fujixerox.com>
To:        freebsd-questions@freebsd.org
Subject:   Help on bash script?
Message-ID:  <20050812013020.E9A131D92E@imss.sgp.fujixerox.com>

next in thread | raw e-mail | index | archive | help
Hi, all: =


I don't know if this is the right list to ask this question. But since I di=
dn'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=3D""
	local NCOREFILES=3D0

	local OS=3D`uname`
	if [ "$OS" =3D=3D "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=3D$[ $NCOREFILES + 1 ] # a bit strange - xq
			echo $NCOREFILES # xq

			NEWNAME=3D"${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 for=
mula of "NCOREFILES=3D$[ $NCOREFILES + 1 ]", =

and not the direct way of "NCOREFILES=3D$NCOREFILES+1"?

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


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


Looking forward to any possible help, =


thanks, =

Xu Qiang





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050812013020.E9A131D92E>