0

I am running this command in my shell script to get time in milliseconds:

    START=`$(date +%s%N)/1000000`

However I keep getting the error:

   1394661620440271000/1000000: No such file or directory

I tried to change the code by adding brackets, extra dollar signs, but I keep getting different kinds of errors. How can I fix the code? Could anyone help with that?

user3344382
  • 1,941
  • 4
  • 18
  • 17
  • change the backticks to double quotes. You'll also need to use some kind of evaluator to do the division. – Judge Mental Mar 12 '14 at 22:12
  • Backticks mean "execute the stuff inside as a command". `1394661620440271000/1000000` doesn't mean "do division", it means "execute the `1394661620440271000/1000000` file". – Oliver Charlesworth Mar 12 '14 at 22:12
  • fastest route to understanding errors like this is to turn on shell debug/trace feature with `set -vx` and turn it back off with `set +vx`. Wrap your problem line with those, and you'll at least be asking a new level of question. try `ST=$(( $(date +%s%N) / 1000000))` . Good luck. – shellter Mar 12 '14 at 22:15

4 Answers4

2

assuming that you're using bash:

START=$(( $(date '+%s%N') / 1000000 ))

you can't just say / on the command line to divide numbers. (( ... )) does arithmetic evaluation in bash.

user3392484
  • 1,929
  • 9
  • 9
2

I think you want the following:

START=$(($(date +%s%N)/1000000))
PlasmaPower
  • 1,864
  • 15
  • 18
1

You could also use plain string manipulation:

$ start=$(date '+%s%N')
$ echo $start
1394663274979099354
$ echo ${start:0:-6}
1394663274979
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

The printf statement can round a value to the nearest millisecond:

printf "%0.3f\n" $(date +%s.%N)
chepner
  • 497,756
  • 71
  • 530
  • 681