If statement to compare two strings

Hi,

I am trying to do the following to see if "ip" is already present in a file.

if ["$ip" == "(cat /etc/hosts.deny | grep {$ip})"]; then
		echo "hi"
	else
		 echo "hello"
	fi

I am seeing errors on the if statement. Can someone please correct the syntax for me? Thanks

Try the following:

if grep -q "$ip" /etc/hosts.deny; then
    echo "hi"
else
    echo "hello"
fi

or

grep -q "$ip" /etc/hosts.deny && echo "hi" || echo "hello"

---------- Post updated at 08:31 PM ---------- Previous update was at 08:14 PM ----------

By the way, the errors most likely are because it needs spaces between [ and ]
Nevertheless, you are saying something you do not intend to.

The value stored in the variable ip is equal to the literal string formed with the characters (cat /etc/hosts.deny | grep {[*]value in ip variable})
Very unlikely.

[*] the ip variable will be expanded and its content will show instead.

A few comments on top of what Aia already said

  • unless hosts.deny contains EXACTLY ONE line consisting only of that $ip contents, the test will fail
  • you need to deploy "command substitution" $(...) (c.f. man bash )
  • the test can be phrased way simpler and safer or even omitted (see Aia's proposal)