From owner-freebsd-questions@FreeBSD.ORG Fri Aug 12 01:31:25 2005 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 2590716A41F for ; Fri, 12 Aug 2005 01:31:25 +0000 (GMT) (envelope-from Qiang.Xu@fujixerox.com) Received: from mx20.fujixerox.co.jp (mx20.fujixerox.co.jp [192.26.96.20]) by mx1.FreeBSD.org (Postfix) with ESMTP id ABD6043D45 for ; Fri, 12 Aug 2005 01:31:24 +0000 (GMT) (envelope-from Qiang.Xu@fujixerox.com) Received: from isvw20.fujixerox.co.jp ([129.249.27.140]) by mx20.fujixerox.co.jp with ESMTP id j7C1VNql005673 for ; Fri, 12 Aug 2005 10:31:23 +0900 (JST) Received: from ms20.fujixerox.co.jp (localhost [127.0.0.1]) by isvw20.fujixerox.co.jp with ESMTP id j7C1VMmR015836 for ; Fri, 12 Aug 2005 10:31:22 +0900 (JST) Received: from sgpaphq-smtp01.fujixerox.com ([13.198.8.73]) by ms20.fujixerox.co.jp with ESMTP id j7BJjUWl006683 for ; Fri, 12 Aug 2005 10:31:22 +0900 (JST) Received: from mailhost.sgp.fujixerox.com by sgpaphq-smtp01.fujixerox.com with ESMTP id 116023291123810227; Fri, 12 Aug 2005 09:30:27 +0800 Received: from localhost (localhost [127.0.0.1]) by localhost.sgp.fujixerox.com (Postfix) with SMTP id E85D61D99F for ; Fri, 12 Aug 2005 09:30:23 +0800 (SGT) Received: from sess.xssc.sgp.xerox.com (unknown [13.198.33.122]) by imss.sgp.fujixerox.com (Postfix) with ESMTP id E9A131D92E; Fri, 12 Aug 2005 09:30:20 +0800 (SGT) From: Xu Qiang To: freebsd-questions@freebsd.org Date: Fri, 12 Aug 2005 09:33:54 +0800 X-Sent-Folder-Path: Sent Items X-Mailer: Oracle Connector for Outlook 9.0.4 60130 (9.0.2711) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Message-Id: <20050812013020.E9A131D92E@imss.sgp.fujixerox.com> Subject: Help on bash script? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Aug 2005 01:31:25 -0000 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