简体   繁体   English

此bash代码如何检测交互式会话?

[英]How does this bash code detect an interactive session?

Following some issues with scp (it did not like the presence of the bash bind command in my .bashrc file, apparently), I followed the advice of a clever guy on the Internet (I just cannot find that post right now) that put at the top of its .bashrc file this: 继scp的一些问题(显然不喜欢我的.bashrc文件中存在bash bind命令)之后,我遵循了互联网上一个聪明人的建议(我现在暂时找不到该帖子),放在其.bashrc文件的顶部如下:

[[ ${-#*i} != ${-} ]] || return

in order to make sure that the bash initialization is NOT executed unless in interactive session. 为了确保除非在交互式会话中,否则不执行bash初始化。

Now, that works. 现在,这可行。 However, I am not able to figure how it works. 但是,我无法弄清楚它是如何工作的。 Could you enlighten me? 你能启发我吗?

According to this answer , the $- is the current options set for the shell and I know that the ${} is the so-called "substring" syntax for expanding variables. 根据这个答案$-是当前为shell设置的选项,我知道${}是扩展变量的所谓“子字符串”语法。

However, I do not understand the ${-#*i} part. 但是,我不理解${-#*i}部分。 And why $-#*i is not the same as ${-#*i} . 以及为什么$-#*i${-#*i}

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

The word is expanded to produce a pattern just as in filename expansion. 单词被扩展以产生一个模式,就像文件名扩展一样。 If the pattern matches the beginning of the expanded 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. 如果parameter是下标为'@'或' ' 的数组变量 ,则将模式删除操作依次应用于数组的每个成员,并且扩展为结果列表。

Source: http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html 资料来源: http : //www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

So basically what happens in ${-#*i} is that *i is expanded, and if it matches the beginning of the value of $- , then the result of the whole expansion is $- with the shortest matching pattern between *i and $- deleted. 因此,基本上在${-#*i}发生的事情是*i被扩展了,如果它与$-的值的开头匹配,则整个扩展的结果就是$- ,并且*i之间的匹配模式最短和$-已删除。

Example

VAR="baioasd";
echo ${VAR#*i};

outputs oasd . 输出oasd

In your case 就你而言

If shell is interactive, $- will contain the letter 'i', so when you strip the variable $- of the pattern *i you will get a string that is different from the original $- ( [[ ${-#*i} != ${-} ]] yelds true). 如果shell是交互式的,则$-将包含字母'i',因此,当剥离模式*i的变量$- ,将得到与原始$-不同的字符串( [[ ${-#*i} != ${-} ]]产生true)。 If shell is not interactive, $- does not contain the letter 'i' so the pattern *i does not match anything in $- and [[ ${-#*i} != $- ]] yelds false, and the return statement is executed. 如果shell不是交互式的,则$-不包含字母'i',因此模式*i$-中的任何内容都不匹配,并且[[ ${-#*i} != $- ]]产生false,然后return语句被执行。

See this : 看到这个

To determine within a startup script whether or not Bash is running interactively, test the value of the '-' special parameter. 要在启动脚本中确定Bash是否正在交互式运行,请测试'-'特殊参数的值。 It contains i when the shell is interactive 当外壳是交互式时,它包含i

Your substitution removes the string up to, and including the i and tests if the substituted version is equal to the original string. 您的替换将删除字符串(包括i在内),并测试替换版本是否等于原始字符串。 They will be different if there is i in the ${-} . 如果${-}i ,则它们将不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM