Numeric calculation in shell scripting

Hi Team,

how can i calculate the number as below in shell script for below expression.

34 /50 * 100 equals to 68%

now how i would represent this in shell script.

Thanks,
Jewel

$ x=$(( 100 * 34 / 50 ))
$ echo $x
68

Hi friend,

this is fine on command prompt. but in the shell script this expression gives below error.

bkpreport.sh[380]: test: Specify a parameter with this command.
bkpreport.sh[384]: test: Specify a parameter with this command.
68

Thanks,
Jewel

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:

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

Or another way in bash:

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

Can you post your script?

Here it is in a shell script:

$ cat temp.sh
x=$(( 100 * 34 / 50 ))
echo $x

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

let x="100 * 34 / 50"
echo $x
$ ./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.

i am using /bin/sh with HP-UX.

and getting same error

With all three ways? Even with the expr way? The expr way should work everywhere, because expr is not part of the shell: expr is a separate Unix command, and should work for doing basic math the same no matter what system.

That you are using /bin/sh explains why the other two ways (not expr) possibly do not work, because the shell on the system may not support those other syntaxes (#1 and #3 in the shell script).

It would help if you would show the error, using code tags.

The error clearly indicates that you didn't show us your complete code and is not related to the arithmetic operation.

I can see that the arithmetic operation was successful and the result 68 got printed.

thanks dear it is working fine
but i not able to print floating value.

Regards,
Jewel

In HP-UX, the shells are limited to integer arithmetic.

So you can use bc to perform floating point arithmetic.

Refer bc man pages:

man bc

Hi,
now i am using in script, but not getting floating value

x1=`expr 100 \* $Q4Complete / $Tot1 `
x2=`expr 100 \* $verror1  / $Tot1 `
x3=`expr 100 \*  $submitted1 /  $Tot1 `

Thanks,
Jewel

Please note that expr is also limited to integer arithmetic.

Use bc instead:

x1=$( echo "scale=4; 100 * ( $Q4Complete / $Tot1 )" | bc )

by using below code ,not getting floating value which is equals to 0

echo "  .035 / 1862 " | bc
 

need help urgent asap!!!

Except /usr/dt/bin/dtksh :

$ echo $(( 34. /50 * 100 ))
68
$ echo $(( .035 / 1862 ))
0.000018796992
1 Like

Be patient, we are not on call!

Use -l option that causes an arbitrary-precision math library to be predefined:

$ echo "  .035 / 1862 " | bc -l
.00001879699248120300

Everyone at the UNIX and Linux Forums gives their best effort to reply to all questions in a timely manner. For this reason, posting questions with subjects like "Urgent!" or "Emergency" and demanding a fast reply are not permitted in the regular forums.

For members who want a higher visibility to their questions, we suggest you post in the Emergency UNIX and Linux Support Forum. This forum is given a higher priority than our regular forums.

Posting a new question in the Emergency UNIX and Linux Support Forum requires forum Bits. We monitor this forum to help people with emergencies, but we do not not guarantee response time or best answers. However, we will treat your post with a higher priority and give our best efforts to help you.

Thank you.

The UNIX and Linux Forums

Good to know about dtksh

But I discovered that it is part of CDE, so dtksh will not be available if CDE is not installed.

Yes that is a good point. In my experience CDE used to be installed by default, so it was there unless the administration team had made a conscious choice to uninstall it / not to install it , but it appears it has become optional in HP-UX 11.31, so there CDE would need to be installed first..

friend,

this command is working fine on command prompt with using variable. But while i using variable with same code in in script, it doesnot working.

Thanks,
Jewel

---------- Post updated at 02:28 PM ---------- Previous update was at 12:28 PM ----------

need help from you all