在bash中使用awk变量

时间:2021-06-25 23:52:34

I have an awk script that takes the number of total eth interrupts in the system.

我有一个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

At the end of this in bash, I have the core_count and the totals array. but then, I need to use these variables, how can I use them at the rest of the script?In other words how can you globalize them?

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

2 个解决方案

#1


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

#2


2  

You can't. Echo them out and pull them in.

你不能。回应他们,把他们拉进来。

{ read core_count ; read -a totals ; } < <(echo -e "2\n4 5")

#1


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

#2


2  

You can't. Echo them out and pull them in.

你不能。回应他们,把他们拉进来。

{ read core_count ; read -a totals ; } < <(echo -e "2\n4 5")