Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax error near unexpected token `&' (using "|&")

Tags:

bash

shell

awk

I have syntax error near unexpected token & in next : in bash scripts its'go like this :

#!/bin/bash
var1=`(/usr/bin/time cdifonline -CD 186821 -ALL > /dev/null)|& grep real|awk '{print $2}'`

when i issue this command on cli i get good output, problem is when invoke this in script i can get any output from var1

./check_cdifonline.sh: command substitution: line 2: syntax error near unexpected token `&'
./check_cdifonline.sh: command substitution: line 2: `(/usr/bin/time cdifonline -CD 186821 -ALL >/dev/null) | & grep real | awk '{print $2}''
like image 612
klerk Avatar asked Dec 12 '13 20:12

klerk


People also ask

How do I fix this error syntax error near unexpected token?

If you execute the code written in the Windows OS in the Cygwin, you may get the Syntax error near unexpected token '('. To fix the error, you need to clear the carriage return characters using the DOS to Unix command line tool as a text file format converter.

What does syntax error mean in Linux?

What Does Syntax Error Mean? A syntax error in computer science is an error in the syntax of a coding or programming language, entered by a programmer. Syntax errors are caught by a software program called a compiler, and the programmer must fix them before the program is compiled and then run.

What is unexpected token?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

What is syntax error in Bash?

Why the Bash unexpected token syntax error occurs? As the error suggests this is a Bash syntax error, in other words it reports bad syntax somewhere in your script or command. There are many things that can go wrong in a Bash script and cause this error.


1 Answers

You can use the sequence |& to pipe both stdout and stderr from one process to another.

You cannot have a space between the | and the &. (csh and tcsh allow a space; bash does not.) I suspect you happen to be typing it without the space when you run the command interactively; the syntax is the same either way.

This:

foo |& bar

is shorthand for this:

foo 2>&1 | bar

UPDATE :

With bash 3.2.25, the |& token is not recognized; was added as a new feature in bash 4.1. Running your script with the older bash, I get the same error message you do.

To make your script compatible with older versions of bash, just do the equivalent redirection without using the |& operator:

#!/bin/bash
var1=`(/usr/bin/time cdifonline -CD 186821 -ALL > /dev/null) 2>&1 | grep real | awk '{print $2}'`

Further refinements: Use $(...) rather than `...`:

#!/bin/bash
var1=$((/usr/bin/time cdifonline -CD 186821 -ALL > /dev/null) 2>&1 | grep real | awk '{print $2}')

You can also incorporate the grep search into the awk command:

#!/bin/bash
var1=$((/usr/bin/time cdifonline -CD 186821 -ALL > /dev/null) 2>&1 | awk '/real/ {print $2}')

Warning: I have not thoroughly tested these beyond verifying that they run without syntax errors.

I still see no difference between |& and | &. Is it possible that /bin/bash is a different version than what you're running interactively? Try /bin/bash --version and echo $BASH_VERSION.

like image 69
Keith Thompson Avatar answered Sep 28 '22 03:09

Keith Thompson