我想下载网页的源代码,然后检查一个字符串是否存在以及另一个字符串是否存在。我下面有可用的代码-是否可以将整个脚本作为单行命令执行?
PAGE_SOURCE=`curl https://example.com`
if ! echo "$PAGE_SOURCE" | grep -q "string that is bad"; then
if echo "$PAGE_SOURCE" | grep -q "string that is good"; then
exit 0
fi
fi
exit 1
您可以使用
if grep -Pq '^(?!.*string that is bad).*string that is good'; then
exit 0
fi
正则表达式中针对包含一个单词但不包含另一个线程的字符串描述的众所周知的PCRE模式应放在单引号引起来的字符串中,否则,!
由于用于扩展历史记录,因此需要转义char。
简短的模式说明:
^
-字符串开始(?!.*string that is bad)
-否定的超前查询,如果除换行符以外的其他0+个字符尽可能多,则匹配失败,然后string that is bad
立即位于当前位置的右侧.*string that is good
-除换行符以外的任何0+字符,然后再按string that is good
。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句