سؤال

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")

هل كانت مفيدة؟

المحلول 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.

نصائح أخرى

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

if nc ...
then
    play the game
else
    exit
fi
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top