grep在if处有太多参数 [英] Too much arguments with grep at if
问题描述
我试图在文件中寻找$allowtunnel
字符串.
I was trying to look for $allowtunnel
string at my file.
基于:如何测试是否Bash shell文件中是否存在字符串?
所以现在我在脚本中有以下内容:
So now I've the following at my script:
allowtunnel="PermitTunnel"
if [ grep -Fxq $allowtunnel /etc/ssh/sshd_config ]; then
因为在执行时它说参数太多",所以我一直在寻找并发现:
Since at execution time it says "too much arguments", I was looking and found this:
所以我将其更改为:
if [[ grep -Fxq $allowtunnel /etc/ssh/sshd_config ]]; then
但是现在,它为我提供了下一个:
But now, It delivers me the next one:
./script_install.sh: línea 242: se esperaba un operador binario condicional
./script_install.sh: línea 242: error sintáctico cerca de `-Fxq`
./script_install.sh: línea 242: ` if [[ grep -Fxq $allowtunnel /etc/ssh/sshd_config ]]; then`
翻译为:
./script_install.sh: line 242: binary conditional operator was expected
./script_install.sh: line 242: syntax error near of `-Fxq`
./script_install.sh: line 242: ` if [[ grep -Fxq $allowtunnel /etc/ssh/sshd_config ]]; then`
据我所知,它不允许我使用第二个[]
,因为它需要另一个运算符.那让我不知道该怎么办.
So as I understand, It doesn't let me use a second []
because it expects another operator. That leaves me without knowing what to do.
我也看到了这一点: bash中的参数过多错误,但是由于我不是在比较什么,而是在等待grep的结果,因此我可以从中得到任何有用的信息.
I've also seen this one: Too many arguments error in bash , but since I'm not comparing something but waiting for the result of grep I get anything useful from this one.
感谢阅读
推荐答案
由于grep返回退出状态(取决于是否找到记录),因此可以编写if语句,如下所示:
Since grep returns an exit status depending if the record is found or not, you could write your if statement like:
if grep .....; then
这篇关于grep在if处有太多参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!