How to sum values from top

Hi.
Im looking for way to sum numbers from top. For example i have such command

top -b -n | grep Cpu | cut -c 35 - 39

which give me output 97.0 . Ho can i do with that value any arithmetic actions (for example 97.0 +1)?
Using

c = $((top -b -n | grep Cpu | cut -c 35 - 39))

gives me eof expression error...

When defining variables, don't leave spaces between the variable name the equal sign and the value. On my box top is missing a value for -n. Also when using a range with cut, leave spaces out too. You should work on the single commands first so that they work and then put them together.

c = $((top -b -n | grep Cpu | cut -c 35 - 39))
 ^ ^            ^                      ^ ^
 | |            |                      | |
Delete those..  |  ..spaces, they are wrong!
                |
       Missing a value here
c=$((top -b -n 1| grep Cpu | cut -c 35-39))

gives the same error message...

arithmetic expression: expecting EOF: "top -b -n 1| grep Cpu | cut -c 35-39"

Hi,

Hope this will be helpful.

$ top -b -n 1| grep Cpu| cut  -c35-39
 83.7
$ echo `top -b -n 1| grep Cpu| cut  -c35-39` + 1| bc -l
84.7
$ echo `top -b -n 1| grep Cpu| cut  -c35-39` + 1| bc -l| read tvar
$ echo $tvar
84.7
$

Put it into single brackets not double - you don't want any calculations there. $() is the same as ranjithpr wrote with ``.

Also start using

```text
 and 
```

when posting code, data or logs please.

echo `top -b -n 1| grep Cpu| cut  -c35-39` + 1| bc -l| read tvar
echo $tvar

gives error message illegal character |

echo $( top -b -n 1| grep Cpu| cut  -c35-39 ) + 1| bc -l| read tvar
echo $tvar

works without error, but gives no output

Stay with your original line:

c=$(top -b -n 1| grep Cpu | cut -c 35-39)
echo $c
c1=$(top -b -n 1| grep Cpu | cut -c 35-39)
echo $c1
93.7

c2=$(top -b -n 1| grep Cpu | cut -c 9-12)
echo $c2
0.2

c3=$(top -b -n 1| grep Cpu | cut -c 19-21)
echo $c3
0.1

So :rolleyes: I need to sum c1+c2+c3 = c (need to get CPU total usage ), that variable c i want to use in different if statements (if test $c -lt 10; then). When i try to use c=$(($c1+$c2)) it gives me error (if i use just c=$(($11+$11)) it works). It means, that c1 and c2 are not integer values? So how can i fix it?

typeset -i c1 c2 c3

... makes no sense to me. $ is beginning character of a variable. There are positional variables/parameters but you are not using $11 from your examples so I guess it is a typo.
Are you sure you wanted to award me 50k bits and are now in minus with your bits account?