简体   繁体   English

Bash:奇怪的变量范围问题

[英]Bash: strange variable scoping issue

I'm writing what should be a simple function that takes the output of /proc/[pid]/maps and writes it to CSV. 我正在编写应该是一个简单的函数,它接受/ proc / [pid] / maps的输出并将其写入CSV。 I'd like to write the total size of all the maps at the bottom. 我想在底部写下所有地图的总大小。

Here's the function: 这是功能:

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"
}

and here's the last few lines of output: 这是最后几行输出:

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 ::

We can see from the first column that I'm calculating the total, but I just can't get the value out of the loop, even when I assign it to a variable declared outside. 我们可以从第一列看到我正在计算总数,但是我无法从循环中获取值,即使我将它分配给外部声明的变量。 So what stupid thing am I doing? 那么我做的蠢事是什么?

The problem is that you are piping into the while loop. 问题是你正在进入while循环。 The loop is executed in a subshell and when the loop finishes all variables defined within it are discarded. 循环在子shell中执行,当循环结束时,将丢弃其中定义的所有变量。 That's why you see that tot_size is blank. 这就是为什么你看到tot_size是空白的。

Instead of a pipe, use redirection as shown below: 而不是管道,使用重定向,如下所示:

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 <<< "$1"

See: BashFAQ/024 for a detailed explanation of this issue. 有关此问题的详细说明,请参阅: BashFAQ / 024

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM