Need help in calucaltion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in calucaltion
# 1  
Old 09-13-2007
Error in Calculating a Expression

Hi

I am trying to calulate the values with this script

/* Script will estimate the values by 15 % */

echo " Enter the directory name "
read value1
value3=`(du -k $value1 | tail -1 | awk '{print $1}')`
echo " Souce directory file size is $value3 "

val=$value3 + `($value3 * 0.15)`

echo " Excepted value is $val"

I am getting this output
----------------------
bash-2.03$ ./file2
Enter the directory name
/home/sp51182
Souce directory file size is 17
./file2: 17: command not found
./file2: +: command not found
Excepted value is

Please help me in solving this problem.

Last edited by padarthy; 09-14-2007 at 12:10 AM.. Reason: Spelling mistake
# 2  
Old 09-13-2007
calucaltion?
# 3  
Old 09-14-2007
Sorry yaar...

Its Calculation... I am calulating the estimated size by 15 %.

Please help me
# 4  
Old 09-14-2007
have you tried using "bc"?

man bc
# 5  
Old 09-14-2007
No.

The output of the expression will be in decimal format ( eg 563.655 ).

kindly advice
# 6  
Old 09-14-2007
Code:
val=$value3 + `($value3 * 0.15)`

Is not going to work. Think of shell as just a thing that runs programs and has some macro processing around it.

Either use 'bc' to do the calculations or consider using a different language to do the job.
# 7  
Old 09-14-2007
Quote:
Originally Posted by padarthy
No.

The output of the expression will be in decimal format ( eg 563.655 ).

kindly advice
kindly look into using the 'bc' as previously advised for your 'calulation':
Code:
#!/bin/ksh

size=$(du -k . | tail -1 | nawk '{print $1}')

echo "size->[${size}]"

calulation=$(echo "scale=2; ${size} * 0.15" | bc)
echo "calulation->[${calulation}]"

Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question