BASH:一次设置多个变量/ performance

por*_*ast 2 variables bash performance gcc elf

我正在使用msys和mingw环境在Windows上运行bash脚本.我似乎有一个问题,使用像'cut'或sed'这样的命令会导致每次调用的开销.我不确定这是否是Windows运行.exe的影响或者是什么.反正这里是我的代码:

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
Run Code Online (Sandbox Code Playgroud)

我希望尽快运行.我认为sed线正在减慢我的速度,所以我将它删除了,我只是使用了切割.对于我使用的每个剪切线,此代码要慢得多.

SOMTHING变量的内容是:

  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
Run Code Online (Sandbox Code Playgroud)

这段代码的目的是从$ SOMETHING输出中提取每个十六进制地址,这样我就可以做一些简单的数学运算.随着此文件变长(最多20行),它变得非常慢.

如果您知道一种有效的方式来分配OFFSET,VIRTADDR,PHYSADDR等变量,它会有很大的帮助!

rob*_*off 6

echo "$SOMETHING" | while read -r junk OFFSET VIRTADDR PHYSADDR FILESIZE MEMSIZE junk2; do
     echo $OFFSET $VIRTADDR $PHYSADDR $FILESIZE $MEMSIZE 
done
Run Code Online (Sandbox Code Playgroud)

  • 不,我没有.`read`分割字段的方式与`cut -d \`不同. (2认同)