[英]bash script check strings in file
我该怎么做:
if !("abc" in file1 and "def" in file2)
then
echo "Failed"
fi
我已经知道如何检查file1中的“ abc”: grep -Fxq "abc" file1
,但是我无法设法使if not (command1 and command2)
部分工作。
你说得差不多了。 只需在感叹号和grep命令之间添加一个空格,它将起作用:
if ! (grep -Fxq "abc" file1 && grep -Fxq "def" file2); then
echo "Failed"
fi
假设bash
,则不需要else
。
请注意,使用括号在子外壳程序环境中作为子外壳程序进程运行抓钩。 您可以使用花括号轻松地避免这种情况(此事情称为组命令):
if ! { grep -Fxq "abc" file1 && grep -Fxq "def" file2; }; then
echo "Failed"
fi
请注意,您需要更多的空间和一个额外的分号bash
语法不是很有趣!!!
您可以这样做:
$ grep -Fxq "abc" file1 && grep -Fxq "def" file2 || echo "Failed"
这使用bash
逻辑运算符AND &&
和OR ||
。
这可以分为多行,例如:
$ grep -Fxq "abc" file1 &&
> grep -Fxq "def" file2 ||
> echo "Failed"
if (grep -Fxq "abc" file1 && grep -Fxq "def" file2);
then
echo ""
else
echo "failed"
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.