JDS
JDS

Reputation: 16998

C shell doing arithmetic operations with large numbers

First of all: sorry for using c shell, blame my company not me. I hate the damn thing as much as most of you do now (at first I was like, hey this ain't so bad).

I am trying to subtract large numbers obtained from time stamps. Here is what I am trying:

set curTime = `date +%s%N`
#... some stuff
@curTime = `date +%s%N` - $curTime #get the diff
echo "time taken: $curTime"

However I guess the numbers are too big - before I tried with just seconds and it worked fine. Here's what I see in the log:

@curMilli = 1349996279792995000 - 1349996279170458000
@curMilli: Command not found.

As I said I do the exact same thing with date +%s and it's fine, so I'm assuming it's something about the largeness of the numbers.

How can I do this? Thanks a lot.

Upvotes: 1

Views: 1167

Answers (1)

Joseph Quinsey
Joseph Quinsey

Reputation: 9972

The article http://en.wikipedia.org/wiki/Bc_programming_language has a short section "Using bc in shell scripts". A test:

set curTime = `/bin/date +%s%N`
/bin/sleep 2
set prevTime = $curTime
set curTime = `/bin/date +%s%N`
set diff = `echo "$curTime - $prevTime;" | /usr/bin/bc`
echo $diff

will give (with the digits after the initial 20 variable):

2016204108

P.s: I wish I could vote you up twice for "I hate the damn thing as much as most of you do now (at first I was like, hey this ain't so bad)."

Upvotes: 1

Related Questions