Code:
for n in 2 3 4 5 6 7 8 9 10; do
dir=/opt/server$n
if [ -d "$dir" ] && ! netstat -ant | grep -q 0:$(($n + 4999)); then
screen -dmS "S$n" "$mono" "$exe" "$dir/" >/dev/null 2>&1
fi
done
Also, don't use
grep … | wc -l. This can almost always be replaced with
grep -c … and very often what you really want is grep's exit code as used above.