如何在 shell 脚本中解决此错误:“读取:非法选项 -t”?

Utk*_*ekh 5 bash shell ubuntu

#!/bin/bash
echo -n "Hurry up and type something! > "
if read -t 10 response ; then
echo "Greate, you made it in time!"
else
echo "sorry, you are too slow!"
fi
Run Code Online (Sandbox Code Playgroud)

我在终端中编写了上面的代码并收到错误“读取:非法选项 -t”。

Kar*_*ath 5

Bash 支持-t,因此看起来您正在尝试使用sh其他 shell执行它,这很奇怪,因为您拥有正确的 shebang。

确保使用./script或运行它path_to_script/script。如果您只是在终端中运行它,请先启动bash.