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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18308913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:17:41  来源:igfitidea点击:

How can I resolve this error in shell scripting: "read: Illegal option -t"?

bashshellubuntu

提问by Utkarsh Parekh

#!/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

I have written above code in terminal and got error "read: Illegal option -t".

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

采纳答案by Karoly Horvath

Bash supports -t, so it looks like you're trying to execute it with shor some other shell, which is odd, since you have the correct shebang.

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

Make sure you run it with ./scriptor path_to_script/script. If you just run it in the terminal, first start bash.

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

回答by Adrian Frühwirth

bashsupports the -toption for the readbuiltin since version bash-2.04(see ChangeLog), so either you are using an ancient version of bash(<= 2.03) or are not really running your script under bash.

bash支持自版本以来-tread内置选项bash-2.04(请参阅ChangeLog),因此要么您使用的是bash(<= 2.03)的旧版本,要么没有真正在bash.

Run bash --versionto check the version and double-check that your shebang really looks like #!/bin/bashin your script.

运行bash --version以检查版本并仔细检查您的 shebang 是否真的#!/bin/bash在您的脚本中看起来像。