bash 阅读:shell 脚本中的非法选项 -s
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36214969/
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
read: Illegal option -s in shell scripting
提问by Vicky
I tried running this code:
我尝试运行此代码:
#!/bin/bash
read -s "Password: " password
With command:
使用命令:
run sh init.sh
it throws an error: read: Illegal option -s
. Any help.
它抛出一个错误:read: Illegal option -s
。任何帮助。
回答by Geoff Nixon
I take it you're using Debian/Ubuntu, or a BSD-derivative?
我认为您使用的是 Debian/Ubuntu 或 BSD 衍生版本?
When you execute a command like run sh init.sh
(although I'm not myself familiar with this run
command) you are overriding the #!/bin/bash
shebang. In your case sh
is a strictly compliant POSIX shell like dash
, where, in fact, the only argument to read
that is not an extension is -r
.
当您执行类似run sh init.sh
(虽然我自己不熟悉此run
命令)之类的命令时,您正在覆盖#!/bin/bash
shebang。在您的情况下sh
是一个严格兼容的 POSIX shell,例如dash
,其中,实际上,唯一read
不是扩展名的参数是-r
.
So maybe you'd want to use run bash init.sh
instead?
所以也许你想run bash init.sh
改用?
回答by anubhava
You need to use -p
option for prompt
:
您需要使用-p
选项prompt
:
read -sp "Password: " password
As per help read
:
根据help read
:
-p prompt output the string PROMPT without a trailing newline before
attempting to read