LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-19-2010, 09:27 PM   #1
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791
Blog Entries: 62

Rep: Reputation: 56
Arithmetic in shell scripts


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.
 
Old 11-19-2010, 10:24 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by Kenny_Strawn View Post
then there's the issue of a lack of a '!=' or equivalent operator.
http://tldp.org/HOWTO/Bash-Prog-Intr...11.html#ss11.3
 
Old 11-19-2010, 10:37 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
For date arithmetic the easiest solution is to use the date command, for example:
Code:
c@CW8:~$ some_time=$(date)
c@CW8:~$ date -d "$some_time + 1 hour"
Sat Nov 20 11:05:19 IST 2010

Last edited by catkin; 11-19-2010 at 10:41 PM. Reason: Removed useless echo
 
Old 11-19-2010, 11:02 PM   #4
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791

Original Poster
Blog Entries: 62

Rep: Reputation: 56
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?
 
Old 11-19-2010, 11:21 PM   #5
propofol
Member
 
Registered: Nov 2007
Location: Seattle
Distribution: Debian Wheezy & Jessie; Ubuntu
Posts: 334

Rep: Reputation: 60
How about:
Code:
some_time=$(date)
c@CW8:~$some_time=$(date -d "$some_time + 1 hour")
Your script:
Code:
for time in $time+1; ...
does not make sense. Look at this.

Use $(( ... )) notation for shell arithmetic:
Code:
t=2
t=$(( t + 5 ))
Regards,
Stefan
 
Old 11-19-2010, 11:59 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Kenny_Strawn View Post
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
 
Old 11-20-2010, 12:10 AM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 11-20-2010, 08:53 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Dark_Helmet View Post
You're posts indicate that you want two very different things
I think they don't.

He wants to make a clock that prints the time every time the minute changes.
 
Old 11-20-2010, 12:53 PM   #9
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
That's ok, we can have a difference of opinion

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.
 
Old 11-20-2010, 06:12 PM   #10
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
As to arithmetic in shell scripts, integer arithmetic is possible using expr.

a=10
a=`expr $a + 10`
echo $a
 
Old 01-02-2011, 01:40 AM   #11
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Or w/ $((...)):
Code:
echo $((1+1))
Only works w/ integers, e.g.:
Code:
$ echo $((14/3))
4
There also is bc (bc arbitrary precision calculator language), which must be installed.
 
Old 01-02-2011, 07:03 AM   #12
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
Code:
$ echo $((14/3))
4
If you have bc available (most linux distros have it):
Code:
$ echo 'scale=2; 14/3' | bc
4.66
$ echo 'scale=4; 14/3' | bc
4.6666
 
Old 01-02-2011, 09:44 AM   #13
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by archtoad6 View Post
There also is bc (bc arbitrary precision calculator language), which must be installed.
Real programmers use dc. ;)
 
Old 01-02-2011, 01:09 PM   #14
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Thanks.

I had an (original) HP-35, so dc looks like a briar patch to me.

Edit: briar patch is a link, follow it.

Last edited by archtoad6; 01-03-2011 at 09:58 PM.
 
Old 01-02-2011, 09:18 PM   #15
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by archtoad6 View Post
I had an (original) HP-35, so dc looks like a briar patch to me.
Um, dc should appeal to you if you had an HP-35. Are you saying you don't like RPN? I'm confused.

(I have an HP-16c, and it's still my calculator of choice. I hope to have it entirely emulated by the time it stops working, which I hope is never.)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash shell script arithmetic mindcontrolusa Programming 6 11-10-2010 01:02 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
Shell scripts consty Programming 2 02-18-2007 09:41 PM
Do shell scripts permit arithmetic operations? johnpaulodonnell Linux - Newbie 6 01-30-2007 10:24 AM
Shell scripts...then some. Freestone Programming 3 04-23-2006 12:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration