Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

I think you want the following:

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

You could also use plain string manipulation:

$ start=$(date '+%s%N')
$ echo $start
1394663274979099354
$ echo ${start:0:-6}
1394663274979

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

printf "%0.3f\n" $(date +%s.%N)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top