【问题标题】:How can I resolve this error in shell scripting: "read: Illegal option -t"?如何解决 shell 脚本中的此错误:“读取:非法选项 -t”?
【发布时间】:2013-08-20 23:04:24
【问题描述】:
#!/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

我在终端中写了上面的代码,得到了错误“read: Illegal option -t”。

【问题讨论】:

  • 我没有收到任何错误。与GNU bash, Version 4.2.37(1)-release (x86_64-pc-linux-gnu) 一起工作正常
  • 我在 ubuntu 12.10 32-bit pc 中遇到了这个错误
  • 检查您使用的 bash 版本:echo $BASH_VERSION

标签: bash shell ubuntu


【解决方案1】:

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

确保使用./scriptpath_to_script/script 运行它。如果只是在终端运行,先启动bash

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,然后我发现我使用的是#!/bin/sh 而不是#!/bin/bash。更改 shebang 后,一切都按预期工作。

    【讨论】:

      【解决方案3】:

      bash 支持read 内置的-t 选项,因为版本bash-2.04(请参阅ChangeLog),因此您使用的是旧版本的bash(2.03)或并没有真正在bash 下运行您的脚本。

      运行 bash --version 以检查版本并仔细检查您的 shebang 是否真的与脚本中的 #!/bin/bash 相似。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-18
        • 2022-11-21
        • 2020-07-14
        相关资源
        最近更新 更多