BC calculation for floating (invalid arithmetic operator )


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BC calculation for floating (invalid arithmetic operator )
# 1  
Old 06-29-2016
Wrench [RESOLVED]BC calculation for floating (invalid arithmetic operator )

Hi,

I wish to compare the CPU LOAD 1 min with 5mins and 15mins.
If 1 min's CPU LOAd spike 3% compare to 5 mins or 15 mins CPU Load, it is warning.
If 1 min's CPU LOAd spike 5% compare to 5 mins or 15 mins CPU Load, it is critical.

However I received following code error, I google it and tried few method it still the same.


Code:
 ./CPULOAD: line 15: [[: 1.98: syntax error: invalid arithmetic operator (error token is ".98")
./CPULOAD: line 19: [[: 1.94: syntax error: invalid arithmetic operator (error token is ".94")

Code:
#!/bin/ksh
str1=`uptime | awk '{print $10}'| sed 's/,//'`
str2=`uptime | awk '{print $11}'| sed 's/,//'`
str3=`uptime | awk '{print $12}'| sed 's/,//'`
 echo $str1
echo $str2
echo $str3
#warn_level='$str1*1.03' | bc
warn_level=`echo "$str1*1.03"|bc`
crit_level=`echo "$str1*1.05"|bc`
#crit_level='$str1*1.05' | bc
 if [[ "$crit_level" -gt "$str2" || "$crit_level" -gt "$str3" ]]; then
        status=2
        statustxt="CPU load is critical - $str1"
 elif [[ "$warn_level" -gt "$str2" || "$warn_level" -gt "$str3" ]]; then
        status=1
        statustxt="CPU load is Warning - $str1"
else
        status=0
        statustxt="CPU load is normal, no spike - $str1"
fi
 echo "$status CPULOAD_spike  PERFDATA=$str1 $statustxt"

May I know which part could be wrong?

Last edited by alvintiow; 06-29-2016 at 05:49 AM.. Reason: Add [resolved] in the title
# 2  
Old 06-29-2016
What version of ksh are you using?
Is this a Linux kernel base operating system?

Code:
if [[ "$crit_level" -gt "$str2" || "$crit_level" -gt "$str3" ]]; then
elif [[ "$warn_level" -gt "$str2" || "$warn_level" -gt "$str3" ]];

Something to think about:
What does the -gt operator works with: strings or numbers? What are you comparing: strings or numbers?
Does your shell support floating point arithmetic?

Last edited by Aia; 06-29-2016 at 02:29 AM..
This User Gave Thanks to Aia For This Post:
# 3  
Old 06-29-2016
As Aia noted, the version of the Korn shell you're using makes a big difference here. With a 1993 or later version of ksh, your code might work. I'm not sure in this case that the OS matters much, but when asking questions like this, it is always a good policy to tell us what environment you're using.

The fact that the error messages that ksh printed did not come from the script you showed us makes us wonder what the other two lines before the if statement looked like and what the line that you removed before the elif looked like???

And, the fact that you're using sed to remove commas from the uptime output makes us wonder what locale you're using. The text in your output is English, but if you're removing commas, is the locale European or are the actions on the commas just there to confuse us???

Please show us:
  1. the actual code that produced those error messages,
  2. the output you get from running the command: /bin/ksh --version,
  3. the output you get from running the command: locale,
  4. and the output you get from running the command: uptime
(note that when displaying the output you get from running commands, the input that is given to commands, and code segments you should use CODE tags; not just when showing us code segments).

I would guess that you're using a 1988 version of ksh, but until I figure out what you're trying to do with the commas, I'm not sure how to address your problem.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 06-29-2016
Hi Aia,

It is SUSE SLEH 11, I am comparing the 'number'.


Hi Don,

1. The below code is the actual full code showing this error (line 15 and 19)
Code:
#!/bin/ksh
str1=`uptime | awk '{print $10}'| sed 's/,//'`
str2=`uptime | awk '{print $11}'| sed 's/,//'`
str3=`uptime | awk '{print $12}'| sed 's/,//'`
 echo $str1
echo $str2
echo $str3
#warn_level='$str1*1.03' | bc
warn_level=`echo "$str1*1.03"|bc`
crit_level=`echo "$str1*1.05"|bc`
#crit_level='$str1*1.05' | bc
 if [[ "$crit_level" -gt "$str2" || "$crit_level" -gt "$str3" ]]; then
        status=2
        statustxt="CPU load is critical - $str1"
 elif [[ "$warn_level" -gt "$str2" || "$warn_level" -gt "$str3" ]]; then
        status=1
        statustxt="CPU load is Warning - $str1"
else
        status=0
        statustxt="CPU load is normal, no spike - $str1"
fi
 echo "$status CPULOAD_spike  PERFDATA=$str1 $statustxt"

2.
Code:
 /bin/ksh --version: version         sh (AT&T Research) 93u+ 2012-08-01

3. locale:
Code:
LANG=POSIX
LC_CTYPE=en_US.UTF-8
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE="POSIX"
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=

4. uptime
Code:
15:02pm  up 77 days  0:26,  1 user,  load average: 1.82, 1.89, 1.88

Below is the exactly output from the code
Code:
1.82
1.89
1.88
./CPULOAD: line 15: [[: 1.91: syntax error: invalid arithmetic operator (error token is ".91")
./CPULOAD: line 15: [[: 1.91: syntax error: invalid arithmetic operator (error token is ".91")
./CPULOAD: line 19: [[: 1.87: syntax error: invalid arithmetic operator (error token is ".87")
./CPULOAD: line 19: [[: 1.87: syntax error: invalid arithmetic operator (error token is ".87")
0 CPULOAD_spike  PERFDATA=1.82 CPU load is normal, no spike - 1.82

I removed the comma due to when
Code:
awk '{print $10}'

the output consists of comma

Code:
uptime | awk '{print $10}'

below is the output
1.84,

Code:
uptime | awk '{print $10}'| sed 's/,//'

below is the output
1.84

Last edited by RudiC; 06-29-2016 at 05:50 AM..
# 5  
Old 06-29-2016
Quote:
Originally Posted by Don Cragun
As Aia noted, the version of the Korn shell you're using makes a big difference here. With a 1993 or later version of ksh, your code might work. I'm not sure in this case that the OS matters much, but when asking questions like this, it is always a good policy to tell us what environment you're using.

The fact that the error messages that ksh printed did not come from the script you showed us makes us wonder what the other two lines before the if statement looked like and what the line that you removed before the elif looked like???

And, the fact that you're using sed to remove commas from the uptime output makes us wonder what locale you're using. The text in your output is English, but if you're removing commas, is the locale European or are the actions on the commas just there to confuse us???
The OS would be significant, since the output of uptime is not the same.
Also, if it is a Linux kernel /proc/loadavg could be used instead of uptime.
Code:
cat /proc/loadavg
0.00 0.01 0.05 1/133 10331

Instead of dealing with another external program and the output would not have comas:
Code:
uptime
 01:15:03 up 39 days,  8:39,  1 user,  load average: 0.00, 0.01, 0.05


Last edited by Aia; 06-29-2016 at 04:54 AM..
This User Gave Thanks to Aia For This Post:
# 6  
Old 06-29-2016
Hi Aia,

The reason I use uptime instead of /proc/loadavg because I wish to use same coding for Solaris, I see the Solaris uptime same as mine SUSE.

So any idea why the calculation showing error?Smilie
# 7  
Old 06-29-2016
Quote:
Originally Posted by alvintiow
Hi Aia,

The reason I use uptime instead of /proc/loadavg because I wish to use same coding for Solaris, I see the Solaris uptime same as mine SUSE.

So any idea why the calculation showing error?Smilie
Does your Solaris system use the same version of ksh?
ksh93 supports floating point. You do not need the external bc program
Something like
Code:
warn_level=$(( $str1 * 1.03 ))

Also, remove the quotes
Code:
if [[ "$crit_level" -gt "$str2" || "$crit_level" -gt "$str3" ]];

Those are strings and not numbers.
This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Gobsmacked by ksh93 floating point arithmetic.

Hi guys... I am working on limited basic set of maths routines for ksh93 that can be sourced as . ./ksh_math.sh and I am gobsmacked by its capabilities. Although some big guns may already know this, I didn't, and ksh93 is easily able to do floating point numbers to floating point powers and... (16 Replies)
Discussion started by: wisecracker
16 Replies

2. UNIX for Beginners Questions & Answers

Invalid arithmetic operator on string concatenation

Hello. LEAP_VERSION="4.2" export ARRAY_MAIN_REPO_LEAP=('zypper_local' 'openSUSE-Leap-'"$LEAP_VERSION"'-Non-Oss' 'openSUSE-Leap-'"$LEAP_VERSION"'-Oss' 'openSUSE-Leap-'"$LEAP_VERSION"'-Update' 'openSUSE-Leap-'"$LEAP_VERSION"'-Update-Non-Oss')Seems that the - is interpreted as a numeric... (2 Replies)
Discussion started by: jcdole
2 Replies

3. UNIX for Dummies Questions & Answers

Invalid arithmetic operator

Hi experts, I'm facing trouble with the below bash script, any help is appreciated #!/usr/bin/bash .. if (( "${date1}" > "${l_date1}" )) then echo "success" else echo "nothing" fi the variable date1 contains 08/06/2013 and l_date1 contains 10/08/2013 this small script to... (6 Replies)
Discussion started by: parpaa
6 Replies

4. Shell Programming and Scripting

Arithmetic calculation in variable

Hi, I am trying to do an addition to a value stored in a variable... var1=`grep -n "match" sample.txt|cut -d : -f1` var2=`echo $var1|cut -d " " -f1` (Here i want add +1 to the output value) (4 Replies)
Discussion started by: Kevin Tivoli
4 Replies

5. Shell Programming and Scripting

floating point arithmetic operation error

I am writing a script in zsh shell, it fetchs a number from a file using the awk command, store it as a variable, which in my case is a small number 0.62000. I want to change this number by multiplying it by 1000 to become 620.0 using the command in the script var2=$((var1*1000)) trouble is... (2 Replies)
Discussion started by: piynik
2 Replies

6. Programming

arithmetic calculation using awk

hi there again, i need to do a simple division with my data with a number of rows. i think i wanted to have a simple output like this one: col1 col2 col3 val1 val2 val1/val2 valn valm valn/valm any suggestion is very much appreciated. thanks much. (2 Replies)
Discussion started by: ida1215
2 Replies

7. Shell Programming and Scripting

checking variables + arithmetic operator help

i'm trying to make a script to simply add numbers together for example i have a file script file called add add 1 2 3 4 5 15 however i want the user to put at least 3 numbers so i did this if then echo "please enter more than two numbers" exit 1 fi but it's telling me i have... (3 Replies)
Discussion started by: el3ctr0nic47
3 Replies

8. UNIX for Dummies Questions & Answers

syntax error: invalid arithmetic operator

hi, i have a bash script that i want to receive a a string from another bash file. But because the string has a dot in the middle it gives me an error. The error is in this line: let valor=$1 and the value passed is rules.txt the error is: let: valor=rules.txt: syntax error: invalid... (2 Replies)
Discussion started by: limadario
2 Replies

9. Shell Programming and Scripting

Arithmetic calculation on real numbers in Bourne Shell Script

I am begining to learn bourne shell and as a practice I have written a script which when given the purchase price and percentage of discount calculates the savings. I somehow cannot figure out why my script fails to do arthimatic calculation on real numbers. Could anyone look at the script... (5 Replies)
Discussion started by: Tirmazi
5 Replies

10. Shell Programming and Scripting

Arithmetic in floating point

is it not possible to simply di aritmetic without using bc or awk i have tried folllowing operatrions but they support only integer types plz suggest me code for floating using values stored in the variables.the ans i get is integer and if i input floating values i get error numeric constant... (6 Replies)
Discussion started by: sumit the cool
6 Replies
Login or Register to Ask a Question