2

我有一个 awk 脚本,它获取系统中总 eth 中断的数量。

#!/bin/bash

FILE="/proc/interrupts"

awk 'NR==1 {
core_count = NF 
print "core count: ", core_count
next
}

/eth/ {
 for (i = 2; i <= 2+core_count; i++)
 totals[i-2] += $i
}

END {
print "Totals"
for (i = 0; i < core_count; i++)
printf("CPU%d: %d\n", i, totals[i])
}
' $FILE

在 bash 的最后,我有 core_count 和 totals 数组。但是,我需要使用这些变量,如何在脚本的其余部分使用它们?换句话说,如何将它们全球化?

4

2 回答 2

2

你不能。将它们呼出并拉入。

{ read core_count ; read -a totals ; } < <(echo -e "2\n4 5")
于 2012-07-23T05:43:17.490 回答
1
#!/bin/bash

FILE="/proc/interrupts"

output=$(awk 'NR==1 {
core_count = NF 
print core_count
next
}

/eth/ {
 for (i = 2; i <= 2+core_count; i++)
 totals[i-2] += $i 
}

END {
for (i = 0; i < core_count; i++)
  printf("%d\n", totals[i])
}
' $FILE)
core_count=$(echo $output | cut -d' ' -f1)
output=$(echo $output | sed 's/^[0-9]*//')
totals=(${output// / })
echo CC: $core_count total0 ${totals[0]} total1 ${totals[1]}
于 2012-07-23T06:00:31.023 回答