2

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

#!/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?

2 Answers 2

2

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

{ read core_count ; read -a totals ; } < <(echo -e "2\n4 5")
Sign up to request clarification or add additional context in comments.

Comments

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 Comment

To avoid the duplicate echo | ... code, set $output will tokenize the output and replace $1, $2 etc with the tokenized values. Also note that echo is problematic, and should be avoided. At a minimum, you should quote the strings you echo, generally.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.