Question

Hello i have a problem in this script This feature is apparently bad but how to fix it?

nc -z -v ip adress port
if connect to ip adress port 13000 (tcp) failed: Connection refused
then
cd /usr/home/game/
./game
else
exit
fi

error :

5: Syntax error: "(" unexpected (expecting "then")

Was it helpful?

Solution 2

result=$(nc -z -v $ipaddress $port 2>&1)
if [ "$result" = "connect to ip adress port 13000 (tcp) failed: Connection refused" ]
then
    cd /usr/home/game
    ./game
else
    exit
fi

$(...) substitutes the output of the command into the command line. And 2>&1 redirects standard error to standard output, so the error message will be captured by this.

OTHER TIPS

You could probably just act based on the exit status of nc:

if nc ...
then
    play the game
else
    exit
fi
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top