I need to check if a file contains a specific line. This file is written continuously by someone, so I put the check inside a while loop.
FILE="/Users/test/my.out"
STRING="MYNAME"
EXIT=1
while [ $EXIT -ne 0 ]; do
if [ -f $FILE ] ; then CHECK IF THE "STRING" IS IN THE FILE - IF YES echo "FOUND"; EXIT=0; fi
done
The file contains text and multiple lines.
Just run the command directly. Add -q option when you don't need the string displayed when it was found. The grep command returns 0 or 1 in the exit code depending on the result of search.
if $FILE
contains the file name and $STRING
contains the string to be searched,
then you can display if the file matches using the following command:
if [ ! -z $(grep "$STRING" "$FILE") ]; then echo "FOUND"; fi
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With