我有一个 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 和总计数组。但是,我需要使用这些变量,如何在脚本的其余部分使用它们?换句话说,如何全局化它们?
最佳答案
你不能。回响它们并将它们拉进来。
{ read core_count ; read -a totals ; } < <(echo -e "2\n4 5")
关于linux - 在 bash 中使用 awk 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11607005/