shell - 在 if 中执行 grep
问题描述
如何在 if 语句中执行 grep?我想检查一个字符串是否包含子字符串。
DSU_DIFF="$(sudo php app/console d:s:u --dump-sql)"
# I want to check if the $DSU_DIFF contains the sub-string "Nothing to update"
if [ "$DSU_DIFF" | grep "Nothing to update" ]
then
echo "Contains substring"
else
echo "No substring contained"
fi
这在语法上是错误的,我应该如何尝试?
解决方案
为什么要使用grep
它?
您可以使用case
:
case "$DSU_DIFF" in
*'Nothing to update'*)
echo "Contains substring";;
*)
echo "No substring contained";;
esac
推荐阅读
- bash - 脚本更改方式代码功能的命令行参数
- ansible - Ansible EC2 动态清单的问题
- javascript - Zapier-dalać 在操作字段(自定义)字段中包含 u'
- wpf - 如何将我的 .NET Framework WPF 项目迁移到 .NET Core?
- xcode - 有没有办法避免手动运行“pod install”而不是同时提交 Pods 目录?
- java - 通过 bean 名称编写可选的 bean 依赖注入
- javascript - node-sparkpost 不包括电子邮件中的附件
- c# - 将 System.Object 类型的包变量转换为 List
使用 SSIS C# 脚本任务 - reactjs - 如何在 react-admin 中隐藏列表工具栏?
- c++ - 我可以将从相机获取的单通道图像输出到 winAppi 窗口吗?