Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirector "<<<" in Ubuntu?

Tags:

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?

like image 635
jmginer Avatar asked Apr 16 '13 19:04

jmginer


People also ask

How do I redirect in Ubuntu?

The standard output (STDOUT) or standard error (STDERR) of one command can be redirected as the standard input (STDIN) for another command using the “>” I/O redirection, and similarly, a standard input (STDIN) can be redirected as the standard output (STDOUT) for another command using the “<” I/O redirection.

Why do we use 2 >> redirection?

Using “2>” re-directs the error output to a file named “error. txt” and nothing is displayed on STDOUT. 2. Here, 2>&1 means that STDERR redirects to the target of STDOUT.

How do I redirect in Linux?

In Linux, for redirecting output to a file, utilize the ”>” and ”>>” redirection operators or the top command. Redirection allows you to save or redirect the output of a command in another file on your system. You can use it to save the outputs and use them later for different purposes.

What do you mean by redirection in Linux?

Input/Output (I/O) redirection in Linux refers to the ability of the Linux operating system that allows us to change the standard input ( stdin ) and standard output ( stdout ) when executing a command on the terminal. By default, the standard input device is your keyboard and the standard output device is your screen.


2 Answers

<<< 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.

like image 83
Keith Thompson Avatar answered Oct 15 '22 12:10

Keith Thompson


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 
like image 40
Hal Canary Avatar answered Oct 15 '22 12:10

Hal Canary