【问题标题】:read: Illegal option -s in shell scripting阅读:shell脚本中的非法选项-s
【发布时间】:2016-03-25 19:40:11
【问题描述】:

我尝试运行此代码:

#!/bin/bash
read -s "Password: " password

使用命令:

run sh init.sh

它会引发错误:read: Illegal option -s。任何帮助。

【问题讨论】:

  • 和 Ruby 无关。
  • 那么壳呢?

标签: bash shell


【解决方案1】:

我认为您使用的是 Debian/Ubuntu 或 BSD 衍生产品?

当你执行像run sh init.sh 这样的命令时(虽然我自己并不熟悉这个run 命令)你正在覆盖#!/bin/bash shebang。在您的情况下,sh 是一个严格兼容的 POSIX shell,例如dash,实际上,read 的唯一参数不是扩展名是-r

所以也许您想改用run bash init.sh

【讨论】:

  • 有没有办法强制执行shebang?如果我将脚本提供给其他人并且不想提醒他们“不要使用 sh script.sh 运行此脚本,它必须是 ./script.sh”。
  • @rococo 没有办法“强制执行”shebang。只有当脚本本身exec'd 时才会评估 Shebang。如果不需要提醒人们的问题,我建议将脚本重命名为init.bash
猜你喜欢
  • 1970-01-01
  • 2013-08-20
  • 2016-06-13
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 2014-09-02
相关资源
最近更新 更多