- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Addition 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
12-30-2003 01:59 AM
12-30-2003 01:59 AM
count=555.07
I want count to increment by ".01" based on my needs at a given time. I tried using some variations with "expr" to make the next count "555.08" in this example, but it does not seem to like the ".##". It returns the error "expr: An integer value was expected." When I do it with 555 and add 1, it works great.
Please let me know if I need some quoutes in special places or if I should be using something else besides expr...
Thanks for you help!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2003 02:38 AM
12-30-2003 02:38 AM
Re: Addition in Shell Script
It may be possible to achieve what you want using integer arithmetic but you'll have to program around it. If you want 2 places of decimals then you'll have to work with your number multiplied by 100 (in your case 55507).
Something like this:-
typeset -i10 count iii ddd
typeset -Z2 ddd
let count=55507
let count=count+1
To display count as a real number you will have to seperate it into two parts. The integer part is easy:-
let iii=count/100
The decimal part is a bit harder:-
let ddd="count-(100*iii)"
You can then print it with:
print ${iii}.${ddd}
The typeset -Z2 ddd ensures that $ddd has a leading zero when it has a value less than 10 as in your case.
Regards,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2003 02:50 AM
12-30-2003 02:50 AM
Re: Addition in Shell Script
count=`perl -e 'print $ARGV[0] + 0.01' $count`
another good idea would be to set count to 55507 and increase it by 1 during each loop.
Best regards
Stefan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2003 02:50 AM
12-30-2003 02:50 AM
Re: Addition in Shell Script
HTH,
Elena.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2003 03:02 AM
12-30-2003 03:02 AM
Re: Addition in Shell Script
Here's an example of something you could test with quickly.
Create a dummy file 'grades' that looks like this:
You 100 99.8 69.4
Me 99.2 78.9 88.3
Other 84.3 96.21 90.33
Now create a dummy awk script that just says something like:
awk '{ sum =$2 + $3 + $4 ; avg = sum / 3
print $1, avg }' grades
Run the dummy awk script and you will see that it added the grades and calculated the avg grade for each and prints it....floating point and all.
Rgrds,
....I luv awk....
Rita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2003 03:45 AM
12-30-2003 03:45 AM
Re: Addition in Shell Script
#!/usr/dt/bin/dtksh
typeset -F inc=.01 count
count=555.07
count=$(( $count + $inc ))
print $count
or use bc as a co process
count=555.07
# This starts bc with a two-way pipe
bc |&
# print -p writes to the pipe
print -p "$count + .01"
# read -p reads from the pipe
read -p count
print $count
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2003 04:57 AM
12-30-2003 04:57 AM
Re: Addition in Shell Script
Just an addition to John Palmers reply.
There is actually a modulus operator in ksh (modulus= the rest in an integer division).
try the following:
let iii=count/100
let ddd=count%100
print ${iii}.${ddd}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2003 05:46 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2003 06:34 AM
12-30-2003 06:34 AM
Re: Addition in Shell Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2003 06:38 AM
12-30-2003 06:38 AM
Re: Addition in Shell Script
Inside the loop put the following line:
count=`awk -v x=$count -v y=0.01 'BEGIN{ printf "%.2f\n", x+y}'`
Elena.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2003 07:07 AM
12-30-2003 07:07 AM
Re: Addition in Shell Script
Ryan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2003 07:17 AM
12-30-2003 07:17 AM
Re: Addition in Shell Script
Regarding the awk command, what does the ""%.2f\n" in printf mean?
Ryan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2003 07:27 AM
12-30-2003 07:27 AM
Re: Addition in Shell Script
It means that the printf function prints the result with a precision of two places to the right of the decimal point.
Elena.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-01-2004 11:01 PM
01-01-2004 11:01 PM
Re: Addition in Shell Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2004 07:20 AM
05-10-2004 07:20 AM
Re: Addition in Shell Script
to change, but the last 2 will not show.
Thanks for any pointers...
Rich