2011-12-14 159 views
2

我新的外殼。我不太瞭解以下功能。這個功能基本上是由1殼牌#內部(())

增加小時,我很奇怪,爲什麼開發商把「10#」在$ g_current_hour + 1前面。根據我的理解,shell中的劑量意味着評論?

f_increment_hour() { 
    g_next_hour=$((10#$g_current_hour+1)) 
} 

回答

3

一切都取決於上下文。 10#表示基數爲10.

具有前導0的常量被解釋爲八進制數。甲 0x或0X表示十六進制。否則,數字採取 形式[鹼#] n,其中任選的鹼是 2和64之間的十進制數表示的算術基,並且n是在 基數的數字。如果鹼基#被省略,則使用鹼基10。

+2

+1:請注意,Open Group sh語言規範規定:「只有ISO C標準第6.4.4.1節中規定的十進制常數,八進制常數和十六進制常數常量需要被識別爲常量「。我相信N#符號是一種bashism。 –

+0

是的,我相信是的。 –

0

'#'將被解釋爲標記的一部分,除非它前面有空格,換行符或分號。 (或任何其他非字符號)

科語言規範的2.3「令牌識別」,規定:

 
7. If the current character is an unquoted <newline>, the current 
    token shall be delimited. 
8. If the current character is an unquoted <blank>, any token 
    containing the previous character is delimited and the current 
    character shall be discarded. 
9. If the previous character was part of a word, the current character 
    shall be appended to that word. 
10. If the current character is a '#' , it and all subsequent characters 
    up to, but excluding, the next <newline> shall be discarded as 
    a comment. The <newline> that ends the line is not considered 
    part of the comment. 

當殼體被解析其輸入並讀爲「foo#欄」,如它正在處理它應用規則9的'#'字符並將#追加到令牌。一旦應用規則9,它將停止檢查,並且從不考慮規則10。如果「#」之前的字符是空白,那麼第9條不適用,那麼第10條檢查和註釋的開始。

換句話說,如果前面的字符不是單詞的一部分(例如空格或分號),那麼'#'只會開始註釋,所以「foo#bar」是一個標記,而不是「foo 「這是一個註釋,但‘富#bar’是令牌‘富’是一個註釋。