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

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

read: Illegal option -s in shell scripting

bashshell

提问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 runcommand) you are overriding the #!/bin/bashshebang. In your case shis a strictly compliant POSIX shell like dash, where, in fact, the only argument to readthat is not an extension is -r.

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

So maybe you'd want to use run bash init.shinstead?

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

回答by anubhava

You need to use -poption 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