{"id":135726,"date":"2019-02-13T08:58:53","date_gmt":"2019-02-13T00:58:53","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=135726"},"modified":"2019-02-11T15:01:43","modified_gmt":"2019-02-11T07:01:43","slug":"shell-xargs-exec","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/shell-xargs-exec.html","title":{"rendered":"shell\u4e4bxargs\u4e0e-exec\u4e0e\u7ba1\u9053\u7684\u533a\u522b\u4f60\u9020\u5417\uff1f"},"content":{"rendered":"
1\u3001xargs\u4f5c\u7528\uff1a<\/strong><\/span><\/div>\n

\uff081\uff09\u5c06\u524d\u4e00\u4e2a\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a\u547d\u4ee4\uff0c\u4f5c\u4e3a\u5b83\u7684\u53c2\u6570\uff0cxargs\u7684\u9ed8\u8ba4\u547d\u4ee4\u662fecho\uff0c\u7a7a\u683c\u662f\u9ed8\u8ba4\u5b9a\u754c\u7b26<\/p>\n

\uff082\uff09\u5c06\u591a\u884c\u8f93\u5165\u8f6c\u6362\u4e3a\u5355\u884c<\/p>\n

2\u3001\u4f7f\u7528\u6a21\u5f0f\uff1a<\/strong><\/span><\/div>\n
front command | xargs -option latercommand<\/pre>\n

front command\uff1a \u524d\u4e00\u4e2a\u547d\u4ee4<\/p>\n

-option\uff1a xargs\u7684\u9009\u9879<\/p>\n

later command\uff1a \u540e\u4e00\u4e2a\u547d\u4ee4<\/p>\n

\u4e00\u822c\u4e0e\u7ba1\u9053\u201c|\u201d\u7ed3\u5408\u4f7f\u7528<\/p>\n

3\u3001xargs\u5e38\u7528\u9009\u9879<\/strong><\/span><\/div>\n

-n\uff1a \u6307\u5b9a\u4e00\u6b21\u5904\u7406\u7684\u53c2\u6570\u4e2a\u6570<\/p>\n

-d\uff1a \u81ea\u5b9a\u4e49\u53c2\u6570\u754c\u5b9a\u7b26<\/p>\n

-p\uff1a \u8be2\u95ee\u662f\u5426\u8fd0\u884c later command \u53c2\u6570<\/p>\n

-t \uff1a \u8868\u793a\u5148\u6253\u5370\u547d\u4ee4\uff0c\u7136\u540e\u518d\u6267\u884c<\/p>\n

-i \uff1a \u9010\u9879\u5904\u7406<\/p>\n

...\u66f4\u591a\u53c2\u6570\u67e5\u770bman xargs<\/p>\n

4\u3001xargs\u4e0e\u7ba1\u9053|\u7684\u533a\u522b<\/strong><\/span><\/div>\n

\u7ba1\u9053\u201c|\u201d \u7528\u6765\u5c06\u524d\u4e00\u4e2a\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u4f20\u9012\u5230\u4e0b\u4e00\u4e2a\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u5165\u3002<\/p>\n

xargs \u5c06\u524d\u4e00\u4e2a\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a\u547d\u4ee4\uff0c\u4f5c\u4e3a\u5b83\u7684\u53c2\u6570\u3002<\/p>\n

\u53ef\u89c1\uff0c\u6807\u51c6\u8f93\u5165\u4e0e\u547d\u4ee4\u53c2\u6570\u662f\u4e0d\u540c\u7684\u3002\u4e2a\u4eba\u7406\u89e3\uff0c\u547d\u4ee4\u53c2\u6570\u5c31\u662f\u76f4\u63a5\u8ddf\u5728\u547d\u4ee4\u540e\u9762\u7684\uff0c\u6807\u51c6\u8f93\u5165\u53ef\u4ee5\u662f\u952e\u76d8\uff0c\u6587\u4ef6\u7b49\u3002<\/p>\n

\u6240\u4ee5\uff0c\u7ba1\u9053\u7b26 | \u6240\u4f20\u9012\u7ed9\u7a0b\u5e8f\u7684\u4e0d\u662f\u7b80\u5355\u5730\u5728\u7a0b\u5e8f\u540d\u540e\u9762\u8f93\u5165\u7684\u53c2\u6570\uff0c\u5b83\u4eec\u4f1a\u88ab\u7a0b\u5e8f\u5185\u90e8\u7684\u8bfb\u53d6\u529f\u80fd\u5982scanf\u548cgets\u7b49\u63a5\u6536\uff0c\u800cxargs\u5219\u662f\u5c06\u5185\u5bb9\u4f5c\u4e3a\u666e\u901a\u7684\u53c2\u6570\u4f20\u9012\u7ed9\u7a0b\u5e8f\uff0c\u76f8\u5f53\u4e8e\u76f4\u63a5\u8ddf\u5728\u547d\u4ee4\u540e\u9762\u3002\u51b5\u4e14\uff0c\u6709\u4e9b\u547d\u4ee4\u662f\u4e0d\u63a5\u53d7\u6807\u51c6\u8f93\u5165\u7684\uff0c\u6bd4\u5982kill\uff0crm\u7b49\u547d\u4ee4\u3002<\/p>\n

\u603b\u7ed3\uff1a\u7ba1\u9053\u7b26\u540e\u4e0d\u52a0xargs\u76f8\u5f53\u4e8e\u5148\u5c06xargs\u540e\u9762\u7684\u547d\u4ee4\u56de\u8f66\u6267\u884c\u4e00\u4e0b\u518d\u4ece\u952e\u76d8\u91cc\u8f93\u5165\u7ba1\u9053\u7b26\u524d\u9762\u547d\u4ee4\u6267\u884c\u7684\u7ed3\u679c\u5185\u5bb9<\/p>\n

\u52a0\u4e0axargs \u76f8\u5f53\u4e8e\u6267\u884c\u547d\u4ee4\uff08xargs\u540e\u9762\u7684\u547d\u4ee4\uff09\u540e\uff0c\u76f4\u63a5\u4ece\u952e\u76d8\u8f93\u5165\u7ba1\u9053\u7b26\u524d\u9762\u547d\u4ee4\u6267\u884c\u7684\u7ed3\u679c\u5185\u5bb9\u518d\u56de\u8f66\uff0c\u5373\u7ba1\u9053\u524d\u9762\u7684\u7ed3\u679c\u76f4\u63a5\u8ddf\u5728xargs \u540e\u9762\u7684\u547d\u4ee4\u540e\u9762<\/p>\n

\u518d\u603b\u7ed3\u4e00\u4e0b\uff0c\u5c31\u662f\u56de\u8f66\u7684\u5148\u540e\u987a\u5e8f\u4e0d\u592a\u4e00\u6837\u3002<\/p>\n

5\u3001xargs\u4e0e-exec\u7684\u533a\u522b<\/strong><\/span><\/div>\n

-exec\uff1a{}\u8868\u793a\u547d\u4ee4\u7684\u53c2\u6570\u5373\u4e3a\u6240\u627e\u5230\u7684\u6587\u4ef6,\u4ee5\uff1b\u8868\u793acomman\u547d\u4ee4\u7684\u7ed3\u675f\u3002\\\u662f\u8f6c\u4e49\u7b26\uff0c
\n\u56e0\u4e3a\u5206\u53f7\u5728\u547d\u4ee4\u4e2d\u8fd8\u6709\u5b83\u7528\u9014\uff0c\u6240\u4ee5\u5c31\u7528\u4e00\u4e2a\\\u6765\u9650\u5b9a\u8868\u793a\u8fd9\u662f\u4e00\u4e2a\u5206\u53f7\u800c\u4e0d\u662f\u8868\u793a\u5176\u5b83\u610f\u601d\u3002<\/p>\n

-ok\uff1a \u548c -exec \u7684\u4f5c\u7528\u76f8\u540c\uff0c\u683c\u5f0f\u4e5f\u4e00\u6837\uff0c\u53ea\u4e0d\u8fc7\u4ee5\u4e00\u79cd\u66f4\u4e3a\u5b89\u5168\u7684\u6a21\u5f0f\u6765\u6267\u884c\u8be5\u53c2\u6570
\n\u6240\u7ed9\u51fa\u7684shell\u7ed9\u51fa\u7684\u8fd9\u4e2a\u547d\u4ee4\u4e4b\u524d\uff0c\u90fd\u4f1a\u7ed9\u51fa\u63d0\u793a\uff0c\u8ba9\u7528\u6237\u6765\u786e\u5b9a\u662f\u5426\u6267\u884c\u3002<\/p>\n

#xargs\u5c06\u53c2\u6570\u4e00\u6b21\u4f20\u7ed9echo\uff0c\u5373\u6267\u884c\uff1aecho begin .\/xargs.txt .\/args.txt<\/p>\n

find . -name '*.txt' -type f | xargs echo begin<\/pre>\n

#exec\u4e00\u6b21\u4f20\u9012\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u6267\u884c\uff1aecho begin .\/xargs.txt;echo begin .\/args.txt<\/p>\n

find . -name '*.txt' -type f -exec echo begin {} \\;<\/pre>\n

xargs \u8981\u7ed3\u5408\u7ba1\u9053\u6765\u5b8c\u6210
\n\u683c\u5f0f\uff1afind [option] express |xargs command
\n\u5f88\u660e\u663e\uff0cexec\u662f\u5bf9\u6bcf\u4e2a\u627e\u5230\u7684\u6587\u4ef6\u6267\u884c\u4e00\u6b21\u547d\u4ee4\uff0c\u9664\u975e\u8fd9\u5355\u4e2a\u7684\u6587\u4ef6\u540d\u8d85\u8fc7\u4e86\u51e0k\uff0c\u5426\u5219\u4e0d
\n\u4f1a\u51fa\u73b0\u547d\u4ee4\u884c\u8d85\u957f\u51fa\u62a5\u9519\u7684\u95ee\u9898\u3002
\n\u800cxargs\u662f\u628a\u6240\u6709\u627e\u5230\u7684\u6587\u4ef6\u540d\u4e00\u80a1\u8111\u7684\u8f6c\u7ed9\u547d\u4ee4\u3002\u5f53\u6587\u4ef6\u5f88\u591a\u65f6\uff0c\u8fd9\u4e9b\u6587\u4ef6\u540d\u7ec4\u5408\u6210\u7684\u547d
\n\u4ee4\u884c\u53c2\u6570\u5f88\u5bb9\u6613\u8d85\u957f\uff0c\u5bfc\u81f4\u547d\u4ee4\u51fa\u9519\u3002<\/p>\n

\u53e6\u5916\uff0c find | xargs \u8fd9\u79cd\u7ec4\u5408\u5728\u5904\u7406\u6709\u7a7a\u683c\u5b57\u7b26\u7684\u6587\u4ef6\u540d\u65f6\u4e5f\u4f1a\u51fa\u9519\uff0c\u56e0\u4e3a\u8fd9\u65f6\u6267\u884c\u7684\u547d\u4ee4
\n\u5df2\u7ecf\u4e0d\u77e5\u9053\u54ea\u4e9b\u662f\u5206\u5272\u7b26\u3001\u54ea\u4e9b\u662f\u6587\u4ef6\u540d\u4e2d\u7684\u7a7a\u683c\uff01 \u800c\u7528exec\u5219\u4e0d\u4f1a\u6709\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n

\u603b\u7ed3<\/strong><\/span><\/div>\n

\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u4e5f\u4e0d\u96be\u770b\u51fa\u5404\u81ea\u7684\u7f3a\u70b9
\n1\u3001exec \u6bcf\u5904\u7406\u4e00\u4e2a\u6587\u4ef6\u6216\u8005\u76ee\u5f55\uff0c\u5b83\u90fd\u9700\u8981\u542f\u52a8\u4e00\u6b21\u547d\u4ee4\uff0c\u6548\u7387\u4e0d\u597d;
\n2\u3001exec \u683c\u5f0f\u9ebb\u70e6\uff0c\u5fc5\u987b\u7528 {} \u505a\u6587\u4ef6\u7684\u4ee3\u4f4d\u7b26\uff0c\u5fc5\u987b\u7528 \\; \u4f5c\u4e3a\u547d\u4ee4\u7684\u7ed3\u675f\u7b26\uff0c\u4e66\u5199\u4e0d\u4fbf\u3002<\/p>\n

3\u3001xargs \u4e0d\u80fd\u64cd\u4f5c\u6587\u4ef6\u540d\u6709\u7a7a\u683c\u7684\u6587\u4ef6\uff1b<\/p>\n

4\u3001exec\u53c2\u6570\u662f\u4e00\u4e2a\u4e00\u4e2a\u4f20\u9012\u7684\uff0c\u4f20\u9012\u4e00\u4e2a\u53c2\u6570\u6267\u884c\u4e00\u6b21\u547d\u4ee4\uff1bxargs\u4e00\u6b21\u5c06\u53c2\u6570\u4f20\u7ed9\u547d\u4ee4\uff0c\u53ef\u4ee5\u4f7f\u7528-n\u63a7\u5236\u53c2\u6570\u4e2a\u6570<\/p>\n

5\u3001exec\u6587\u4ef6\u540d\u6709\u7a7a\u683c\u7b49\u7279\u6b8a\u5b57\u7b26\u4e5f\u80fd\u5904\u7406\uff1bxargs\u4e0d\u80fd\u5904\u7406\u7279\u6b8a\u6587\u4ef6\u540d\uff0c\u5982\u679c\u60f3\u5904\u7406\u7279\u6b8a\u6587\u4ef6\u540d\u9700\u8981\u7279\u6b8a\u5904\u7406<\/p>\n

\u7efc\u4e0a\uff0c\u5982\u679c\u8981\u4f7f\u7528\u7684\u547d\u4ee4\u652f\u6301\u4e00\u6b21\u5904\u7406\u591a\u4e2a\u6587\u4ef6\uff0c\u5e76\u4e14\u4e5f\u77e5\u9053\u8fd9\u4e9b\u6587\u4ef6\u91cc\u6ca1\u6709\u5e26\u7a7a\u683c\u7684\u6587\u4ef6\uff0c
\n\u90a3\u4e48\u4f7f\u7528 xargs\u6bd4\u8f83\u65b9\u4fbf; \u5426\u5219\uff0c\u5c31\u8981\u7528 exec\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\uff081\uff09\u5c06\u524d\u4e00\u4e2a\u547d\u4ee4\u7684\u6807\u51c6\u8f93\u51fa\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a\u547d\u4ee4\uff0c\u4f5c\u4e3a\u5b83\u7684\u53c2\u6570\uff0cxargs\u7684\u9ed8\u8ba4\u547d\u4ee4\u662fecho\uff0c\u7a7a\u683c\u662f\u9ed8\u8ba4\u5b9a\u754c\u7b26 […]<\/p>\n","protected":false},"author":643,"featured_media":135729,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-135726","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/135726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=135726"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/135726\/revisions"}],"predecessor-version":[{"id":135828,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/135726\/revisions\/135828"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/135729"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=135726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=135726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=135726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}