提问者:小点点

与 grep 的争论太多 if


我正在尝试在我的文件中查找$let隧道字符串。

基于: 如何使用 Bash shell 测试文件中是否存在字符串?

所以现在我的脚本有以下内容:

allowtunnel="PermitTunnel"
if [ grep -Fxq $allowtunnel /etc/ssh/sshd_config ]; then

因为在执行时它说“参数太多”,所以我看了一下,发现了这个:

Bash脚本-参数太多

所以我把它改成:

if [[ grep -Fxq $allowtunnel /etc/ssh/sshd_config ]]; then

但是现在,它为我提供了下一个:

./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`

所以据我所知,它不允许我使用第二个[],因为它需要另一个运算符。这让我不知道该怎么办。

我也看到过这个问题:bash中的参数太多错误,但由于我不是在比较什么,而是在等待grep的结果,所以我从这个问题中得到了任何有用的东西。

感谢阅读


共1个答案

匿名用户

因为grep根据记录是否被找到来返回退出状态,所以您可以这样编写if语句:

if grep .....; then