- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: bc doesnt do maths
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-20-2008 05:11 AM
тАО11-20-2008 05:11 AM
(15/31)*100 = 48.387096774
or
(15/31)*100 = 48
how can I do the above maths using shell script ?
bc say/produce '0' when 15 is divided by 31
please help
Regards
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-20-2008 05:38 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-20-2008 05:39 AM
тАО11-20-2008 05:39 AM
Re: bc doesnt do maths
echo "scale=2; (15/31)*100" | bc
the scale=2 is just to tell bc that you want only 2 decimals.
quote from http://www.kingcomputerservices.com/unix_101/using_bc_part_1.htm
"The number of decimals to be printed by bc is controlled by the scale variable. The default scale is 0."
Best regards
Fredrik Eriksson
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-20-2008 05:41 AM
тАО11-20-2008 05:41 AM
Re: bc doesnt do maths
If you want to use 'bc' add the 'scale' factor to define the number of digits to the right of the decimal place:
# echo "scale=9;(15/31)*100"| bc
48.387096700
Other ways to accomplish your task, include 'awk' and 'perl' or a shell line Korn93 that supports real numbers:
# awk 'BEGIN{printf "%2.9f\n",(15/31)*100}'
48.387096774
# perl -e 'printf "%2.9f\n",(15/31)*100'
48.387096774
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-21-2008 03:48 AM
тАО11-21-2008 03:48 AM
Re: bc doesnt do maths
please help me in the following code
#!/bin/bash
a=19
b=17
s1=$(echo "($a/31)*100" | bc -l)
s2=$(echo "($b/31)*100" | bc -l)
#s1=$(awk 'BEGIN{printf "%2.0f\n",($a/31)*100}')
#s2=$(awk 'BEGIN{printf "%2.0f\n",($b/31)*100}')
if [ $s1 -gt $s2 ]; then echo "Holiday"
else echo "More work"
fi
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-21-2008 04:14 AM
тАО11-21-2008 04:14 AM
Re: bc doesnt do maths
line 12: [: 61.29032258064516129000: integer expression expected
then add scale=0 to bc.
#!/bin/bash
a=19
b=17
s1=$(echo "scale=0;($a/31)*100" | bc -l)
s2=$(echo "scale=0;($b/31)*100" | bc -l)
#s1=$(awk 'BEGIN{printf "%2.0f\n",($a/31)*100}')
#s2=$(awk 'BEGIN{printf "%2.0f\n",($b/31)*100}')
if [ $s1 -gt $s2 ]; then echo "Holiday"
else echo "More work"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-21-2008 08:47 PM
тАО11-21-2008 08:47 PM
Re: bc doesnt do maths
>line 12: [: 61.29032258064516129000: integer expression expected
>then add scale=0 to bc.
vi script.sh
#!/bin/bash
.
.
s1=$(echo "scale=0;($a/31)*100" | bc -l)
s2=$(echo "scale=0;($b/31)*100" | bc -l)
.
.
# bash -x script.sh
+ a=19
+ b=17
++ echo 'scale=0;(19/31)*100'
++ bc -l
+ s1=0
++ echo 'scale=0;(17/31)*100'
++ bc -l
+ s2=0
+ '[' 0 -gt 0 ']'
+ echo 'More work'
More work
when using "scale=0", you can check/see that s1 and s2 both equals to 0
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-23-2008 06:46 PM
тАО11-23-2008 06:46 PM
Re: bc doesnt do maths
echo "(17/31)*100" | bc -l | cut -f 1 -d "."
To remove the decimals.