bash Ubuntu 中的重定向器“<<<”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16045139/
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
Redirector "<<<" in Ubuntu?
提问by jmginer
I'm getting this error
我收到这个错误
Syntax error: redirection unexpected
语法错误:重定向意外
in the line:
在这一行:
if grep -q "^127.0.0." <<< "$RESULT"
How I can run this in Ubuntu?
我如何在 Ubuntu 中运行它?
回答by Keith Thompson
<<<is a bash-specific redirection operator (so it's not specific to Ubuntu). The documentationrefers to it as a "Here String", a variant of the "Here Document".
<<<是一个特定于 bash 的重定向操作符(所以它不是特定于 Ubuntu)。所述文档是指它作为一个“在这里字符串”,所述的变体“在这里文档”。
3.6.7 Here Strings
A variant of here documents, the format is:
<<< word
The wordis expanded and supplied to the command on its standard input.
3.6.7 这里的字符串
此处文档的一个变体,格式为:
<<<字
这个词被扩展并提供给它标准输入上的命令。
A simple example:
一个简单的例子:
$ cat <<< hello
hello
If you're getting an error, it's likely that you're executing the command using a shell other than bash. If you have #!/bin/shat the top of your script, try changing it to #!/bin/bash.
如果您收到错误消息,很可能是您使用 bash 以外的 shell 执行命令。如果您#!/bin/sh的脚本顶部有 ,请尝试将其更改为#!/bin/bash.
If you try to use it with /bin/sh, it probably assumes the <<refers to a "here document", and then sees an unexpected <after that, resulting in the "Syntax error: redirection unexpected" message that you're seeing.
如果您尝试将它与 一起使用/bin/sh,它可能会假定<<引用“此处的文档”,然后在此之后看到意外<,从而导致您看到的“语法错误:重定向意外”消息。
zsh and ksh also support this syntax.
zsh 和 ksh 也支持这种语法。
回答by Hal Canary
if grep -q "^127.0.0." <<< "$RESULT"
then
echo IF-THEN
fi
is a Bash-specific thing. If you are using a different bourne-compatable shell, try:
是 Bash 特有的东西。如果您使用的是不同的与 bourne 兼容的 shell,请尝试:
if echo "$RESULT" | grep -q "^127.0.0."
then
echo IF-THEN
fi
回答by Jakuje
It works for me on Ubuntu, if I complete you IF block:
如果我完成了 IF 块,它在 Ubuntu 上对我有用:
if grep -q "^127.0.0." <<< "$RESULT"; then echo ""; fi

