macos 尝试打印终端中的行数时出现错误“xargs unterminated quote”

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

Getting error "xargs unterminated quote" when tried to print the number of lines in terminal

macosxargs

提问by Lena

I want to get the number of lines in my application. I am using this code:

我想获取应用程序中的行数。我正在使用此代码:

find . "(" -name "*.m" -or -name "*.h" ")" -print | xargs wc -l

It is working fine in other applications but for one of my applications it is giving the error "xargs unterminated quote".

它在其他应用程序中运行良好,但对于我的一个应用程序,它给出了错误“xargs 未终止的引用”。

回答by Gary G

Does one of your filenames have a quote in it? Try something like this:

您的文件名中是否有引号?尝试这样的事情:

find . "(" -name "*.m" -or -name "*.h" ")" -print0 | xargs -0 wc -l

The -print0argument tells findto use the NULL character to terminate each name that it prints out. The -0argument tells xargsthat its input tokens are NULL-terminated. This avoids issues with characters that otherwise would be treated as special, like quotes.

-print0参数告诉find使用 NULL 字符来终止它打印出的每个名称。该-0参数xargs表明其输入标记以 NULL 结尾。这避免了字符问题,否则会被视为特殊字符,例如引号。

回答by Brad Parks

This can happen because you have a single quote in a filename somewhere...

这可能是因为您在某处的文件名中有一个单引号......

ie -> '

即-> '

To find the problem file, run the following in the terminal:

要查找问题文件,请在终端中运行以下命令:

\find . | grep \' 

and it can also happen if you have an alias for xargs setup that's causing an issue. To test if this is the case, just run xargs with a '\' in front of it, e.g.

如果您有导致问题的 xargs 设置的别名,也会发生这种情况。要测试是否是这种情况,只需在 xargs 前面加上 '\' 运行,例如

\find . | \xargs ....

The "\" simply means "run the command without any aliases"

“\”仅表示“运行没有任何别名的命令”

回答by Ortomala Lokni

The canonical way to solve quotes, spaces and special characters problems when using findis to use the -execoption instead of xargs.

使用时解决引号、空格和特殊字符问题的规范方法find是使用-exec选项而不是xargs.

For your case you can use:

对于您的情况,您可以使用:

find . "(" -name "*.m" -or -name "*.h" ")" -exec wc -l "{}" \;

回答by ngopal

After some tinkering, I found that this command worked for me (because I had spaces and unmatched quotations in my filenames):

经过一番修修补补,我发现这个命令对我有用(因为我的文件名中有空格和不匹配的引号):

find . -iname "*USA*" -exec cp "{}" /Directory/to/put/file/ \;

find . -iname "*USA*" -exec cp "{}" /Directory/to/put/file/ \;

.refers to the location the search is being run

.指正在运行搜索的位置

-inamefollowed by the expression refers to the match criteria

-iname后跟表达式指的是匹配条件

-exec cp "{}" /Directory/to/put/file/ \;tells the command to execute the copy command where each file found via -inamereplaces "{}"

-exec cp "{}" /Directory/to/put/file/ \;告诉命令执行通过-iname替换找到的每个文件的复制命令"{}"

You need the \;to denote to the exec command that the cpstatement is ending.

您需要\;来表示cp语句即将结束的 exec 命令。