|
From: | Matheny, Rob |
Subject: | Problem with Script Variables |
Date: | Thu, 14 Oct 2004 16:11:00 +0100 |
Configuration Information [Automatically generated, do not change]:
Machine: sparc
OS: solaris2.8
Compiler: gcc
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='sparc' -DCONF_OSTYPE='solaris2.8' -DCONF_MACHTYPE='sparc-sun-solaris2.8' -DCONF_VENDOR='sun' -DLOCALEDIR='/usr/local/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./include -I./lib -DNDEBUG -I/usr/local/BerkeleyDB.4.2/include -I/usr/local/ssl/include -I/usr/openwin/include -I/usr/local/lib/glib-2.0/include -I/usr/local/include -g -O2
uname output: SunOS hdels1 5.8 Generic_117000-03 sun4u sparc SUNW,Sun-Blade-1000
Machine Type: sparc-sun-solaris2.8
Bash Version: 3.0
Patch Level: 0
Release Status: release
Description:
[In bash scripts I have noticed that if I have a variables inside 'for while' loops that after the 'done' statement the loop variables are not accessable. Ie echo $var has no value where it just did inside the loop. Below is and example of where I found this behavior. Please look at the fileline variable which is affected by the above stated condition.
#!/bin/bash
#BUILD ARRAY
libname=$1
docnumber=$2
arrayfileline=1
cat ${libname}.log | tr -d "\0" > ${libname}2.log
rm ${libname}.log
cat ${libname}2.log | while read LINE
do
export fileline[$arrayfileline]=$LINE
((arrayfileline=arrayfileline+1))
linecount=`echo ${#fileline[*]}`
echo $linecount > /tmp/linecount
done
################
getarrayline=1
linecount=`cat /tmp/linecount`
linestat=go
while [ $linestat == go ]
do
echo "ARRAY LINE ${fileline[$getarrayline]}"
echo ${fileline[$getarrayline]} | grep $docnumber > /dev/null
if [ $? -eq 0 ] #FOUND DOC IN NDX LOG
then
((getnextline=getarrayline+1))
echo "${fileline[$getarrayline]} ${fileline[$getnextline]}"
fi
((getarrayline=getarrayline+1))
if [ $linecount == $getarrayline ]
then
linestat=nogo
fi
Done]
Repeat-By:
[#!/bin/bash
#BUILD ARRAY
libname=$1
docnumber=$2
arrayfileline=1
cat ${libname}.log | tr -d "\0" > ${libname}2.log
rm ${libname}.log
cat ${libname}2.log | while read LINE
do
export fileline[$arrayfileline]=$LINE
((arrayfileline=arrayfileline+1))
linecount=`echo ${#fileline[*]}`
echo $linecount > /tmp/linecount
done
################
getarrayline=1
linecount=`cat /tmp/linecount`
linestat=go
while [ $linestat == go ]
do
echo "ARRAY LINE ${fileline[$getarrayline]}"
echo ${fileline[$getarrayline]} | grep $docnumber > /dev/null
if [ $? -eq 0 ] #FOUND DOC IN NDX LOG
then
((getnextline=getarrayline+1))
echo "${fileline[$getarrayline]} ${fileline[$getnextline]}"
fi
((getarrayline=getarrayline+1))
if [ $linecount == $getarrayline ]
then
linestat=nogo
fi
Done.]
Cheers,
Rob Matheny
CLASSIFICATION UNCLASSIFIED
[Prev in Thread] | Current Thread | [Next in Thread] |