shell arithmetic least significant place


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell arithmetic least significant place
# 1  
Old 11-02-2011
shell arithmetic least significant place

I need help on arithmetic
Code:
root@server [~]# hour=`date | awk {'print $4'} | cut -d: -f 1`; echo $hour
04

Now I subtract this result by 1 or 01 I get "3" as the answer. I need "03" as the answer, ie last two significant numbers should be there.
Code:
root@server [~]# hour=`date | awk {'print $4'} | cut -d: -f 1`;lasthour=`expr $hour - 01`; echo $lasthour
3

10 - 8 = 02
15 - 10 = 05

25 - 10 = 15
30 - 12 - 18

Like above

Last edited by anilcliff; 11-02-2011 at 08:14 AM..
# 2  
Old 11-02-2011
printf %02d $lasthour instead of echo.
This User Gave Thanks to CarloM For This Post:
# 3  
Old 11-02-2011
thanks it worked
# 4  
Old 11-02-2011
If you are in ksh, you can force the format of your variable:-
Code:
typeset -Z2 currhour=`date +%H`         # Just get the hour, check the format for your OS with man date
((prevhour=$currhour-1))
echo "$currhour - 1 = $prevhour

You need to consider how to handle hour zero though.



I hope that this helps,
Robin
Liverpool/Blackburn
UK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell arithmetic : operations on decimal points

i am having a varialbe a , which is input to my file i want to multiply this input with value .43, and assign it to variable b. i tried it as below: #!/bin/sh a=$1 b=`expr $1\*0.43` echo b=$b error : expr: non-integer argument Please tell me , how to do this. Thanks (10 Replies)
Discussion started by: rishifrnds
10 Replies

2. Shell Programming and Scripting

Help with Arithmetic calculations in Shell script

Hi, I need a help with arithmetic calculations in my script. I have two variables: a=17; b=1712 I want to perform ($a/$b)*100 with two decimals in the result. I tried with following: res=$((100*a/b)) res=`echo "scale=2; $a / $b" | bc` But I am not getting the decimal values.... (4 Replies)
Discussion started by: karumudi7
4 Replies

3. Shell Programming and Scripting

how to do decimal arithmetic in shell script

hi, I have a file with decimal/non-decimal values $ cat b22 373 164 92 62 20 131 94 12 129 111 95 154 37 15 447 25 7.4 135 77 122 32 92 70 57 37 42 72 17 13 97 40 41 53 22 80 71 29 87 23 31 273 6.2 12K 43 44 45 22 11 7.7 13 18 173 36 20 18 13 56 67 104 53 5.4 241 19 13 3.8 38 14 31 329 16 155... (8 Replies)
Discussion started by: sam05121988
8 Replies

4. Homework & Coursework Questions

Arithmetic Problem with shell script programming.

Hello everybody, I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input. I... (8 Replies)
Discussion started by: Florinel76
8 Replies

5. Shell Programming and Scripting

Arithmetic Problem with shell script programming.

Hello everybody, I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input. I... (1 Reply)
Discussion started by: Florinel76
1 Replies

6. Post Here to Contact Site Administrators and Moderators

Broken link FAQ date arithmetic with shell

page unix com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html Date Arithmetic with the Shell has link of www samag com/documents/s=8284/sam0307b/0307b.htm which is no longer. Is this the correct place to post this?:confused: and I got message... (1 Reply)
Discussion started by: dgerman
1 Replies

7. Shell Programming and Scripting

Arithmetic calculation on real numbers in Bourne Shell Script

I am begining to learn bourne shell and as a practice I have written a script which when given the purchase price and percentage of discount calculates the savings. I somehow cannot figure out why my script fails to do arthimatic calculation on real numbers. Could anyone look at the script... (5 Replies)
Discussion started by: Tirmazi
5 Replies

8. Fedora

Simple arithmetic in shell

Hey, I just wanted to know how one can write simple arithmetic like addition, subtraction, multiplication and division in shell-script. (14 Replies)
Discussion started by: #moveon
14 Replies

9. UNIX Desktop Questions & Answers

shell script ` ` need help if in the right place

> #!/bin/bash > > > echo "please enter your login ID" > > read login_id > > > whileairmax_sk@yahoo.com (2 Replies)
Discussion started by: kim187
2 Replies

10. Shell Programming and Scripting

What is a good place to learn basic shell scripting? Thanks!

See the title. Originally wasn't going to type anything but I need a message of at least 10 characters! :cool: (2 Replies)
Discussion started by: deutchap6verse5
2 Replies
Login or Register to Ask a Question