Sponsored Content
Top Forums Shell Programming and Scripting BC calculation for floating (invalid arithmetic operator ) Post 302976491 by Don Cragun on Wednesday 29th of June 2016 08:38:16 PM
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 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 06:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy