我正在编写一个简单的函数,它获取 /proc/[pid]/maps 的输出并将其写入 CSV。我想在底部写下所有地图的总大小。
这是功能:
output_MAPS ()
{
{
local total=0
echo "total , size , size_hex , start , end , perms , offset , dev , inode , path"
echo "$1" | while read line ; do
local start="$( echo "$line"| awk '{print $1}'| awk 'BEGIN { FS="-" } { print $1 }' )"
local end="$( echo "$line"| awk '{print $1}'| awk 'BEGIN { FS="-" } { print $2 }' )"
local perms="$( echo "$line"| awk '{print $2}' )"
local offset="$( echo "$line"| awk '{print $3}' )"
local dev="$( echo "$line"| awk '{print $4}' )"
local inode="$( echo "$line"| awk '{print $5}' )"
local path="$( echo "$line"| awk '{print $6}' )"
local size=$(( 0x$end - 0x$start ))
local size_hex="$( printf "%X" "$size")"
local tot_size=$(( $tot_size + $size ))
echo "$tot_size , $size , $size_hex , $start , $end , $perms , $offset , $dev , $inode , $path"
total="$tot_size"
done
echo "TOTAL $total :: $tot_size"
}> "$outputdir/$4_$3_$2.csv"
}
这是最后几行输出:
114282496 , 20480 , 5000 , 2aaab14b0000 , 2aaab14b5000 , rw-p , 2aaab14b0000 , 00:00 , 0 ,
170688512 , 56406016 , 35CB000 , 2aaab14b5000 , 2aaab4a80000 , r--p , 00000000 , 08:02 , 3702398 , /usr/lib/locale/locale-archive
170717184 , 28672 , 7000 , 2aaab4a80000 , 2aaab4a87000 , r--s , 00000000 , 08:02 , 4000735 , /usr/lib64/gconv/gconv-modules.cache
170721280 , 4096 , 1000 , 2aaab4a87000 , 2aaab4a88000 , rw-p , 2aaab4a87000 , 00:00 , 0 ,
170811392 , 90112 , 16000 , 7ffffffe9000 , 7ffffffff000 , rw-p , 7ffffffe8000 , 00:00 , 0 , [stack]
179200000 , 8388608 , 800000 , ffffffffff600000 , ffffffffffe00000 , ---p , 00000000 , 00:00 , 0 , [vsyscall]
TOTAL 0 ::
我们可以从第一列中看到我正在计算总数,但我无法将值从循环中取出,即使我将它分配给外部声明的变量也是如此。那我在做什么傻事?