Redirector “<<<” in Ubuntu?

一世执手 提交于 2019-11-29 16:43:41

问题


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?


回答1:


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:

if echo "$RESULT" | grep -q "^127.0.0."
then
    echo IF-THEN
fi



回答2:


<<< is a bash-specific redirection operator (so it's not specific to Ubuntu). The documentation refers to it as a "Here String", a variant of the "Here Document".

3.6.7 Here Strings

A variant of here documents, the format is:

<<< word

The word is expanded and supplied to the command on its standard input.

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/sh at the top of your script, try changing it to #!/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.

zsh and ksh also support this syntax.




回答3:


It works for me on Ubuntu, if I complete you IF block:

if grep -q "^127.0.0." <<< "$RESULT"; then echo ""; fi


来源:https://stackoverflow.com/questions/16045139/redirector-in-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!