ksh script using expr to calculate percentages


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh script using expr to calculate percentages
# 1  
Old 04-30-2008
ksh script using expr to calculate percentages

Within a ksh script on HP-UX I trying to calculate a percentage of a number (number/100 x percentage) using the below method and expr.

TARPERC=`expr 16 / 100 \* 5`
TARSUM=`expr 16 + $TARPERC`
ZIPSUM=`expr $TARSUM \* 2`

If the input is 16

outputs are:
TARPERC: 0
TARSUM: 16
ZIPSUM: 32

The output should be 0.8 or thereabouts.

expr is dealing with the first calculation but is outputting the result as 0. Is there any way of doing this using expr or can you recommend a command and syntax that can handle floating points that can use in my script ?

hope this makes sense

Thanks
# 2  
Old 04-30-2008
What about awk?
Code:
echo |awk '{print 16 / 100 * 5}'

Regards.
# 3  
Old 04-30-2008
Thanks, that works. Additional question: How would I substitute a variable for the value of 16 ?

TARPERC=`awk '{print ${SUM} / 100 * 5}'`

Doesn't seem to work. Awk moans about the $SUM variable
# 4  
Old 04-30-2008
Code:
export KK=16
echo |awk '{print a / 100 * 5}' a=$KK

Smilie
# 5  
Old 04-30-2008
With awk :
Code:
$ sum=123
$ awk "END {print $sum / 100 * 5}" </dev/null
6.15
$ awk 'END {print '$sum' / 100 * 5}' </dev/null
6.15
$ awk 'END {print val / 100 * 5}' val=$sum </dev/null
6.15
$

Another way :
Code:
$ echo "scale=2; $sum / 100 * 5" | bc
6.15
$

Jean-Pierre.
# 6  
Old 05-01-2008
Thanks

Thank you all, managed to get to work using:

TARPERC=`echo |awk '{print a / 100 * 5}' a=$SUM`
# 7  
Old 05-01-2008
No need to use awk or expr. ksh is capable of doing want you want.
Code:
#!/bin/ksh

TARPERC=$((16.00/100.00*5.00))
TARSUM=$((16+$TARPERC))
ZIPSUM=$(($TARSUM*2))

print $TARPERC
print $TARSUM
print $ZIPSUM

which ouputs
Code:
0.8
16.8
33.6

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Diffing words - percentages

is there a way to do the following: say i have two words: WelcomeMattTom and WelcomeMTom How can i compare the two words to know how much alike, in percentages they are? like, how similar is WelcomeMTom to WelcomeMattTom? not clear yet? say i introduced a third word,... (6 Replies)
Discussion started by: SkySmart
6 Replies

2. Shell Programming and Scripting

KSH script Not working (calculate days since 1/1/2000 given day 4444)

I am unable to get this KSH script to work. Can someone help. I've been told this should work with KSH93. Which I think I have on Solaris 10. If I do a grep -i version /usr/dt/bin/dtksh I get @(#)Version M-12/28/93d @(#)Version 12/28/93 @(#)Version M-12/28/93 This is correct for... (5 Replies)
Discussion started by: thibodc
5 Replies

3. Shell Programming and Scripting

Grouping and calculation of percentages

Hi, I have a table like this, Group type L1 L2 L3 L4 L5 L6 A xx1 0 3 3 2 1 0 A xx2 2 2 2 1 7 2 B yy1 2 4 6 6 3 1 C yy2 7 7 7 0 2 3 C zz2 8 8 2 ... (6 Replies)
Discussion started by: polsum
6 Replies

4. Shell Programming and Scripting

Script to provide percentages?

so i'm have been stifled here inn my attempts at this. i need to calculate an unusual figure. what is the percentage difference between 400 and 3? usually, to get the percentage, you just divide the smaller number by the bigger number. then multiply the answer by 100. in this case... (10 Replies)
Discussion started by: SkySmart
10 Replies

5. Shell Programming and Scripting

Comparing sizes in percentages of 2 files in bash

Hi guys, I hope you can enlight me with a script I'm doing for Solaris 10. Script goes like this: #!/usr/bin/bash fechahoy=`perl /export/home/info/John/fechamod.pl` fechayer=`perl /export/home/info/John/fecha.pl` echo $fechahoy echo $fechayer DAT1=`ssh ivt@blahblah ls -la... (1 Reply)
Discussion started by: sr00t
1 Replies

6. Shell Programming and Scripting

help with expr command in script

Hi, I am trying to code a unix function to calculate date difference between two date variables. I am stuck at a point where I am trying to convert hours into minutes. Below is the code I am doing. function get_elapsed_time { export PROPS_FILE=temp.properties export... (8 Replies)
Discussion started by: Nutan
8 Replies

7. Shell Programming and Scripting

expr inside a ksh script Solaris

Hi; If I do something like this. dftotalsize=0;export dftotalsize;df -k | grep \/db001 | awk '{print $4}' | while read theinput \ ; do export $theinput; dftotalsize=`expr $dftotalsize + $theinput`; export dftotalsize; echo $dftotalsize; done ; echo `expr $dftotalsize \/ 1024 \/ 1024 "GB" Is... (4 Replies)
Discussion started by: myjess
4 Replies

8. Shell Programming and Scripting

expr error in ksh

Hi ALL, i am so much confused y the following script is not working in the korn shel which works in bash shell. please solve the error that i am facing. i want to extract the format of the size from a variable i.e. GB or KB or MB or B or BYTES code: -------- size_dir_pass=1.2gb... (2 Replies)
Discussion started by: G.K.K
2 Replies

9. Shell Programming and Scripting

calculate server uptime in % (99.98), using ksh script

Let me preface by saying, I have looked through many threads that deal with keep the decimal, however I'm not sure that any one resolution meets my needs, ok, ok, they could. So maybe it's just that I am not understanding the resolution - therefore I am posting a new thread. myknowledgebase=at... (2 Replies)
Discussion started by: cml2008
2 Replies

10. UNIX for Advanced & Expert Users

how do I get the value of expr with ksh

Hi, I have written a korn shell script to compute the value of k. formulae : a=10 b=20 c=30 k=(a+b)*c my shell script is : a=10 b=20 c=30 k=`expr (($a + $b ) * $c )` echo $k ### here paranthesis ( ) not accepting by expr function. (3 Replies)
Discussion started by: krishna
3 Replies
Login or Register to Ask a Question