2012-07-23 5 views
2

시스템의 총 eth 인터럽트 수를 사용하는 awk 스크립트가 있습니다.bash에서 awk 변수 사용

#!/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 배열이 있습니다. 그런데이 변수를 사용해야 할 때 나머지 스크립트에서 어떻게 사용할 수 있습니까? 즉, 어떻게 글로벌화 할 수 있습니까?

답변

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

감사가 중복 된'에코을 방지하기 위해 – barp

+0

을 너무 유용 @perreal. | ..'. 코드'set $ output'은 출력을 토큰 화하고'$ 1','$ 2' 등을 토큰 화 된 값으로 대체합니다. 또한 'echo'는 문제가 있으므로 피해야합니다. 최소한 일반적으로'echo'문자열을 인용해야합니다. – tripleee

2

수 없습니다. 그들을 밖으로 에코하고 그들을 끌어

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