BASH: set multiple variables at once / performance

2

I am running bash scripts on windows using a msys and mingw environment. I have seem to have an issue where using a command like 'cut' or sed' causes an overhead for each call. I'm not sure if this is an effect of windows running .exe's or what. anyways here is my code:

SOMETHING=`m6811-elf-readelf.exe -l "${TARGET_BASENAME}.elf" | sed '1,/Type / d' | sed '1,/^$/!d'`

echo "$SOMETHING" | while read -r line; do 
    # MEMORY_ADDRESSES=`echo $line | sed -ne 's/LOAD[^0-9a-z]*0x\([^ ]*\) 0x\([^ ]*\) 0x\([^ ]*\) 0x\([^ ]*\) 0x\([^ ]*\) .*/\1 \2 \3 \4 \5/p;'`
     OFFSET=`echo "$line" | cut -f12 -d\ `
     VIRTADDR=`echo "$line" | cut -f13 -d\ `
     PHYSADDR=`echo "$line" | cut -f14 -d\ `
     FILESIZE=`echo "$line" | cut -f15 -d\ `
     MEMSIZE=`echo "$line" | cut -f16 -d\ `
     echo $OFFSET $VIRTADDR $PHYSADDR $FILESIZE $MEMSIZE 
done

I would like to get this running as fast as possible. I thought that the sed line was slowing me down, so I removed it and I am just using cut. For every cut line that I use, this code is much slower.

The contents of the SOMTHING variable are:

  LOAD           0x000000 0x00000000 0x00000000 0x00680 0x00680 RW  0x1000
  LOAD           0x001100 0x00002100 0x00002100 0x00000 0x0005a R   0x1000
  LOAD           0x00115a 0x0000215a 0x0000215a 0x00615 0x00669 RWE 0x1000
  LOAD           0x002000 0x00010000 0x00008000 0x00056 0x00056 R E 0x1000
  LOAD           0x003000 0x00018000 0x00028000 0x03ffe 0x03ffe R E 0x1000
  LOAD           0x007000 0x0001c000 0x00038000 0x03e60 0x03e60 R E 0x1000
  LOAD           0x00b000 0x00020000 0x00048000 0x021b5 0x021b5 R E 0x1000
  LOAD           0x00e000 0x00024000 0x00058000 0x0236a 0x0236a R E 0x1000

The purpose of this code is to extract each hex address from the $SOMETHING output so I can do some simple math. As this file gets longer (up to 20 lines) it gets pretty slow.

If you know an efficient way for me to assign the OFFSET,VIRTADDR,PHYSADDR etc variables, it would help a lot!!

performance
bash
variables
gcc
elf

2 Answers

6
echo "$SOMETHING" | while read -r junk OFFSET VIRTADDR PHYSADDR FILESIZE MEMSIZE junk2; do
     echo $OFFSET $VIRTADDR $PHYSADDR $FILESIZE $MEMSIZE 
done
answered on Stack Overflow Nov 1, 2011 by rob mayoff • edited Nov 1, 2011 by portforwardpodcast
0
echo "$SOMETHING" | cut -d ' ' -f 11-15

The cut utility can be used to extract more than one field at a time.

Or if you want to process the data further, you can use awk:

echo "$SOMETHING" | awk '{ print($2, $3, $4, $5, $6); }'

The awk tool splits the input on word boundaries, so you don't have to count the space characters. You can reach the last word of each line by $NF and the second-to-last word by $(NF - 1).

Depending on what you do with the data, using perl may be even more appropriate.

answered on Stack Overflow Nov 1, 2011 by Roland Illig

User contributions licensed under CC BY-SA 3.0