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
EXPR(1) 						    BSD General Commands Manual 						   EXPR(1)

NAME
expr -- evaluate expression SYNOPSIS
expr expression DESCRIPTION
The expr utility evaluates expression and writes the result on standard output. All operators and operands must be passed as separate arguments. Several of the operators have special meaning to command interpreters and must therefore be quoted appropriately. All integer operands are interpreted in base 10 and must consist of only an optional leading minus sign followed by one or more digits. Arithmetic operations are performed using signed integer math with a range according to the C intmax_t data type (the largest signed integral type available). All conversions and operations are checked for overflow. Overflow results in program termination with an error message on stdout and with an error status. Operators are listed below in order of increasing precedence; all are left-associative. Operators with equal precedence are grouped within symbols '{' and '}'. expr1 | expr2 Return the evaluation of expr1 if it is neither an empty string nor zero; otherwise, returns the evaluation of expr2 if it is not an empty string; otherwise, returns zero. expr1 & expr2 Return the evaluation of expr1 if neither expression evaluates to an empty string or zero; otherwise, returns zero. expr1 {=, >, >=, <, <=, !=} expr2 Return the results of integer comparison if both arguments are integers; otherwise, returns the results of string comparison using the locale-specific collation sequence. The result of each comparison is 1 if the specified relation is true, or 0 if the relation is false. expr1 {+, -} expr2 Return the results of addition or subtraction of integer-valued arguments. expr1 {*, /, %} expr2 Return the results of multiplication, integer division, or remainder of integer-valued arguments. expr1 : expr2 The ``:'' operator matches expr1 against expr2, which must be a basic regular expression. The regular expression is anchored to the beginning of the string with an implicit ``^''. If the match succeeds and the pattern contains at least one regular expression subexpression ``(...)'', the string corresponding to ``1'' is returned; otherwise the matching operator returns the number of characters matched. If the match fails and the pattern contains a regular expression subexpression the null string is returned; otherwise 0. Parentheses are used for grouping in the usual manner. The expr utility makes no lexical distinction between arguments which may be operators and arguments which may be operands. An operand which is lexically identical to an operator will be considered a syntax error. See the examples below for a work-around. The syntax of the expr command in general is historic and inconvenient. New applications are advised to use shell arithmetic rather than expr. EXIT STATUS
The expr utility exits with one of the following values: 0 the expression is neither an empty string nor 0. 1 the expression is an empty string or 0. 2 the expression is invalid. EXAMPLES
o The following example (in sh(1) syntax) adds one to the variable a: a=$(expr $a + 1) o This will fail if the value of a is a negative number. To protect negative values of a from being interpreted as options to the expr command, one might rearrange the expression: a=$(expr 1 + $a) o More generally, parenthesize possibly-negative values: a=$(expr ( $a ) + 1) o With shell arithmetic, no escaping is required: a=$((a + 1)) o This example prints the filename portion of a pathname stored in variable a. Since a might represent the path /, it is necessary to pre- vent it from being interpreted as the division operator. The // characters resolve this ambiguity. expr "//$a" : '.*/(.*)' o With modern sh(1) syntax, "${a##*/}" expands to the same value. The following examples output the number of characters in variable a. Again, if a might begin with a hyphen, it is necessary to prevent it from being interpreted as an option to expr, and a might be interpreted as an operator. o To deal with all of this, a complicated command is required: expr ( "X$a" : ".*" ) - 1 o With modern sh(1) syntax, this can be done much more easily: ${#a} expands to the required number. SEE ALSO
sh(1), test(1) STANDARDS
The expr utility conforms to IEEE Std 1003.1-2008 (``POSIX.1''). The extended arithmetic range and overflow checks do not conflict with POSIX's requirement that arithmetic be done using signed longs, since they only make a difference to the result in cases where using signed longs would give undefined behavior. According to the POSIX standard, the use of string arguments length, substr, index, or match produces undefined results. In this version of expr, these arguments are treated just as their respective string values. BSD
September 9, 2010 BSD