Operating System - HP-UX
1833863 Members
2259 Online
110063 Solutions
New Discussion

Re: Addition in Shell Script

 
SOLVED
Go to solution
Ryan B
Frequent Advisor

Addition in Shell Script

I have the following requirement for a shell script:

count=555.07

I want count to increment by ".01" based on my needs at a given time. I tried using some variations with "expr" to make the next count "555.08" in this example, but it does not seem to like the ".##". It returns the error "expr: An integer value was expected." When I do it with 555 and add 1, it works great.

Please let me know if I need some quoutes in special places or if I should be using something else besides expr...

Thanks for you help!
14 REPLIES 14
John Palmer
Honored Contributor

Re: Addition in Shell Script

The Korn/Posix shell is only capable of integer arithmetic. It can do this internally without the need for 'expr' which being an external command is relatively inefficient.

It may be possible to achieve what you want using integer arithmetic but you'll have to program around it. If you want 2 places of decimals then you'll have to work with your number multiplied by 100 (in your case 55507).

Something like this:-

typeset -i10 count iii ddd
typeset -Z2 ddd

let count=55507
let count=count+1

To display count as a real number you will have to seperate it into two parts. The integer part is easy:-

let iii=count/100

The decimal part is a bit harder:-
let ddd="count-(100*iii)"

You can then print it with:
print ${iii}.${ddd}

The typeset -Z2 ddd ensures that $ddd has a leading zero when it has a value less than 10 as in your case.

Regards,
John
Stefan Stechemesser
Honored Contributor

Re: Addition in Shell Script

what about perl ?

count=`perl -e 'print $ARGV[0] + 0.01' $count`

another good idea would be to set count to 55507 and increase it by 1 during each loop.

Best regards

Stefan
Elena Leontieva
Esteemed Contributor

Re: Addition in Shell Script

The expr command can perform simple integer operations. For floating point arithmetic, the awk or bc utilities can be used.

HTH,
Elena.
Rita C Workman
Honored Contributor

Re: Addition in Shell Script

awk also does a good job with floating point values...

Here's an example of something you could test with quickly.

Create a dummy file 'grades' that looks like this:

You 100 99.8 69.4
Me 99.2 78.9 88.3
Other 84.3 96.21 90.33

Now create a dummy awk script that just says something like:

awk '{ sum =$2 + $3 + $4 ; avg = sum / 3
print $1, avg }' grades

Run the dummy awk script and you will see that it added the grades and calculated the avg grade for each and prints it....floating point and all.

Rgrds,
....I luv awk....
Rita

curt larson_1
Honored Contributor

Re: Addition in Shell Script

1) you could use a shell that supports floating point numbers

#!/usr/dt/bin/dtksh

typeset -F inc=.01 count

count=555.07
count=$(( $count + $inc ))
print $count

or use bc as a co process

count=555.07
# This starts bc with a two-way pipe
bc |&
# print -p writes to the pipe
print -p "$count + .01"
# read -p reads from the pipe
read -p count
print $count
Leif Halvarsson_2
Honored Contributor

Re: Addition in Shell Script

Hi,
Just an addition to John Palmers reply.

There is actually a modulus operator in ksh (modulus= the rest in an integer division).

try the following:

let iii=count/100

let ddd=count%100

print ${iii}.${ddd}
Michael Schulte zur Sur
Honored Contributor
Solution

Re: Addition in Shell Script

Hi,

try this:
COUNT=1
COUNT=`echo 0.01 + ${COUNT}|bc`

greetings,

Michael
Ryan B
Frequent Advisor

Re: Addition in Shell Script

Wow, thanks for all the responses. I am going through each one and then will let you know what works best.
Elena Leontieva
Esteemed Contributor

Re: Addition in Shell Script

count=555.07

Inside the loop put the following line:

count=`awk -v x=$count -v y=0.01 'BEGIN{ printf "%.2f\n", x+y}'`

Elena.
Ryan B
Frequent Advisor

Re: Addition in Shell Script

The bc and awk methods, last 2 posts, worked the best for my situation. However, I appreciate the rest of the responses as I learned other methods for the future. Thanks again and have great New Year!!

Ryan
Ryan B
Frequent Advisor

Re: Addition in Shell Script

Elena,

Regarding the awk command, what does the ""%.2f\n" in printf mean?

Ryan
Elena Leontieva
Esteemed Contributor

Re: Addition in Shell Script

Ryan,

It means that the printf function prints the result with a precision of two places to the right of the decimal point.

Elena.
Alex Ostapenko
Advisor

Re: Addition in Shell Script

I like Elena's awk solution also. See "man printf" under the Conversion Specifications section for detailed info about the different types of formatting you can do in "printf" under awk.
Rich Polk
New Member

Re: Addition in Shell Script

I'd like to do the opposite, change 555.07 to 556.07. I can get the first 3 integers
to change, but the last 2 will not show.

Thanks for any pointers...

Rich