gpt4 book ai didi

linux -/etc/profile 中的 Bash 语法

转载 作者:IT王子 更新时间:2023-10-29 00:49:29 25 4
gpt4 key购买 nike

我刚刚在我的 /etc/profile 中注意到这一行,我想知道这 if 意味着什么以及它何时为真。

if [ "${-#*i}" != "$-" ]; then

i 迭代多个 *.sh 文件。

很抱歉,如果这是一个愚蠢的问题,但正如您可以想象的那样,在 Google 中主要寻找符号确实不是一种选择。

谢谢!

最佳答案

来自联机帮助页的 OPTIONS 部分:

-i        If the -i option is present, the shell is interactive.

来自联机帮助页的特殊参数部分:

  -      Expands  to  the  current option flags as specified upon invoca-
tion, by the set builtin command, or those set by the shell
itself (such as the -i option).

来自联机帮助页的参数扩展部分:

  ${parameter#word}
${parameter##word}

The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘‘#’’ case) or the longest matching pattern (the ‘‘##’’ case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

所以 "${-#*i}" 表示扩展 $- 变量并从字符串的开头删除直到字母 i。然后将该扩展与 !=$- 的扩展进行比较(相同的变量只是未修改)。

当它们不相同时,这意味着第一次扩展删除了一些内容,这意味着字母 i 出现在 $- 的值中,这意味着(因为 >-i 不是 set 的参数)-i 参数被传递给了 shell 并且 shell 是一个交互式 shell。

关于linux -/etc/profile 中的 Bash 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27024890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com