bash 阅读:非法选项 -d

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5098591/
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-17 23:31:18  来源:igfitidea点击:

read: Illegal option -d

bash

提问by mikewilliamson

Here is the offending part of my script:

这是我的脚本中令人反感的部分:

read -d '' TEXT <<'EOF'
Some Multiline
text that
I would like
in 
a 
var
EOF

echo "$TEXT" > ~/some/file.txt

and the error:

和错误:

read: 175: Illegal option -d

I use this read -d all over the place and it works fine. Not sure why its not happy now. I'm running the script on Ubuntu 10.10

我到处都使用这个 read -d 并且它工作正常。不知道为什么现在不开心。我在 Ubuntu 10.10 上运行脚本

Fixes? Workarounds?

修复?解决方法?

回答by paxdiablo

If you run shand then try that command, you get:

如果您运行sh然后尝试该命令,您将得到:

read: 1: Illegal option -d

If you do it while still in bash, it works fine.

如果您还在 中执行此操作bash,则效果很好。

I therefore deduce that your script is notrunning under bash.

因此,我推断您的脚本没有bash.

Make sure that your script begins with the line:

确保您的脚本以以下行开头:

#!/usr/bin/env bash

(or equivalent) so that the correct shell is running the script.

(或等效的)以便正确的 shell 运行脚本。

Alternatively, if you cannot do that (because the script is not a bashone), just be aware that -dis a bashfeature and may not be available in other shells. In that case, you will need to find another way.

或者,如果您不能这样做(因为脚本不是bash一个),请注意这-d是一项bash功能,可能在其他 shell 中不可用。在这种情况下,您将需要找到另一种方法。

回答by hobbs

The -doption to readis a feature unique to bash, not part of the POSIX standard (which only specifies -rand -poptions to read). When you run your script with shon Ubuntu, it's getting run with dash, which is a POSIX shell, and not bash. If you want the script to run under bash then you should run it with bash, or give it a #!/bin/bashshebang. Otherwise, it should be expected to run under any POSIX sh.

-d选项read是一种功能独特的来砸,而不是POSIX标准的一部分(这仅指定-r-p选项read)。当你sh在 Ubuntu 上运行你的脚本时,它会用 dash 运行,它是一个 POSIX shell,而不是 bash。如果您希望脚本在 bash 下运行,那么您应该使用 bash 运行它,或者给它一个#!/bin/bashshebang。否则,它应该可以在任何 POSIX sh 下运行。