bash 意外标记“&”附近的语法错误(使用“|&”)

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

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

bashshellawk

提问by klerk

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

&在接下来的意外标记附近有语法错误:在 bash 脚本中,它是这样的:

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

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

当我在 cli 上发出这个命令时,我得到了很好的输出,问题是当我在脚本中调用它时,我可以从 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 }''

回答by Keith Thompson

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

您可以使用该序列|&将 stdout 和 stderr 从一个进程传送到另一个进程。

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.

|和之间不能有空格&。(csh 和 tcsh 允许有空格;bash 不允许。)我怀疑您在交互式运行命令时碰巧在没有空格的情况下键入它;无论哪种方式,语法都是相同的。

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.

使用 bash 3.2.25,|&无法识别令牌;在 bash 4.1 中作为新功能添加。使用较旧的 bash 运行您的脚本,我收到与您相同的错误消息。

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

要使您的脚本与旧版本的 bash 兼容,只需在不使用|&运算符的情况下执行等效重定向:

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

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

进一步的改进:使用$(...)而不是`...`

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

You can also incorporate the grepsearch into the awkcommand:

您还可以将grep搜索合并到awk命令中:

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

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/bashis a different version than what you're running interactively? Try /bin/bash --versionand echo $BASH_VERSION.

我仍然看不到|&和之间的区别| &。是否有可能/bin/bash与您交互式运行的版本不同?尝试/bin/bash --versionecho $BASH_VERSION

回答by Jotne

Try change to:

尝试更改为:

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

回答by klerk

Thanks Keith I have no problems anymore you saved my day :) so the bash version was the problem and with avoiding of & I have no errors, thanks also for fine tuning :)

谢谢基思,我再也没有问题了,你救了我的一天 :) 所以 bash 版本是问题所在,并且避免了 & 我没有错误,也感谢您的微调:)

so this works with my bash version

所以这适用于我的 bash 版本

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