문제

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