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
Getting error "xargs unterminated quote" when tried to print the number of lines in terminal
提问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 -print0
argument tells find
to use the NULL character to terminate each name that it prints out. The -0
argument tells xargs
that 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 find
is to use the -exec
option 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
.
指正在运行搜索的位置
-iname
followed 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 -iname
replaces "{}"
-exec cp "{}" /Directory/to/put/file/ \;
告诉命令执行通过-iname
替换找到的每个文件的复制命令"{}"
You need the \;
to denote to the exec command that the cp
statement is ending.
您需要\;
来表示cp
语句即将结束的 exec 命令。