Problem with dinamic array in ksh script
The other day I was working on a unix
script in which I use dynamic arrays. I found some strange behavior when trying
to fill up the array. When I use the code with “while loop” the array didn’t
fill up.
n=0
grep -i "fbackup(" logfile.log | while read line
do
errores[$a]="$line"
(( n=n+1 ))
done
grep -i "fbackup(" logfile.log | while read line
do
errores[$a]="$line"
(( n=n+1 ))
done
When the above code was changed to use the
“for loop”, it did worked.
ORIGIFS=$IFS
IFS=`echo -en "\n\b"` # set $IFS to end-of-line
n=0
for linea in `grep -i "fbackup(" logfile.log`
do
errores[$n]=${linea}
(( n=n+1 ))
done
IFS=$ORIGIFS # set $IFS back
IFS=`echo -en "\n\b"` # set $IFS to end-of-line
n=0
for linea in `grep -i "fbackup(" logfile.log`
do
errores[$n]=${linea}
(( n=n+1 ))
done
IFS=$ORIGIFS # set $IFS back
I foud the solution to this issue on
http://www.linux.org.za/Lists-Archives/glug-tech-0406/msg00068.html
No hay comentarios:
Publicar un comentario