======================
Backup - OK -
Backup - OK -
Backup - ERROR -
Backup - WARNING -
======================
I want to search the text file for the string ERROR.
If this string is present in the text file, there should be an output.
I tried following in the bash, but it won't work. Maybe you have an idea?
if [ "$(cat database.txt)" = "ERROR" ];
then
echo "Found an ERROR."
elif [ "$(cat database.txt)" = "WARNING" ];
then
echo "Found a WARNING".
else
echo "Backup sucessful."
fi
I get always the output: Backup sucessful, but in the text file the string ERROR is present.
Thanks for your help.
greetz
lino
edit by bakunin: please use code-tags for code-parts or output. Thank you.
The problem with this is that you are asking if "cat database.txt" is exactly "ERROR", which of course it isn't. For tasks like yours, where you want to know if a text contains a certain string there is a Unix command named "grep". Have a look at the man page for grep to find out what you could do with it (which is quite a lot, i can assure you).
Your code should look like:
if [ $(grep -c "ERROR" database.txt) = 0 ]; then
print - "no error found"
else
print - "error found"
fi
Similar for "WARNING", etc..
I hope this helps (and you don't forget your code-tags from now on ).