简体   繁体   English

if语句中的fish shell -eq和-a

[英]fish shell -eq and -a in if statement

I was reading the git.fish completion script (/usr/local/Cellar/fish/2.1.2/share/fish/completions) for fish shell and I ran into some problems with understanding what the syntax means. 我正在阅读鱼壳的git.fish完成脚本(/usr/local/Cellar/fish/2.1.2/share/fish/completions),我在理解语法的含义时遇到了一些问题。

In the block, 在街区,

function __fish_git_needs_command
  set cmd (commandline -opc)
  if [ (count $cmd) -eq 1 -a $cmd[1] = 'git' ]
    return 0
  end
  return 1
end

I understand that cmd is set as commandline -opc . 我知道cmd被设置为commandline -opc But in the next statement (count $cmd) -eq 1 -a $cmd[1] = 'git' , what do -eq and -a mean? 但是在下一个语句中(count $cmd) -eq 1 -a $cmd[1] = 'git' ,- -eq-a是什么意思?

I am new to fish shell and I am trying to understand the syntax by trying to write my own completion script for a program. 我是fish shell的新手,我试图通过尝试为程序编写自己的完成脚本来理解语法。 Help would be greatly appreciated. 非常感谢帮助。

Thank you. 谢谢。

In fact -eq and -a are not part of fish syntax. 实际上-eq-a不是鱼语法的一部分。 They are ordinary arguments! 他们是普通的论点!

if [ (count $cmd) -eq 1 -a $cmd[1] = 'git' ]

The opening square bracket here is actually a command, like cat or grep. 这里的开口方括号实际上是一个命令,如cat或grep。 You really do have a file /bin/[ . 你确实有一个文件/bin/[ It may be easier to understand via the test command, which is the same thing: 通过test命令可能更容易理解,这是同样的事情:

if test (count $cmd) -eq 1 -a $cmd[1] = 'git'

Now it's easy to see that -eq and -a are just ordinary arguments being passed to test , with no syntactic significance to fish. 现在很容易看出-eq-a只是传递给test普通参数,对于鱼没有语法意义。

test has its own little language like awk or sed. test有自己的小语言,如awk或sed。 See man test to learn about it. 请参阅man test以了解它。

The -eq is an integer comparison function . -eq整数比较函数

The -a is a logical and . -a合乎逻辑的

So the logical equivalent would be something like: 所以逻辑等价物将是这样的:

if [ (count $cmd) == 1 && $cmd[1] == 'git' ]

(in Java pseudo-syntax). (在Java伪语法中)。

Background 背景

The reason why -eq is used is because a shell normally works with text processing only. 使用-eq的原因是因为shell通常仅用于文本处理。 As a result numbers are stored in "strings". 结果数字存储在“字符串”中。 Sometimes two numbers are equivalent, but not string-equivalent. 有时两个数字是等价的,但不是字符串等价的。 For instance the following example: 例如以下示例:

if [ "01" -eq "1" ]
then
    echo "integer equal"
fi
if [ "01" = "1" ]
then
    echo "string equal"
fi

Will only print integer equal . 只打印integer equal

From the Fish documentation : 来自Fish文档

  • NUM1 -eq NUM2 returns true if NUM1 and NUM2 are numerically equal. 如果NUM1和NUM2在数值上相等,则NUM1 -eq NUM2返回true。
  • COND1 -a COND2 returns true if both COND1 and COND2 are true. 如果COND1和COND2都为真,则COND1 -a COND2返回true。

It tests that (count $cmd) = 1 and that $cmd[1] = 'git' . 它测试(count $cmd) = 1$cmd[1] = 'git'
( = here being equality, not an assignment). =这里是平等,而不是任务)。

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

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