这怎么行从一个bash脚本工作? [英] How does this line from a Bash Script work?
问题描述
我试图找出如何从<一顶code的某一行href=\"https://github.com/scop/bash-completion/blob/52315ef2ceb3f8e6b7fe45a09f8df24b73394da4/completions/man#L3\"相对=nofollow> BASH手册页标签完成脚本作品:
[$ OSTYPE == * @(达尔文| FreeBSD的|在solaris | cygwin的| OpenBSD系统)*]] || _userland GNU \\
||返回1
我相信这是一个后卫;如果BASH特殊变量 $ OSTYPE
不包含在字符串中的一个(基本正常吗?)恩pression包含在括号内,或者如果userland都是GNU然后它会停止脚本的执行。但是,我无法理解语法的工作原理或这意味着什么,我不知道控制的流程是什么。
您可以找到 _userland
<一个定义href=\"https://github.com/scop/bash-completion/blob/52315ef2ceb3f8e6b7fe45a09f8df24b73394da4/bash_completion#L97\"相对=nofollow>这里:
#检查,如果我们给定用户空间运行
#@参数$ 1的userland检查
_userland()
{
当地的userland = $(的uname -s)
[[$用户态== @(Linux的|的GNU / *)]] - 放大器;&放大器;用户空间= GNU
[[$用户态== $ 1]]
}
请问这个功能工作?它返回一个值?
如果你能提供相关文档或文章的参考文献,这将是有益的。谢谢你。
在的C1 ||链C2 || C3 || ...
每个命令将尝试直到一个成功。一个成功的命令后,在链中的剩余命令将不被执行。
因此,在这条产业链的命令:
[$ OSTYPE == * @(达尔文| FreeBSD的|在solaris | cygwin的| OpenBSD系统)*]] \\
|| _userland GNU \\
||返回1
如果 OSTYPE
是达尔文,FreeBSD的,等等,然后链条中的一站。你可以阅读更多关于男人庆典
使用有图案,搜索模式匹配即可。在这个例子中,适用的规则是:
@(模式列表)
匹配给定模式中的一个
块引用>该模式是由
分离|
。在*
围绕@(...)
意味着这些模式可以在发生在任何地方OSTYPE
。只有@(...)
将意味着在给定的图案精确匹配,@(...)*
会意味着开始与任何给定的模式,* @(...)
将意味着在任何给定的模式结束。如果
OSTYPE
不匹配,我们尝试在链中的下一个项目:_userland GNU
。如果成功的话,则链停止。否则,我们尝试在链中的下一个项目,这是收益1
,所以我们退出功能。的
_userland
函数所做的:
本地用户态= $(的uname -s)
:的uname -s
命令的输出存储在本地变量名为的userland
[$用户态== @(Linux的| GNU / *)]]&功放;&安培;用户级= GNU
:如果该值的Linux
或启动GNU /
,然后设置的userland = GNU
[$用户态== $ 1]]
:比较值,我们收到的参数。这种比较的出口code是该函数的返回值。如果值匹配,这意味着成功。I'm trying to figure out how a certain line of code from the top of BASH man page tab completion script works:
[[ $OSTYPE == *@(darwin|freebsd|solaris|cygwin|openbsd)* ]] || _userland GNU \ || return 1
I believe it's a guard; if the BASH special variable
$OSTYPE
does not contain one of the strings in the (basic regular?) expression contained within parentheses, or if the userland is GNU then it'll discontinue execution of the script. But, I can't understand how the syntax works or what it means and I don't know what the flow of control is.You can find the definition of
_userland
here:# Check if we're running on the given userland # @param $1 userland to check for _userland() { local userland=$( uname -s ) [[ $userland == @(Linux|GNU/*) ]] && userland=GNU [[ $userland == $1 ]] }
How does this function work? Does it return a value?
If you could provide references to relevant documentation or articles, that would be helpful. Thank you.
解决方案In a chain of
c1 || c2 || c3 || ...
each command will be tried until one is successful. After a successful command, the remaining commands in the chain will not be executed.So in this chain of commands:
[[ $OSTYPE == *@(darwin|freebsd|solaris|cygwin|openbsd)* ]] \ || _userland GNU \ || return 1
If
OSTYPE
is one of darwin, freebsd, etc, then the chain stops. You can read more about the pattern used there inman bash
, search for Pattern Matching. In this example, the rule that applies is:@(pattern-list) Matches one of the given patterns
The patterns are separated by
|
. The*
around the@(...)
means that these patterns can occur anywhere withinOSTYPE
. Only@(...)
would mean exact match on the given patterns,@(...)*
would mean starting with any of the given patterns,*@(...)
would mean ending with the any of the given patterns.If
OSTYPE
didn't match, we try the next item in the chain:_userland GNU
. If successful, then the chain stops. Otherwise we try the next item in the chain, which isreturn 1
, so we exit the function.The
_userland
function does:
local userland=$( uname -s )
: store the output ofuname -s
command in a local variable calleduserland
[[ $userland == @(Linux|GNU/*) ]] && userland=GNU
: if the value isLinux
or starts withGNU/
, then setuserland=GNU
[[ $userland == $1 ]]
: compare the value to the parameter we received. The exit code of this comparison is the return value of the function. If the value matches, that means success.这篇关于这怎么行从一个bash脚本工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!