#!/usr/bin/expect -f
ps
ps -ef > test.txt
现在,如果我要检查test.txt中是否存在某些关键字,该如何处理?
我们说“ apache”或“ fast”。
我们可以在这里使用if语句吗?如果可以,如何使用?
首先,第一个shebang行不是您想要的脚本。期望作为外壳具有有限的用途,这不是其中之一
第一行应该是这样的
#!/bin/bash
在你的情况下
然后
ps -ef > test.txt
grep -e fast -e apache test.txt
将打印出包含这些单词之一的所有行。
或者,您也可以跳过“写入文件”步骤,并像这样一行处理:
ps -ef | grep -e fast -e apache
编辑条件检查:
ps -ef | grep -e fast -e apache | grep -v grep > dev/null; result=${?}
if [ ${result} -eq 0 ]
then
echo "Found one or more occurrences of 'apache' and/or 'fast'"
else
echo "Searched strings were not found"
fi
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句