Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listing percentage from sh script. 1

Status
Not open for further replies.

Dorga

MIS
Jul 30, 2003
101
NZ
Hi,

I am looking for a way to show a percentage from the range of 00.00% to 99.99%. I have tried using expr, but am only able to get 00% to 99%.

Simple example.

312 / 3020 * 100 = 10.33 or, 90.77%

with expr and bc the best I have been able to come up with is 90%.

I would gladly accept an RTFM if I just knew which M to R ;)..

Thanks..
 
man bc

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV,

Actually, it did help.

#!/bin/sh
x=`printf "%s\n" 'scale = 3; 100-(312/3274*100)' | bc`
echo $x

this returns the result as expected, 90.500.

Now, I can not figure out why I can not replace the numbers with variables.

If I make the following changes, I get this responce.

syntax error on line 1, teletype

#!/bin/sh
V1=312
V2=3274
V3=100
x=`printf "%s\n" 'scale = 3; $V3-($V1/$V2*$V3)' | bc`
echo $x

Ideas?
 
Code:
x=`printf "%s\n" [COLOR=red]"[/color]scale = 3; $V3-($V1/$V2*$V3)[COLOR=red]"[/color] | bc`

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Another trick I have used in the past is to cheat and use integer maths and then shift the decimal point. ;-)

Code:
expr 312 \* 10000 / 3020 | sed 's/\(..\)$/.\1%/'

Annihilannic.
 
In your shell man page pay attention to the shell quoting paragraph.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
vgersh99/Annihilannic/PHV,

Thank you all for your help, as noted the answer was in the man page, I just missed it.

Thanks again! =)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top