linux - 使用 if else 在文件中搜索模式的 Shell 脚本

标签 linux shell scripting tcsh

这是我的脚本,我想在文件中查找模式。我知道grep -q '<Pattern>' '<file>' && echo $?的退出状态如果找到模式则为 0。但我收到 if: 表达式语法 错误。

 if ( (grep -q '<Pattern>' '<file>' && echo $?)==0  ) then
 echo "Pattern found"
 else
 echo "Pattern not found"
 endif

最佳答案

我想你想要这个:

if ( { grep -q '<Pattern>' '<file>' } ) then
 echo "Pattern found"
else
 echo "Pattern not found"
endif

注意命令周围的大括号以及大括号和命令之间的空格。

请参阅man tcsh , 表达式:

Command exit status

Commands can be executed in expressions and their exit status returned by enclosing them in braces ('{}'). Remember that the braces should be separated from the words of the command by spaces.

关于linux - 使用 if else 在文件中搜索模式的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52060475/

相关文章:

linux - Bash 脚本忽略 if, then 语句;每次返回相同的结果

email - 发送苹果邮件中的所有草稿

bash - 如何将数字拆分为字符数组

python - 在 python 中定义 linux 命令的命令行参数

mysql - 日期格式问题 STR_TO_DATE ('18:11:52' , '%H:%i:%s' ) 返回 NULL

linux - AWS EC2 Linux : Cannot open shared object file

linux - unix - chmod 文件的默认 future 权限

linux - “git remote”显示的 url 与 .git/config 中设置的不同

macos - 为什么找不到让我匹配多个模式?

linux - 在 UNIX 中以相反的顺序获取句子