- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: UNIX arithmetic
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
тАО08-29-2005 09:55 AM
тАО08-29-2005 09:55 AM
I have a bsic quetion on Unix aritmetic.
Below is the sample script
val1=16019
val2=2
val3=`expr $val1 / $val2`
the result should actually be "8009.5" but I am getting "8009".
Can anyone please tell me what I need to do to get the value "8009.5" as the result.
Thanks
Rahul
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-29-2005 10:03 AM
тАО08-29-2005 10:03 AM
Re: UNIX arithmetic
use "bc" for math.
echo 3.5+2|bc
5.5
or echo $val1/$val2|bc
you get the idea.
- Tags:
- bc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-29-2005 10:06 AM
тАО08-29-2005 10:06 AM
Re: UNIX arithmetic
Hold on.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-29-2005 10:09 AM
тАО08-29-2005 10:09 AM
Re: UNIX arithmetic
http://www.shelldorado.com/scripts/cmds/calc
UNIX because I majored in cryptology...
- Tags:
- shelldorado
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-29-2005 10:10 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-29-2005 10:22 AM
тАО08-29-2005 10:22 AM
Re: UNIX arithmetic
Use the command line example suggested by Tim. You'll have to do the same operation in case of further manipulation of the string stored inside of val3 ex. if you need to multiply val3 by 3 to obtain 24028.5 then...
# val4=`echo "$val3*3"|bs`
hope it helps!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-29-2005 11:27 AM
тАО08-29-2005 11:27 AM
Re: UNIX arithmetic
regards!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-30-2005 04:24 AM
тАО08-30-2005 04:24 AM
Re: UNIX arithmetic
Now shell variables can be assigned values such as val9=123.9567 and it will look correct. That is because the shell defaults all variables to strings, not numbers. And when a variable is used in shell arithmetic, the value is reduced to an integer by truncation (not rounding). So the examp,e val9=123.9567 will be used as 123 and not rounded to 124. If you need to make calculations that may result in a fractional part, use the shell variables as string holders and do all the calculations in bc.
Bill Hassell, sysadmin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-30-2005 04:38 AM
тАО08-30-2005 04:38 AM
Re: UNIX arithmetic
You have to do it with "awk".
Say you want to divide 5.12 by 21.56:
TIME=21.56
echo 5.12 $TIME | awk '{printf "%.2f\n", $1 / $2 }'
yields:
0.24
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-30-2005 04:45 AM
тАО08-30-2005 04:45 AM
Re: UNIX arithmetic
Some modern shells, notably Korn93 and decendents, DO HAVE the ability to perform real arithmetic. You use the '-F' or -'E' typeset options.
The '-E' option specifies scientific notation. An option count (n) signifies the number of significant places. Similarly, '-F' asks for floating point notation with an option n-count signaling the number of decimal places.
On HP-UX, you can find a version as:
/usr/dt/bin/dtksh
By example:
#!/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
...produces as output:
.1250000000
.125
0.125
Regards!
...JRF...
- Tags:
- dtksh