Simple arithmetic in shell

 
Thread Tools Search this Thread
Operating Systems Linux Fedora Simple arithmetic in shell
# 15  
Old 05-22-2009
The only common shells that fully support floating point arithmetic are ksh93 and zsh.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell arithmetic : operations on decimal points

i am having a varialbe a , which is input to my file i want to multiply this input with value .43, and assign it to variable b. i tried it as below: #!/bin/sh a=$1 b=`expr $1\*0.43` echo b=$b error : expr: non-integer argument Please tell me , how to do this. Thanks (10 Replies)
Discussion started by: rishifrnds
10 Replies

2. Shell Programming and Scripting

Help with Arithmetic calculations in Shell script

Hi, I need a help with arithmetic calculations in my script. I have two variables: a=17; b=1712 I want to perform ($a/$b)*100 with two decimals in the result. I tried with following: res=$((100*a/b)) res=`echo "scale=2; $a / $b" | bc` But I am not getting the decimal values.... (4 Replies)
Discussion started by: karumudi7
4 Replies

3. Shell Programming and Scripting

how to do decimal arithmetic in shell script

hi, I have a file with decimal/non-decimal values $ cat b22 373 164 92 62 20 131 94 12 129 111 95 154 37 15 447 25 7.4 135 77 122 32 92 70 57 37 42 72 17 13 97 40 41 53 22 80 71 29 87 23 31 273 6.2 12K 43 44 45 22 11 7.7 13 18 173 36 20 18 13 56 67 104 53 5.4 241 19 13 3.8 38 14 31 329 16 155... (8 Replies)
Discussion started by: sam05121988
8 Replies

4. Shell Programming and Scripting

shell arithmetic least significant place

I need help on arithmetic root@server # hour=`date | awk {'print $4'} | cut -d: -f 1`; echo $hour 04 Now I subtract this result by 1 or 01 I get "3" as the answer. I need "03" as the answer, ie last two significant numbers should be there. root@server # hour=`date | awk {'print $4'} | cut... (3 Replies)
Discussion started by: anilcliff
3 Replies

5. Homework & Coursework Questions

Arithmetic Problem with shell script programming.

Hello everybody, I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input. I... (8 Replies)
Discussion started by: Florinel76
8 Replies

6. Shell Programming and Scripting

Arithmetic Problem with shell script programming.

Hello everybody, I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input. I... (1 Reply)
Discussion started by: Florinel76
1 Replies

7. Post Here to Contact Site Administrators and Moderators

Broken link FAQ date arithmetic with shell

page unix com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html Date Arithmetic with the Shell has link of www samag com/documents/s=8284/sam0307b/0307b.htm which is no longer. Is this the correct place to post this?:confused: and I got message... (1 Reply)
Discussion started by: dgerman
1 Replies

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

9. Shell Programming and Scripting

simple shell - how to get a parameter typed in a shell script

Hi, I am new to unix and using linux 7.2. I would like to create a script that would make it easyer for me to run my java programms. At the moment I have to type java myJavaprogram I am trying to write a script that will allow me to type something like this "myscript myJavaprogram" or maybe... (4 Replies)
Discussion started by: cmitulescu
4 Replies

10. Shell Programming and Scripting

Simple arithmetic operation

I have no idea why I can't get this to work, if anybody can help i would appreciate it. #!/bin/bash x=`cat counter.txt | wc -l` y= '$x / 7' printf "%d People have visited this page" $y :confused: (2 Replies)
Discussion started by: paladyn_2002
2 Replies
Login or Register to Ask a Question
PMREGISTERDERIVED(3)					     Library Functions Manual					      PMREGISTERDERIVED(3)

NAME
pmRegisterDerived - register a derived metric name and definition C SYNOPSIS
#include <pcp/pmapi.h> char *pmRegisterDerived(char *name, char *expr); cc ... -lpcp DESCRIPTION
Derived metrics provide a way of extending the Performance Metrics Name Space (PMNS) with new metrics defined at the PCP client-side using arithmetic expressions over the existing performance metrics. Typical uses would be to aggregate a number of similar metrics to provide a higher-level summary metric or to support the ``delta V over delta V'' class of metrics that are not possible in the base data semantics of PCP. An example of the latter class would be the average I/O size, defined as delta(disk.dev.total_bytes) / delta(disk.dev.total) where both of the disk.dev metrics are counters, and what is required is to to sample both metrics, compute the difference between the cur- rent and previous values and then calculate the ratio of these differences. The arguments to pmRegisterDerived are the name of the new derived metric and expr is an arithmetic expression defining how the values of name should be computed. name should follow the syntactic rules for the names of performance metrics, namely one or more components separated with a dot (``.''), and each component must begin with an alphabetic followed by zero or more characters drawn from the alphabetics, numerics and underscore (``_''). For more details, refer to PCPIntro(1) and pmns(5). name must be unique across all derived metrics and should not match the name of any regular metric in the PMNS. It is acceptable for name to share some part of its prefix with an existing subtree of the PMNS, e.g. the average I/O size metric above could be named disk.dev.avgsz which would place it amongst the other disk.dev metrics in the PMNS. Alternatively, derived metrics could populate their own subtree of the PMNS, e.g. the average I/O size metric above could be named my.summary.disk.avgsz. The expression expr follows these syntactic rules: * Terminal elements are either names of existing metrics or integer constants. Recursive definitions are not allowed, so only the names of regular metrics (not other derived metrics) may be used. Integer constants are constrained to the precision of 32-bit unsigned integers. * The usual binary arithmetic operators are supported, namely - addition (``+''), subtraction (``-''), multiplication (``*'') and division (``/'') with the normal precedence rules where multiplication and division have higher precedence than addition and subtraction, so a+b*c is evaluated as a+(b*c) * Parenthesis may be used for grouping. * The following unary functions operate on a single performance metric and return one or more values. For all functions (except count()), the type of the operand metric must be arithmetic (integer of various sizes and signedness, float or double). +---------+----------------------------------------------------+ |Function | Value | +---------+----------------------------------------------------+ |avg(x) | A singular instance being the average value across | | | all instances for the metric x. | +---------+----------------------------------------------------+ |count(x) | A singular instance being the count of the number | | | of instances for the metric x. | +---------+----------------------------------------------------+ |delta(x) | Returns the difference in values for the metric x | | | between one call to pmFetch(3) and the next. There | | | is one value in the result for each instance that | | | appears in both the current and the previous sam- | | | ple. | +---------+----------------------------------------------------+ |max(x) | A singular instance being the maximum value across | | | all instances for the metric x. | +---------+----------------------------------------------------+ |min(x) | A singular instance being the minimum value across | | | all instances for the metric x. | +---------+----------------------------------------------------+ |sum(x) | A singular instance being the sum of the values | | | across all instances for the metric x. | +---------+----------------------------------------------------+ * White space is ignored. Syntactic checking is performed at the time pmRegisterDerived is called, but semantic checking is deferred until each new context is cre- ated with pmNewContext(3) or re-establised with pmReconnectContext(3), at which time the PMNS and metadata is available to allow semantic checking and the metadata of the derived metrics to be established. SEMANTIC CHECKS AND RULES
There are a number of conversions required to determine the metadata for a derived metric and to ensure the semantics of the expressions are sound. In a binary expression, if the semantics of both operands is not a counter (i.e. PM_SEM_INSTANT or PM_SEM_DISCRETE) then the result will have semantics PM_SEM_INSTANT unless both operands are PM_SEM_DISCRETE in which case the result is also PM_SEM_DISCRETE. The mapping of the pmUnits of the metadata uses the following rules: * If both operands have a dimension of COUNT and the scales are not the same, use the larger scale and convert the values of the operand with the smaller scale. * If both operands have a dimension of TIME and the scales are not the same, use the larger scale and convert the values of the operand with the smaller scale. * If both operands have a dimension of SPACE and the scales are not the same, use the larger scale and convert the values of the operand with the smaller scale. * For addition and subtraction all dimensions for each of the operands and result are identical. * For multiplication, the dimensions of the result are the sum of the dimensions of the operands. * For division, the dimensions of the result are the difference of the dimensions of the operands. Scale conversion involves division if the dimension is positive else multiplication if the dimension is negative. If scale conversion is applied to either of the operands, the result is promoted to type PM_TYPE_DOUBLE. Putting all of this together in an example, consider the derived metric defined as follows: x = network.interface.speed - delta(network.interface.in.bytes) / delta(sample.milliseconds) The type, dimension and scale settings would propagate up the expression tree as follows. +------------------------+--------+-------------------+--------------------+ | Expression | Type | Dimension & Scale | Scale Factor(s) | +------------------------+--------+-------------------+--------------------+ |sample.milliseconds | DOUBLE | millisec | | |delta(...) | DOUBLE | millisec | | |network...bytes | U64 | byte | | |delta(...) | U64 | byte | | |delta(...) / delta(...) | DOUBLE | byte/millisec | /1048576 and *1000 | |network...speed | FLOAT | Mbyte/sec | | |x | DOUBLE | Mbyte/sec | | +------------------------+--------+-------------------+--------------------+ Because semantic checking cannot be done at the time pmRegisterDerived is called, errors found during semantic checking are reported using pmprintf(3). These include: Error: derived metric <name1>: operand: <name2>: <reason> There was a problem calling pmLookupName(3) to identify the operand metric <name2> used in the definition of the derived metric <name1>. Error: derived metric <name1>: operand (<name2> [<pmid2>]): <reason> There was a problem calling pmLookupDesc(3) to identify the operand metric <name2> with PMID <pmid2> used in the definition of the derived metric <name1>. Semantic error: derived metric <name>: <operand> <op> <operand>: Illegal operator for counters If both operands have the semantics of counter, only addition or subtraction make sense, so multiplication and division are not allowed. Semantic error: derived metric <name>: <operand> <op> <operand>: Illegal operator for counter and non-counter Only multiplication or division are allowed if the left operand has the semantics of a counter and the right operand is not a counter. Semantic error: derived metric <name>: <operand> <op> <operand>: Illegal operator for non-counter and counter Only multiplication is allowed if the right operand has the semantics of a counter and the left operand is not a counter. Semantic error: derived metric <name>: <operand> <op> <operand>: Non-arithmetic type for <left-or-right> operand The binary arithmetic operators are only allowed with operands with an arithmetic type (integer of various sizes and signedness, float or double). Semantic error: derived metric <name>: <function>(<operand>): Non-arithmetic operand for function The unary functions are only defined if the operand has arithmetic type. EXPRESSION EVALUATION
For the binary arithmetic operators, if either operand must be scaled (e.g. convert bytes to Kbytes) then the result is promoted to PM_TYPE_DOUBLE. Otherwise the type of the result is determined by the types of the operands, as per the following table which is evaluated from top to bottom until a match is found. +--------------------------------+----------+----------------+ | Operand Types | Operator | Result Type | +--------------------------------+----------+----------------+ |either is PM_TYPE_DOUBLE | any | PM_TYPE_DOUBLE | +--------------------------------+----------+----------------+ |any | division | PM_TYPE_DOUBLE | +--------------------------------+----------+----------------+ |either is PM_TYPE_FLOAT | any | PM_TYPE_FLOAT | +--------------------------------+----------+----------------+ |either is PM_TYPE_U64 | any | PM_TYPE_U64 | +--------------------------------+----------+----------------+ |either is PM_TYPE_64 | any | PM_TYPE_64 | +--------------------------------+----------+----------------+ |either is PM_TYPE_U32 | any | PM_TYPE_U32 | +--------------------------------+----------+----------------+ |otherwise (both are PM_TYPE_32) | any | PM_TYPE_32 | +--------------------------------+----------+----------------+ CAVEATS
Unary negation is not supported, so the following expressions would be syntactically incorrect, -3*abc and -this.number Derived metrics are not available when using pmFetchArchive(3) as this routine does not use a target list of PMIDs that could be remapped (as is done for pmFetch(3)). pmRegisterDerived does not apply retrospectively to any open contexts, so the normal use would be to make all calls to pmRegisterDerived (possibly via pmLoadDerivedConfig(3)) and then call pmNewContext(3). There is no pmUnregisterDerived method, so once registered a derived metric persists for the life of the application. DIAGNOSTICS
On success, pmRegisterDerived returns NULL. If a syntactic error is found at the time of registration, the value returned by pmRegisterDerived is a pointer into expr indicating where the error was found. To identify what the error was, the application should call pmDerivedErrStr(3) to retrieve the corresponding parser error message. SEE ALSO
PCPIntro(1), PMAPI(3), pmDerivedErrStr(3), pmFetch(3), pmLoadDerivedConfig(3), pmNewContext(3) and pmReconnectContext(3). Performance Co-Pilot PMREGISTERDERIVED(3)