0

I have this bash code, when I run this, it throws arithmetic operator error

#!/bin/bash

path=$1;
cluster=`cd $1; df -k . | tail -1 | awk -F: '{print $1}' | awk -F- '{print $1}'`
volume=`cd $1; df -k . | tail -1 | awk '{print $1}' | awk -F: '{print $2}' | awk -F/ '{print $3}'`

if [[ $cluster == *"clap"* ]]; then
avg_latency_val=$(cd /net/kkcmon02/app/whisper/netapp/perf/sanjose/$cluster-cls-mgt/svm/vs0/vol/$volume; whisper-fetch --pretty avg_latency.wsp| tail -n 30 | grep -v None | awk -v N=6 '{ sum += $N } END { if ( NR > 0 ) {print sum/NR} else {print 0}}')

elif [[ $cluster == *"isic"* ]]; then
echo "Graphana can monitor only Netapp volumes & this is a volume coming from Isilon."

else
avg_latency_val=$(cd /net/kkcmon02/app/whisper/netapp/perf7/sanjose/$cluster/vol/$volume; whisper-fetch --pretty avg_latency.wsp| tail -n 30 | grep -v None | awk -v N=6 '{ sum += $N } END { if ( NR > 0 ) {print sum/NR} else {print 0}}')

fi

if [[ "${avg_latency_val}" -le  "30" ]] ; then
    echo "Average Latency on the volume for last 30 mins is "${avg_latency_val}" ms (Good)"

elif [[ "${avg_latency_val}" -gt "30" ]] ; then
    echo "Average Latency on the volume for last 30 mins is "${avg_latency_val}" ms (Bad)"

else
    echo "Average Latency on the volume Looks Good!!"
fi

Below is the error occurred when I ran the above code. Please let me know what I'm doing wrong here

k_test.sh: line 21: [[: 1.98381: syntax error: invalid arithmetic operator (error token is ".98381")

3
  • You've got a lot going on here. Can you post just the part you're having trouble with? Commented May 12, 2017 at 9:39
  • If it's a problem with decimals, try using the bc command Commented May 12, 2017 at 9:41
  • If you're trying to do arithmetic use ((..)) instead of [[..]] Commented May 12, 2017 at 9:55

1 Answer 1

-3

Bash only handles integer numbers. You will have to do some chop shop work to get it to understand a decimal point like that

Sign up to request clarification or add additional context in comments.

10 Comments

How is this an answer?
What do you mean "chop shop work?" bc handles decimals just fine. bc -q <<< "scale=9; (3.38524*(-2.3423))"
elif [[ "${avg_latency_val}" -gt "30" ]] ; It can't tell if this value is greater than because its a decimal point... Workaround: Remove the decimal point and simply change it to a different measurement of time. If you took the time to read, it attualy tells you... invalid arithmetic operator (error token is ".98381") Maybe read before giving me attitude...
@JonathanPugh, will try that.
I don't think that anyone is "giving you attitude", the point is that a vague reference to "chop shop work" is not a helpful answer.
|