if fi [:太多的论点 [英] if fi [: too many arguments

查看:148
本文介绍了if fi [:太多的论点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个脚本有问题吗?

if [ grep -q "#<filter>" /etc/hosts ] &&  [ grep -q "#</filter>" /etc/hosts ] ; then
 echo "exist"
fi

它显示的错误是:

[: too many arguments


推荐答案

问题是您在 [语法中包装普通命令。这是无效的。 [标记实际上是一个命令,它将其参数解释为用于进行字符串和数字比较的非常有限的表达式语言,以及文件和目录的条件,例如测试必须使用类型,存在,时间戳。

The problem is that you are wrapping ordinary commands in the [ syntax. This is not valid. The [ token is actually a command, which interprets its arguments as a very limited expression language for doing string and numeric comparisons, and conditions on files and directories, such as tests having to do with type, existence, timestamps.

在shell语法中,您可以在(...)括号。这样做的结果是在新的子进程中运行所附的命令。您还可以使用大括号对命令进行分组以创建组命令。

In the shell syntax, you can wrap a sequence of commands in (...) parentheses. The effect of this is to run the enclosed commands in a new child process. You can also group commands with braces to create a group command.

&& || 命令有同等的优先权;它不像C语言中的类似排序。基本上如果你有一个命令,你可以使用 ||来处理另一个命令另一个命令&&另一个命令 || 运算符表示:如果此处左侧的所有内容都失败,请运行以下命令。 执行此操作,如果失败,请执行此操作。 && 运算符表示:如果到目前为止一切都成功,也请执行以下操作。

The && and || commands have equal precedence; it is not like the analogous sequencing in the C language. Basically if you have a command, you can tack on another command using || another command or && another command. The || operator means: run the following command if everything to the left of here failed. "Do this, or else if it fails, do this". The && operator means: if everything succeeded so far, also do the following.

如果您需要逻辑如(A || B)&& (C || D) - 换句话说,A,B,C和D必须按该顺序运行,并且您希望每个 ||中至少有一个命令组成功,可以使用这样的大括号:

If you want logic like (A || B) && (C || D) -- in other words, A, B, C and D must be run in that sequence, and you want at least one command in each || group to succeed, that can be done with braces like this:

{ A || B; } && { C || D; }

或括号,如果您不介意分叉子流程来控制评估。请注意,group命令语法中需要分号;或者必须使用换行符,并且大括号周围必须有空格。

or with parentheses, if you don't mind forking child processes just to control evaluation. Note that the semicolons are required in the group command syntax; or else newlines must be used, and there must be whitespace around the braces.

没有大括号, A || B&& C || D A ||的含义相同{B&& {C || d; } :它基本上是正确的,具有相同的优先级。顺便说一句,& 运算符(后台作业)的优先级较低 A || B&& C || D& 意味着将整个事情放在后台,而不仅仅是 D

Without the braces, A || B && C || D means the same thing as A || { B && { C || D; } }: it is essentially right associative with equal precedence. And, by the way, the & operator (backround job) has a lower precedence A || B && C || D & means to put the whole darn thing in the background, not only D.

这篇关于if fi [:太多的论点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆