fish shell -eq 和 -a 在 if 语句中
fish shell -eq and -a in if statement
我正在阅读鱼 shell 的 git.fish 完成脚本 (/usr/local/Cellar/fish/2.1.2/share/fish/completions) 并且我 运行 遇到了一些理解上的问题语法是什么意思。
块中,
function __fish_git_needs_command
set cmd (commandline -opc)
if [ (count $cmd) -eq 1 -a $cmd[1] = 'git' ]
return 0
end
return 1
end
我了解到 cmd
设置为 commandline -opc
。但是在下一个语句 (count $cmd) -eq 1 -a $cmd[1] = 'git'
中, -eq
和 -a
是什么意思?
我是 fish 的新手 shell,我试图通过尝试为程序编写自己的完成脚本来理解语法。将不胜感激。
谢谢。
-eq
是一个 integer comparison function。
-a
是一个logical and。
所以逻辑等价物是这样的:
if [ (count $cmd) == 1 && $cmd[1] == 'git' ]
(在 Java 伪语法中)。
背景
之所以使用 -eq
是因为 shell 通常仅适用于文本处理。结果数字存储在 "strings" 中。有时两个数字是等价的,但不是字符串等价的。比如下面这个例子:
if [ "01" -eq "1" ]
then
echo "integer equal"
fi
if [ "01" = "1" ]
then
echo "string equal"
fi
只会打印 integer equal
.
- NUM1 -eq NUM2 returns true if NUM1 and NUM2 are numerically equal.
- COND1 -a COND2 returns true if both COND1 and COND2 are true.
它测试 (count $cmd) = 1
和 $cmd[1] = 'git'
。
(=
这里是相等,不是赋值)。
事实上 -eq
和 -a
不是 fish 语法的一部分。都是普通的参数!
if [ (count $cmd) -eq 1 -a $cmd[1] = 'git' ]
这里的左方括号实际上是一个命令,比如cat或者grep。您确实有一个文件 /bin/[
。通过test
命令可能更容易理解,其实是一样的:
if test (count $cmd) -eq 1 -a $cmd[1] = 'git'
现在很容易看出 -eq
和 -a
只是传递给 test
的普通参数,对 fish 没有语法意义。
test
有自己的小语言,如 awk 或 sed。请参阅 man test
了解它。
我正在阅读鱼 shell 的 git.fish 完成脚本 (/usr/local/Cellar/fish/2.1.2/share/fish/completions) 并且我 运行 遇到了一些理解上的问题语法是什么意思。
块中,
function __fish_git_needs_command
set cmd (commandline -opc)
if [ (count $cmd) -eq 1 -a $cmd[1] = 'git' ]
return 0
end
return 1
end
我了解到 cmd
设置为 commandline -opc
。但是在下一个语句 (count $cmd) -eq 1 -a $cmd[1] = 'git'
中, -eq
和 -a
是什么意思?
我是 fish 的新手 shell,我试图通过尝试为程序编写自己的完成脚本来理解语法。将不胜感激。
谢谢。
-eq
是一个 integer comparison function。
-a
是一个logical and。
所以逻辑等价物是这样的:
if [ (count $cmd) == 1 && $cmd[1] == 'git' ]
(在 Java 伪语法中)。
背景
之所以使用 -eq
是因为 shell 通常仅适用于文本处理。结果数字存储在 "strings" 中。有时两个数字是等价的,但不是字符串等价的。比如下面这个例子:
if [ "01" -eq "1" ]
then
echo "integer equal"
fi
if [ "01" = "1" ]
then
echo "string equal"
fi
只会打印 integer equal
.
- NUM1 -eq NUM2 returns true if NUM1 and NUM2 are numerically equal.
- COND1 -a COND2 returns true if both COND1 and COND2 are true.
它测试 (count $cmd) = 1
和 $cmd[1] = 'git'
。
(=
这里是相等,不是赋值)。
事实上 -eq
和 -a
不是 fish 语法的一部分。都是普通的参数!
if [ (count $cmd) -eq 1 -a $cmd[1] = 'git' ]
这里的左方括号实际上是一个命令,比如cat或者grep。您确实有一个文件 /bin/[
。通过test
命令可能更容易理解,其实是一样的:
if test (count $cmd) -eq 1 -a $cmd[1] = 'git'
现在很容易看出 -eq
和 -a
只是传递给 test
的普通参数,对 fish 没有语法意义。
test
有自己的小语言,如 awk 或 sed。请参阅 man test
了解它。