Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i am doing something wrong here?

I am looking for exist status 1 so that i can use it in if condition

[test system] /var/opt/oracle/test>cat test
london
america
australia
[test system] /var/opt/oracle/test>grep london test
london
[test system] /var/opt/oracle/test>grep london test|echo $?
0
[test system] /var/opt/oracle/test>grep england  test|echo $?
0
[test system] /var/opt/oracle/test>

Use semicolon to execute commands consecutively. You are incorrectly using pipe instead.
The easiest way is to do:

if grep -q string file.txt ; then 
  echo "string found" 
else
  echo "not found"
fi 
1 Like

Try this in place of |

grep 'england' test ; echo $?