ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
that's not exactly a count time program... that's more a convert time program
first: echo -n "sentence" to display the sentence and don't go to the next line
second: read value ... it displays a prompt (cursor) and wait for a value... the entered value is stored in value
Then that's maths
so it counts how many hours there's in value thanks to a division (3600 sec = 1 hr)... the result is stored in hour variable... it takes the rest of the division. Then it does the same for minutes...
this divides your number of seconds by 3600 (discarding the remainder) to give you a number of hours.
seconds=$((seconds % 3600))
x % y means get the remainder of x / y, so this gets the "left over" number of seconds after the full hours have been removed. Note that this value then replaces the original value of the variable 'seconds'.
the next two lines do the same thing to the remaining seconds, only to calculate minutes instead.
The three variables are then printed out.
Yes, that's what a calculator will give you - but the shell uses integer division, and discards the .389. You can see this for yourself by typing:
Code:
echo $((5001/3600))
the output will just be 1. So the code sets $hours to 1, and moves on to replace $seconds with the remainder of 1401.
It then does the same thing with 1401 - divides by 60 to get $minutes, and the remainder replaces $seconds.
the right hand side (x+1) will be calculated, and then the value will be put into x. So when the program sees:
seconds = $((seconds % 60))
it does the calculation (current value of $seconds (1401) modulus 60, and then puts the answer (21) in $seconds. At any point after this line, $seconds is 21 - the value 1401 is 'lost' in the program.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.