You are over-complicating things a bit. Your command could be re-written like this:
if grep "1 host up" <(nmap -O "$i")
then
echo "UP"
else
echo "DOWN"
fi
Some explanations:
- We use process substitution (with the
<( )
to redirect the output of the command as a parameter to the grep command. - The grep command returns zero if it finds a match, and non-zero otherwise. Thus, we can use it directly in the if statement
You can also avoid the if all together like this:
grep "1 host up" <(nmap -O "$i") && echo "UP" || echo "DOWN"