sábado, 3 de diciembre de 2011

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


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



No hay comentarios: