grep.sh

#!/bin/bash

success=0
word=22222
filename=/root/1.txt

grep -q "$word" "$filename"
#-q:静默查找,不输出到stdout上

if [ $? -eq $success ];then
  echo "$word found in $filename."
else
  echo "$word not found in $filename."
fi
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

验证:

[root@logstash ~]# sh grep.sh 
22222 found in /root/1.txt.
[root@logstash ~]#
  • 1.
  • 2.
  • 3.