- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: multiplication in a shell script
Operating System - Linux
1762202
Members
3729
Online
108906
Solutions
Forums
Categories
Company
Local Language
юдл
back
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
юдл
back
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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
тАО12-08-2006 07:34 AM
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-08-2006 07:40 AM
тАО12-08-2006 07:40 AM
Solution
Hi Jacques:
Most shells only support integer arithmetic.
# X=100;Y=25;((Z=X/Y));echo $Z
If you want to incorporate real numbers, 'bc' provides a convenient vehicle. Choose the 'scale' to set the precision you want:
# X=1;Y=8;echo "scale=3\n $X/$Y"|bc
You can also leverage 'awk' and couple it with 'printf' to format output, including moving to a different number base:
# X=1;Y=8;echo "$X $Y"|awk '{printf "%.4f\n",$1/$2}'
# X=7;Y=8;echo "$X $Y"|awk '{printf "%4x\n",$1+$2}'
If you use the Korn93 shell, its 'typeset' allows a more direct approach to real numbers:
#!/usr/dt/bin/dtksh
typeset -F R1
typeset -F3 R2
typeset -E R3
let R1=1/8
echo $R1
let R2=1/8
echo $R2
let R3=1/8
echo $R3
exit 0
Regards!
...JRF...
Most shells only support integer arithmetic.
# X=100;Y=25;((Z=X/Y));echo $Z
If you want to incorporate real numbers, 'bc' provides a convenient vehicle. Choose the 'scale' to set the precision you want:
# X=1;Y=8;echo "scale=3\n $X/$Y"|bc
You can also leverage 'awk' and couple it with 'printf' to format output, including moving to a different number base:
# X=1;Y=8;echo "$X $Y"|awk '{printf "%.4f\n",$1/$2}'
# X=7;Y=8;echo "$X $Y"|awk '{printf "%4x\n",$1+$2}'
If you use the Korn93 shell, its 'typeset' allows a more direct approach to real numbers:
#!/usr/dt/bin/dtksh
typeset -F R1
typeset -F3 R2
typeset -E R3
let R1=1/8
echo $R1
let R2=1/8
echo $R2
let R3=1/8
echo $R3
exit 0
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-08-2006 08:17 AM
тАО12-08-2006 08:17 AM
Re: multiplication in a shell script
thanks 10 points submitted
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-08-2006 08:18 AM
тАО12-08-2006 08:18 AM
Re: multiplication in a shell script
this is what I was looking for THX
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP