- Community Home
- >
- Servers and Operating Systems
- >
- Operating System - Linux
- >
- General
- >
- bc doesnt do maths
-
- Forums
-
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
- HPE Blog, Austria, Germany & Switzerland
- Blog HPE, France
- HPE Blog, Italy
- HPE Blog, Japan
- HPE Blog, Middle East
- HPE Blog, Latin America
- HPE Blog, Russia
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
-
Blogs
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Blog, Latin America
- HPE Blog, Middle East
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
-
Information
- Community
- Welcome
- Getting Started
- FAQ
- Ranking Overview
- Rules of Participation
- Tips and Tricks
- Resources
- Announcements
- Email us
- Feedback
- Information Libraries
- Integrated Systems
- Networking
- Servers
- Storage
- Other HPE Sites
- Support Center
- Aruba Airheads Community
- Enterprise.nxt
- HPE Dev Community
- Cloud28+ Community
- Marketplace
-
Forums
-
Blogs
-
Information
-
English
- 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
- Email to a Friend
- 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
- Email to a Friend
- Report Inappropriate Content
11-20-2008 05:38 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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
- Email to a Friend
- 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.
Hewlett Packard Enterprise International
- Communities
- HPE Blogs and Forum
© Copyright 2021 Hewlett Packard Enterprise Development LP