里面的外壳# (( ))

发布于 2024-12-21 07:18:55 字数 201 浏览 0 评论 0原文

我是外壳新手。我不太明白以下功能。这个函数基本上将小时增加 1。

我想知道为什么开发人员在 $g_current_hour+1 前面放“10#”。据我了解,shell中的dose#表示注释?

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

I am new to shell. I am not quite understand the following function. This function basically increase the hour by 1.

I am wondering why the developer put "10#" in front of $g_current_hour+1. From my understanding, dose # in shell means comments?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑叹一世浮沉 2024-12-28 07:18:55

一切都取决于上下文。这里 10# 表示以 10 为基数。

以 0 开头的常量被解释为八进制数。一个
前导 0x 或 0X 表示十六进制。否则,数字取
形式 [base#]n,其中可选基数是介于
2和64代表算术基数,n是其中的数字
根据。如果省略 base#,则使用基数 10。

Everything depends on the context. Here 10# means base 10.

Constants with a leading 0 are interpreted as octal numbers. A
leading 0x or 0X denotes hexadecimal. Otherwise, numbers take the
form [base#]n, where the optional base is a decimal number between
2 and 64 representing the arithmetic base, and n is a number in that
base. If base# is omitted, then base 10 is used.

海未深 2024-12-28 07:18:55

'#' 将被解释为标记的一部分,除非它前面有空格、换行符或分号。
(或任何其他非单词符号)

语言规范的第 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.

当 shell 解析其输入并读取“foo#bar”时,因为它正在处理“#”字符,所以它应用规则 9并将 # 附加到标记中。一旦应用了规则 9,它就会停止检查并且永远不会考虑规则 10。如果“#”之前的字符是空格,则规则 9 不适用,因此检查规则 10 并开始注释。

换句话说,只有当“#”前面的字符不是单词的一部分(例如空格或分号)时,“#”才会开始注释,因此“foo#bar”是一个标记,而不是“foo”后跟一个注释,但“foo #bar”是标记“foo”后跟注释。

'#' will be interpreted as part of a token unless it is preceded by a space, newline, or semi-colon.
(or any other non-word symbol)

Section 2.3 "Token recognition" of the language spec, states:

 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.

When the shell is parsing its input and reads "foo#bar", as it is processing the '#' character it applies rule 9 and appends the # to the token. Once rule 9 is applied, it stops checking and rule 10 is never considered. If the character preceding the '#' is whitespace, then rule 9 does not apply, so rule 10 is checked and a comment is started.

In other words, a '#' only starts a comment if the character preceded it is not part of a word ( eg whitespace or semi-colon), so "foo#bar" is one token, and not "foo" followed by a comment, but "foo #bar" is the token "foo" followed by a comment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文