bash - 使用 ls | grep 在 if 语句中
问题描述
我正在尝试使用 if [grep] 使用退出值来触发它
从命令行进行测试
$ ls ~/dir | grep txt
$ echo $?
0
但是,当我在脚本的 if 语句中使用它时,即
if [ ls /some/dir | grep -q pattern ]; then
echo y
fi
它说
line 1: [ missing `]' (the line the if statement was written on)
为什么会发生这种情况,有没有办法解决它?
解决方案
推荐阅读
- amazon-web-services - AWS ECR 事件触发的 Cloudwatch 事件规则
- java - IntelliJ 和 Eclipse IDE 之间的不同编译时间
- android - 当我使用 LAN 时,Wifi 中的模拟器没有连接
- python-3.x - 如何按键合并两个数组和分组?
- c# - 从用 C# 编写的 Windows 服务运行进程时出现内存问题
- scala - Sparksql 使用 Scala
- angular - Visual Studio 中的新 Angular 应用,polyfill.js 中有很多 JavaScript 错误
- modelica - OpenModelica 抱怨不能为负的负值
- android - 数字标牌
- logging - 如何获取特定应用程序的日志?