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 )
# 8  
Old 06-29-2016
[Resolved]

Hi Aia,

Thanks for the quick help, this resolved the problem
# 9  
Old 06-29-2016
I'm still confused by this thread. With the script you showed us, it should work just fine (with or without double quotes around the expansions of the variables your [[ expression ]] statements) if t is run by a 1993 or later version of the Korn shell (including version 93u+, which is what you say you are using).

The diagnostic messages you showed us are something that I would expect to see if you executed a script similar to what you showed us using bash as your shell instead of ksh. And, I say a script similar to (but not the script you showed us) because the diagnostics point to problems in the [[...]] statements on lines 15 and 19 in your script; but the script you showed us has those statements on lines 12 and 15 (NOT 15 and 19).

But, running successfully and producing the results you want are two different issues. From your problem statement, I thought you wanted to produce a warning or critical message if field 10 in the uptime output is 3% or 5%, respectively, higher than the lower or the field 11 or field 12 values. (I.e., the current 1 minute value is greater than the 5 and 15 minute values.) But, the code you showed us (if run by a 1993 or later ksh), if the field 11 or 12 value is less than 103% or 105% of the field 10 value. So, for example, if fields 10, 11, and 12 all contained the value 1.00 (showing absolutely no increase in load in the last 15 minutes; your script would report a critical condition because 105% of the field 10 value (1.05) is greater than the field 11 and 12 values (1.00). Is this really what you want?

The following script should do what you want on your Linux system. If you also have a 1993 or later version of ksh on your Solaris system (which might only be true if you're running a Solaris 11.0 or later system), the following would do what I think you described in your requirements just using shell built-ins. I must say, however, that I don't understand the need to include the current load value (as contained in $str1) in each of your three possible output messages) twice.

Code:
#!/bin/ksh
set -- $(uptime)
str1=${10%,}
str2=${11%,}
str3=${12}
echo str1:$str1
echo str2:$str2
echo str3:$str3
warn_level=$((1.03 * (str2 < str3 ? str2 : str3)))
crit_level=$((1.05 * (str2 < str3 ? str2 : str3)))
echo warn:$warn_level
echo crit:$crit_level
if [[ str1 -gt crit_level ]]
then	status=2
	statustxt="CPU load is critical - $str1"
elif [[ str1 -gt warn_level ]]
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"

If you need something that will work on a system running a Solaris 10 or earlier release without a ksh93, and you find that the above script does what you want on your Linux system, we'll need to look for an alternative solution.
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 06-29-2016
Hi Don,

Apology for the confuse, the line different due to I remove the 3 lines 'echo', I did not notice it.

I tried your code works fine in Solaris and SUSE! Your code is so neat Smilie
May I know what is the meaning for below coding?
Code:
warn_level=$((1.03 * (str2 < str3 ? str2 : str3)))


I found my logic mistake and changed as below yesterday (divide result from 1min CPU LOAD with 5mins and 15mins, if it is less than 1.03 (3%) then it is normal, if more than 1.03 (3) then is warning, if more than 1.05 (5%) it is critical.).

1 min load 3% < 5mins and 15mins is normal
1 min load 3% > 5mins or 15mins is warning
1 min load 5% > 5mins or 15mins is critical

Code:
#!/bin/ksh
str1=`uptime | awk '{print $10}'| sed 's/,//'`
str2=`uptime | awk '{print $11}'| sed 's/,//'`
str3=`uptime | awk '{print $12}'| sed 's/,//'`
 warn_level=$(( 1.03 ))
crit_level=$(( 1.05 ))
 if [[ $str1/$str2 -gt $crit_level || $str1/$str3 -gt $crit_level ]]; then
        status=2
        statustxt="CPU load is critical - $str1"
 elif [[ $str1/$str2 -gt $warn_level || $str1/$str3 -gt $warn_level ]]; 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"

# 11  
Old 06-30-2016
The assignment command:
Code:
warn_level=$((1.03 * (str2 < str3 ? str2 : str3)))

assigns the result of the arithmetic expression:
Code:
1.03 * (str2 < str3 ? str2 : str3)

to the variable warn_level. As in the awk programming language and in the C programming language, the expression:
Code:
expr1 ? expr2 : expr3

evaluates expr1 and if it yields a non-zero value the result of the above expression is whatever expr2 evaluates to; otherwise, the result of the above expression is whatever expr3 evaluates to. In this case, the entire expression evaluates to the multiplication of 1.03 times the minimum of $str2 and $str3.

Note also that using the shell's set special built-in, three variable assignments and 2 variable expansions is MUCH more efficient that using three command assignments each of which invokes a command substitution which in turn also invokes the external utility awk and the external utility sed.
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