Numeric calculation in shell scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Numeric calculation in shell scripting
# 1  
Old 04-12-2013
Numeric calculation in shell scripting

Hi Team,

how can i calculate the number as below in shell script for below expression.
Code:
34 /50 * 100 equals to 68%

now how i would represent this in shell script.


Thanks,
Jewel

Last edited by Franklin52; 04-17-2013 at 03:22 AM.. Reason: Please use code tags
# 2  
Old 04-12-2013
Code:
$ x=$(( 100 * 34 / 50 ))
$ echo $x
68

# 3  
Old 04-12-2013
Hi friend,

this is fine on command prompt. but in the shell script this expression gives below error.
Code:
bkpreport.sh[380]: test: Specify a parameter with this command.
bkpreport.sh[384]: test: Specify a parameter with this command.
68

Thanks,
Jewel

Last edited by Franklin52; 04-12-2013 at 03:03 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 04-12-2013
What shell are you using? I was using bash. Perhaps your shell does not support that syntax. Here is a way to do it no matter which shell:
Code:
$ x=`expr 100 \* 34 / 50`
$ echo $x
68

Or another way in bash:
Code:
$ let x="100 * 34 / 50"
$ echo $x
68

# 5  
Old 04-12-2013
Can you post your script?
# 6  
Old 04-12-2013
Here it is in a shell script:
Code:
$ cat temp.sh
x=$(( 100 * 34 / 50 ))
echo $x

x=`expr 100 \* 34 / 50`
echo $x

let x="100 * 34 / 50"
echo $x

Code:
$ ./temp.sh
68
68
68

With perhaps some exception in some special case, if it works on the command line, it will work in a script.
# 7  
Old 04-12-2013
i am using /bin/sh with HP-UX.

and getting same error
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to do simple date (time) calculation in shell script?

Hi, I'm looking for a way to do a simple math calc during a shell script as a means of logging how long a particular task takes. For example... STARTTIME=whenever this script starts ./path/to/command.sh >>logfile.log TOTALTIME=<time at this stage of the script after above command... (7 Replies)
Discussion started by: nbsparks
7 Replies

2. Shell Programming and Scripting

Math calculation over shell

Hi I am trying to calculate the rate at which something is happening. I have 2 files- a1 and b1. I want to calculate something like this ((wc -l a1)/(wc -l a1 + wc -l b1))*100 over a loop for different a and b. Is this possible, help me out fellas. Thanks a lot :) (5 Replies)
Discussion started by: jamie_123
5 Replies

3. Shell Programming and Scripting

Modulo calculation in shell script

hi i am writing a progrm to print the even numbers and the code which i am following is as follows #!/usr/bin/bash echo "enter a number a" read a if then echo "the number is even" else echo "the number is odd" fi ~ what is the mistake i am doing ...please tell me (3 Replies)
Discussion started by: harjinder
3 Replies

4. Shell Programming and Scripting

KSH SHELL: problem calculation number of lines inside compressed file

Hi Gurus, I am working with a korn shell script to simplify some operations of calculation number of lines inside compressed file. The called function (inside a cycle) is the following: ######################################### # F.ne: CheckCount #########################################... (3 Replies)
Discussion started by: GERMANICO
3 Replies

5. UNIX for Dummies Questions & Answers

Find and Replace random numeric value with non-numeric value

Can someone tell me how to change the first column in a very large 17k line file from a random 10 digit numeric value to a non numeric value. The format of lines in the file is: 1702938475,SNU022,201004 the first 10 numbers always begin with 170 (6 Replies)
Discussion started by: Bahf1s
6 Replies

6. Shell Programming and Scripting

calculation using awk or shell script in between the numbers

file A E969K D223L E400L E34L file B predicted 3 1 250 251 500 501 1000 The output should be E969K 501 1000 D223L 1 250 E400L 251 500 E34L 1 250 I tried in this way (1 Reply)
Discussion started by: cdfd123
1 Replies

7. 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

8. Shell Programming and Scripting

Need help in shell script to trim numeric values

Hello I am currently working on a shell script which actually needs to pull some file from a server , The filenames will have the extension with date/time stamp , i need to trim that and name it to proper format Example : xyz_20091108_22142365.gzip i need to remove the... (5 Replies)
Discussion started by: ranga27
5 Replies

9. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies

10. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies
Login or Register to Ask a Question