0

I'm trying to display how long it takes for my script to execute in both seconds and milliseconds.

This is my code:

#!/bin/bash before=10#`date +'%s%N'` sleep 1 after=10#`date +'%s%N'` echo "$before" let "time_taken=$before-$after" echo "$time_taken" echo "$s" 

But I get the following error:

./tijdsmeting.sh: line 9: let: time_taken=10#1590683503N: value too great for base (error token is "10#15906835 ~ |03N") 

I researched it and it has something to do with decimal/octal/... But I did specify it needed to be decimal by using #10. Maybe using ibase? And my errortoken isn't 08 like in the other cases on the internet..

5
  • 4
    Perhaps the issue is that your system's date implementation doesn't support the %N format specifier, and is rendering it as literal N? Commented May 28, 2020 at 16:40
  • Ohwww that could be possible.. I'm using a Mac. Commented May 28, 2020 at 16:43
  • 1
    Indeed, you can see the N in the error message there. In base 24, N would be a valid digit... As an aside, I don't think you should need 10# here, the output of date +%s should not have leading zeroes (unlike months, days, hours and minutes often have) Commented May 28, 2020 at 17:50
  • If you're just interested in the duration in seconds, investigate bash's $SECONDS variable. Commented May 28, 2020 at 18:43
  • Run it under the bash built-in time command: gives you real 0m3.740s. You may need some inventive subshelling to capture it. Commented May 29, 2020 at 0:20

1 Answer 1

1

%N is an extension of the GNU implementation of date (added in 4.5.1 in 2002).

It is now also supported by

  • ast-open's date since version 2005-01-11
  • busybox date since 1.17.0 (2010), though it's optional there and generally not enabled by default
  • toybox date since 0.7.4 (2017).
  • FreeBSD date since 14.1 (2024)

But generally not other implementations.

Here, it seems your date doesn't support %N and upon date +%N outputs N (a behaviour I can reproduce with FreeBSD before 14.1, and probably what you'd get in macos as well).

So that:

let "time_taken=$before-$after" 

(btw ITYM after-before) becomes something like:

let 'time_taken=10#1590683503N-10#1590683504N' 

And bash complains about that N which can't be used in base 10.

In any case, running date itself will take thousands of nanoseconds, so having that much precision probably don't make much sense.

Here, you could switch to zsh where, like in ksh93, the $SECONDS special variable can be made a floating point number and simply do:

#! /bin/zsh - typeset -F SECONDS=0 sleep 1 time_taken=$SECONDS 

Or use the $epochtime array variable of the zsh/datetime module which contains seconds and nanoseconds as 2 separate integers:

#! /bin/zsh - zmodload zsh/datetime || exit before=($epochtime) sleep 1 after=($epochtime) (( time_taken_ns = 1_000_000_000 * (after[1] - before[1]) + after[2] - before[2] )) 
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.