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.
Is it possible to do arithmetic in shell scripts? If so, just how is it done?
Here is the code so far:
Code:
#!/bin/bash
time=$(date +%H%M)
for time in $time+1; do
echo $time
done
What I am trying to do here really is create a shell script that automatically prints the time every time it changes.
I am wondering whether to use a condition or a loop here. I am settling on a condition, but then there's the issue of a lack of a '!=' or equivalent operator.
Yes, but I want this script to execute indefinitely, as though it were a clock, printing the time in the %H%M format every minute. Any idea how to do that?
Yes, but I want this script to execute indefinitely, as though it were a clock, printing the time in the %H%M format every minute. Any idea how to do that?
Then you don't need to do any arithmetic; you just need an infinite delayed loop
Code:
#!/bin/bash
last_time=$(date +%H%M)
while true
do
sleep 1
this_time=$(date +%H%M)
if [[ $this_time != $last_time ]]; then
echo "$this time"
last_time=$this_time
fi
done
You're posts indicate that you want two very different things:
Quote:
What I am trying to do here really is create a shell script that automatically prints the time every time it changes.
Quote:
I want this script to execute indefinitely, as though it were a clock, printing the time in the %H%M format every minute
The first quote implies that you want the script to wait until the system clock changes before printing. In other words, you want to "poll" the system clock.
The second quote implies that you want to recreate the system clock.
I'll give an example of each (though, it won't be a "true" polling script for the first quote):
For quote #1:
Code:
#!/bin/bash
old_time="junk"
while true ; do
current_time=$( date +%H%M )
if [ "${current_time}" != "${old_time}" ] ; then
echo ${current_time}
old_time="${current_time}"
fi
sleep 1
done
For quote #2:
Code:
#!/bin/bash
while true ; do
date +%H%M
sleep 60
done
As I was writing this, catkin posted his response which, funny enough, is almost exactly the same for quote #1.
I should point out that the version for quote #2 may eventually "get off." By that I mean that you're not guaranteed that your loop will execute precisely at 60 seconds. You're likely to get output that seemingly "skips" a minute as the fractional delays add up.
While the end result may look very similar, the requests imply two different methods of accomplishing the result.
I look at it from a triggering perspective.
In the first quote, the printing is triggered by the system time changing. In catkin's script and the first one I gave, we both reference the system clock indirectly--through the date command. Once the output of the date command changes, the time is printed. We're polling the system clock.
In the second quote, the printing is triggered by an internal mechanism in the script--where the internal mechanism mimics a clock. In my second script, the date command is not used to check the time, but only to format it for proper display. The internal mechanism is the sleep command. The script has recreated a clock.
As a final example, let me change up the OPs quoted statements a little. Let's assume that, instead of a clock, the OP wanted an email notification.
Quote:
What I am trying to do here really is create a shell script that automatically notifies me when I receive an email.
versus
Quote:
I want this script to execute indefinitely, as though it were an email server, and notify me when I receive an email every minute
The first quote implies integration with the email server, to give a notification precisely when the email arrives. The second quote can avoid the integration by simply checking the contents of /var/mail, ~/Mail, ~/mbox, etc. once per minute.
Anyway, that's how I look at it.
Last edited by Dark_Helmet; 11-20-2010 at 12:54 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.