- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- decimals in shell script
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
Forums
Discussions
Discussions
Discussions
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
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
03-20-2001 06:30 AM
03-20-2001 06:30 AM
decimals in shell script
ex)
var=5.6
echo $var (only want 5 to show)
Thanks,
Tony
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2001 06:52 AM
03-20-2001 06:52 AM
Re: decimals in shell script
One way is to use 'awk' with the "." as the field delimiter:
# V=5.6
# echo $V|awk -F. '{print $1}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2001 06:52 AM
03-20-2001 06:52 AM
Re: decimals in shell script
integer i
var=5.6
i=$var
echo $i
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2001 06:55 AM
03-20-2001 06:55 AM
Re: decimals in shell script
With printf;
printf "%5.0f" $var
Greetz, Danny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2001 06:57 AM
03-20-2001 06:57 AM
Re: decimals in shell script
just another way:
val=5.6
echo ${val%.*}
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2001 07:00 AM
03-20-2001 07:00 AM
Re: decimals in shell script
Provided the value supplied is a valid number
(the shell will print a failure message to standard error if it isn't) then
let VAR=
print ${VAR}
will do the trick. The let command implies 'integer VAR' which is an alias to 'typeset -i VAR'
Regards,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2001 07:07 AM
03-20-2001 07:07 AM
Re: decimals in shell script
...and here's yet another:
# V=5.6
# echo "$V / 1"|bc
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2001 07:08 AM
03-20-2001 07:08 AM
Re: decimals in shell script
#!/usr/dt/bin/dtksh
var=5.6
print $(( var=int($var) ))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2001 12:25 PM
03-20-2001 12:25 PM
Re: decimals in shell script
#!/bin/sh
#----------------------------------------------------------
# Script Written by: Brian Markus - bmarkus@rcoe.k12.ca.us
# Reason - pure boredom, and to help a fellow hpux'er
# This script will take any size decimal
# number assign the left side of the
# decimal to a variable named predec,
# then assign the right side of the decimal
# to a variable named postdec
#----------------------------------------------------------
# Set your variable with the decimal
# assign the the decimal number to decnum
decnum=549890.398
#display your variable before altering it
echo "pre number = $decnum"
#get the length of the variable
varlen=`expr length $decnum`
#find the position that the decimal is in
decpos=`expr index $decnum \.`
#set the end point for the left side of the decimal
let decpos=decpos-1
#assign the left side of the decimal to predec
predec=`expr substr $decnum 1 $decpos`
#set the beginning point on the right side of the decimal
let decpos=decpos+2
#assign the right side of the decimal to postdec
postdec=`expr substr $decnum $decpos $varlen`
#put the left and right side together with out the decimal
newnumber=${predec}${postdec}
#display the new built number
echo "post number = $newnumber"
hope this helps
Brian.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2001 12:31 PM
03-20-2001 12:31 PM