LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Shell script errors (https://www.linuxquestions.org/questions/linux-general-1/shell-script-errors-4175622039/)

Acnologia 01-20-2018 07:38 AM

Shell script errors
 
Hello,

I have a script which doesn't work, and I can't get to know why. Can you help me please ?

My script takes MIB values and put them in a tab. I want to take the third value, 10 times, and get an average of it.

My tab returns for example : 6202 0 17384 0

Here is the script :

Code:

#!/bin/bash


MIB=1.3.6.1.2.1.2.2.1.11


i=0
total=0

while [ $i -le 10 ]
do
        tab=$(snmpwalk -v1 localhost -c public $MIB | cut -d' ' -f 4)
       
        $total=$(($total + ${tab[2]}))
done


echo $(($total / 10))

I might well try to print only ${tab[2]}, but all I have is an empty line, though ${tab[*]} does print the full tab :

http://upload.dinhosting.fr/o/2/O/Documents.png


Can you help me debug it please ?

pan64 01-20-2018 08:05 AM

tab=( $(snmpwalk .... ) )
or if that won't work see mapfile and check probably here: https://stackoverflow.com/questions/...-array-in-bash

by the way you can try to use shellcheck to check your script.

Acnologia 01-20-2018 08:38 AM

Indeed it worked. It was all about brackets. Thank you !
And thanks for shellcheck !


All times are GMT -5. The time now is 09:22 AM.