“!”是什么意思?在`同时!数控...;做...;完成`的意思

What does the "!" in `while ! nc ...; do...; done` mean

我是 bash 的新手,我遇到了这段代码。

j=0
while ! nc -z "$host" "$port"; do
  j=$((j+1))
  if [ $j -ge 10 ]; then
    echo >&2 "$host:$port not reachable, giving up"
    exit 1
  fi
done

我无法理解 nc 之前的 ! 在这里是如何工作的。谁能帮忙解释一下?

谢谢

这里,!是一个关键字(感谢user1934428指正)进行了NOT操作

如果命令 nc -z "$host" "$port" 没有成功执行,它将 return "false"(即非零值)。因此,! [nc command] 将 return "true"(即零)。

所以就像 "while this nc command fails, do the loop. After ten tries ($j is greater than or equal to 10), give up"。

您可能想看看这个 interactive tutorial and this Wikibook