You're getting this error since you are trying to compare string using equality operators intended for integers, -eq
, -ne
, -gt
, and similar are integer functions.
To compare strings use =
to compare for equality OR !=
to compare for non-equality.
Check this for more on comparison operators.
if [ $tname -eq $name ]; then
should be changed to:
if [ "$tname" = "$name" ]; then
(also remember to quote your variables).
From the link you provided, I see the below line.
result=$(echo "$used / $total * 100" |bc -l|cut -c -2)
As per @Graeme's comment, change the above line to below.
result=$(echo "$used / $total * 100" |bc -l)
Now, after adding the above line, we have to change the output of the result
to integer as below.
result1=${result/.*}
I guess in one of the machines where the error occurs, this output is not an integer. Just convert the output of result to integer so that you can handle such cases. Add the below line after you calculate the result
.
result1=${result/.*}
And instead of result
change the variable names as result1
inside the if
loops, and the error won't occur.
I suspect, the cut -c -2
attributes to the error mostly since it is cutting the first 2 characters only. What if result has just one character? Suppose if result is 1.23456
, the above cut will result in 1.
as the value for result
which obviously is the cause of the integer expected
error.
The reason it is working fine in the remaining servers is because it has not encountered a case where the result
variable has just a single digit. It is highly likely to fail in the remaining servers too if the result is a single digit variable (something like I mentioned in the above example).
Best Answer
You need to use a
$
to expand a variable in bash, unless it's inside of(( ))
. You are comparing the literal string "num" to the number 0. Use one of the following: