2015-09-07 49 views
-3
[email protected]:/home/wideDB1$ echo $(($(date +%s%N)/1000000)) 
1441637184683 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/1000000)) 
730 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/1000)) 
bash: 088253206: value too great for base (error token is "088253206") 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/1000)) 
bash: 085061725: value too great for base (error token is "085061725") 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/10000)) 
59078 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/1000)) 
672523 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/10)) 
12079796 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/10000)) 
74644 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/100000)) 
1403 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/1000000)) 
5 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/100000)) 
9977 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/1000000)) 
360 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/100000)) 
6663 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/10)) 
10325856 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/100)) 
8020128 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/1000)) 
9461 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/10000)) 
81272 
[email protected]:/home/wideDB1$ echo $(($(date +%N)/10000)) 
bash: 046340492: value too great for base (error token is "046340492") 

我必須在bash腳本中準確地使用此功能來同步任務之間的毫秒數,但這裏非常不穩定。任何人都可以幫助我弄清楚這項工作的最佳解決方案?定期「價值太大基地」做日期輸出在數學上的數學

乾杯! 亞歷山德羅$((...))內部

+2

你得到的價值太大了'錯誤,因爲前面的零指示外殼的八進制數做算術時。你可以通過sed -E'/^0 //'來刪除它。 – user1666959

+0

另請參閱http://wooledge.org/~greybot/meta/august –

+0

@ user1666959,boo hiss re:建議調用一個外部工具來完成shell可以執行的操作。 –

回答

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 
2

默認情況下,以0開頭的數字串被視爲八進制數;您只需告知bash即可在所有情況下將從date +%N中捕獲的字符串作爲十進制數字,前綴爲10#

$ echo $((10#$(date +%N)/1000000)) 
+0

非常感謝你,它看起來很好!乾杯! – Alessandro