Michael A
Michael A

Reputation: 4613

How do I iterate through each line of a command's output in bash?

I have a script that reads from /proc/stat and calculates CPU usage. There are three relevant lines in /proc/stat:

cpu  1312092 24 395204 12582958 77712 456 3890 0 0 0
cpu0 617029 12 204802 8341965 62291 443 2718 0 0 0
cpu1 695063 12 190402 4240992 15420 12 1172 0 0 0

Currently, my script only reads the first line and calculates usage from that:

cpu=($( cat /proc/stat | grep '^cpu[^0-9] ' ))
unset cpu[0]
idle=${cpu[4]}
total=0
for value in "${cpu[@]}"; do
  let total=$(( total+value ))
done

let usage=$(( (1000*(total-idle)/total+5)/10 ))
echo "$usage%"

This works as expected, because the script only parses this line:

cpu  1312092 24 395204 12582958 77712 456 3890 0 0 0

It's easy enough to get only the lines starting with cpu0 and cpu1

cpu=$( cat /proc/stat | grep '^cpu[0-9] ' )

but I don't know how to iterate over each line and apply this same process. Ive tried resetting the internal field separator inside a subshell, like this:

cpus=$( cat /proc/stat | grep '^cpu[0-9] ' )
(
IFS=$'\n'
for cpu in $cpus; do
    cpu=($cpu)
    unset cpu[0]
    idle=${cpu[4]}
    total=0
    for value in "${cpu[@]}"; do
        let total=$(( total+value ))
    done
    let usage=$(( (1000*(total-idle)/total+5)/10 ))
    echo -n "$usage%"
done
)

but this gets me a syntax error

line 18: (1000*(total-idle)/total+5)/10 : division by 0 (error token is "+5)/10 ")

If I echo the cpu variable in the loop it looks like it's separating the lines properly. I looked at this thread and I think Im assigning the cpu variable to an array properly but is there another error Im not seeing?

I put my script into whats wrong with my script and it doesnt show me any errors apart from a warning about using cat within $(), s o I'm stumped.

Upvotes: 5

Views: 5336

Answers (2)

Staven
Staven

Reputation: 3165

Because the OP asked, an awk program.

awk '
    /cpu[0-9] .*/ {
        total = 0
        idle = $5
        for(i = 0; i <= NF; i++) { total += $i; }
        printf("%s: %f%%\n", $1, 100*(total-idle)/total);
    }
' /proc/stat

The /cpu[0-9] .*/ means "execute for every line matching this expression". The variables like $1 do what you'd expect, but the 1st field has index 1, not 0: $0 means the whole line in awk.

Upvotes: 3

janos
janos

Reputation: 124656

Change this line in the middle of your loop:

IFS=' ' cpu=($cpu)

You need this because outside of your loop you're setting IFS=$'\n', but with that settingcpu($cpu)` won't do what you expect.

Btw, I would write your script like this:

#!/bin/bash -e

grep ^cpu /proc/stat | while IFS=$'\n' read cpu; do
    cpu=($cpu)
    name=${cpu[0]}
    unset cpu[0]
    idle=${cpu[4]}
    total=0
    for value in "${cpu[@]}"; do
        ((total+=value))
    done
    ((usage=(1000 * (total - idle) / total + 5) / 10))
    echo "$name $usage%"
done

The equivalent using awk:

awk '/^cpu/ { total=0; idle=$5; for (i=2; i<=NF; ++i) { total += $i }; print $1, int((1000 * (total - idle) / total + 5) / 10) }' < /proc/stat

Upvotes: 6

Related Questions