ubu@bruntu:/home/wideDB1$ echo $(($(date +%s%N)/1000000))
1441637184683
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/1000000))
730
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/1000))
bash: 088253206: value too great for base (error token is "088253206")
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/1000))
bash: 085061725: value too great for base (error token is "085061725")
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/10000))
59078
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/1000))
672523
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/10))
12079796
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/10000))
74644
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/100000))
1403
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/1000000))
5
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/100000))
9977
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/1000000))
360
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/100000))
6663
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/10))
10325856
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/100))
8020128
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/1000))
9461
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/10000))
81272
ubu@bruntu:/home/wideDB1$ echo $(($(date +%N)/10000))
bash: 046340492: value too great for base (error token is "046340492")
我必须准确地在bash脚本中使用此函数来同步任务之间的毫秒数,但这里非常不稳定。任何人都可以帮我找出这项工作的最佳解决方案吗?
干杯! 的Alessandro
答案 0 :(得分:2)
默认情况下,以0开头的数字字符串被视为八进制数字;您只需告诉bash
在所有情况下将date +%N
中捕获的字符串视为十进制数字,前缀为10#
。
$ echo $(( 10#$(date +%N)/1000000))
答案 1 :(得分:1)
$((...))
内的数字字符串被解释为数字,但以0
开头的数字被解释为八进制数字,即基数为8。
您可以通过参数扩展轻松删除前导零:
#!/bin/bash
shopt -s extglob # Enable extended globbing -- in this case, +(0)
for i in {1..20} ; do
nanos=$(date +%N)
echo -n "$nanos -> "
nanos=${nanos##+(0)} # Remove as many 0's from the left as possible.
echo $nanos
done