简体   繁体   English

如何在xargs命令中使用&>>?

[英]How to use &>> in a xargs command?

Experts, 专家介绍,

Following this question I execute this command: 跟随这个问题,我执行以下命令:

xargs argument using posix: 使用posix的xargs参数:

find <somePath> -name '*.html' | sort -n | xargs -I{} sh -c 'w3m "{}" &>> "test.log"' --{}

I get the following errors: 我收到以下错误:

  • file.hml: -c: line 0: syntax error near unexpected token `>' file.hml:-c:第0行:意外令牌'>'附近的语法错误
  • file.hml: -c: line 0: `w3m ".html" &>> "test.log"' file.hml:-c:第0行:“ w3m“ .html”&>>“ test.log”“

Why does bash not understand &>> in my line execution? 为什么bash在我的行执行中不理解&>>?

As @Nahuel Fouilleul said "sh (posix) doesn't recognize the (bash) redirection &>>". 正如@Nahuel Fouilleul所说,“ sh(posix)无法识别(bash)重定向&>>”。

xargs argument using bash 使用bash的xargs参数

However the solution proposed is not working when I execute the following command: 但是,当我执行以下命令时,建议的解决方案不起作用:

find <somePath> -name '*.html' | sort -n | xargs -I{} w3m {} &>> "test.log"

I receive this error: 我收到此错误:

  • bash: syntax error near unexpected token `>' bash:意外令牌'>'附近的语法错误

Why does bash not understand &>> in my line execution? 为什么bash在我的行执行中不理解&>>? See my answer 看我的答案

it may not work if sh (posix) doesn't recognize the (bash) redirection &>> . 如果sh (posix)无法识别(bash)重定向&>>则可能无法正常工作。 but the command still can break for certain file names. 但是该命令对于某些文件名仍然可以中断。

Command should be. 命令应该是。

.. | xargs -I{} bash -c 'w3m "$1" &>> "test.log"' "process_name_you_want" {}

or to avoid to open log file for each file redirection can be moved out 或者避免打开日志文件,因为每个文件的重定向都可以移出

.. | xargs -I{} w3m {} &>> "test.log"

EDIT: answer is general to a tool which processes files and write to output, after looking at w3m tool it seems it's a web browser in a console, what are you trying to do ? 编辑:答案是处理文件并写入输出的工具的一般性,在查看w3m工具后,它似乎是控制台中的Web浏览器,您打算做什么?

From here I realized that the operator "&>>" is only available from Bash version 4 . 这里我意识到,运算符“&>>”仅在Bash版本4中可用。 I am using Bash version 3.2.51 so it doesn't work. 我正在使用Bash版本3.2.51,所以它不起作用。

For versions previous to bash 4 two operators are needed: 对于bash 4之前的版本,需要两个运算符:

w3m >> test.log 2>&1

where: 哪里:

">>" append stdout to test.log “ >>”将stdout附加到test.log

"2>&1" redirect the stderr to where the sdout is going “ 2>&1”将stderr重定向到sdout要去的地方

So the command working with xargs is the following: 因此,使用xargs的命令如下:

.. | xargs -I{} w3m {} >> "test.log" 2>&1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM