After years of 'just watching' i finally need your help! I cant find a solution for this puzzle.
I am trying to make a script that checks if a server is running on my ubuntu platform, if there is echo "smthing" else echo "other_thing".
I am blocked on the start of it because i need to check my localhost:port for a connection.
I am using "nc -zv localhost 80" for testing purposes.
my idea: If port 80 is on echo "yes" if dont echo "No."
I always get errors or miss results because the code seems to be not good.
#! /bin/sh
cmd=`nc -zv localhost 80`
answer="Connection to localhost 80 port [tcp/http] succeeded!"
if [ "$cmd" = "$answer" ]; then
echo "Yes"
else
echo "No"
fi
exit 0
Thank you!!
$()instead of backticks). I wonder what can be wrong. What is the output ofnc -zv localhost 80? What do you get if you sayecho "$cmd"?printf '%q\n' "$cmd", rather thanecho "$cmd", to ensure that any hidden characters get displayed.ncends with a carriage-return/newline pair; the command substitution strips the newline but leaves the carriage return.