{"id":590,"date":"2018-02-02T15:58:17","date_gmt":"2018-02-02T07:58:17","guid":{"rendered":"https:\/\/blog.jsjs.org\/?p=590"},"modified":"2018-02-02T15:58:17","modified_gmt":"2018-02-02T07:58:17","slug":"linux%e8%84%9a%e6%9c%ac%e4%b9%8b-dev-null-21%ef%bc%8c%e4%bb%a5%e5%8f%8a21-vs-21","status":"publish","type":"post","link":"https:\/\/blog.jsjs.org\/?p=590","title":{"rendered":"Linux\u811a\u672c\u4e4b>\/dev\/null 2>&#038;1\uff0c\u4ee5\u53ca2>1 VS 2>&#038;1"},"content":{"rendered":"<p>1. \u6807\u51c6\u8f93\u5165stdin\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a0\uff0c\u6807\u51c6\u8f93\u51fastdout\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a1\uff0c\u6807\u51c6\u9519\u8befstderr\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a2<\/p>\n<p>2. \/dev\/null \u7a7a\u8bbe\u5907\uff0c\u76f8\u5f53\u4e8e\u5783\u573e\u6876<\/p>\n<p>3. \u91cd\u5b9a\u5411\u7b26\u53f7\uff1a&gt;<\/p>\n<p>3. 2&gt;1 \u4e0e 2&gt;&amp;1 \u7684\u533a\u522b<br \/>\n2&gt;1, \u628a\u6807\u51c6\u9519\u8befstderr\u91cd\u5b9a\u5411\u5230\u6587\u4ef61\u4e2d<br \/>\n2&gt;&amp;1,\u628a\u6807\u51c6\u9519\u8befstderr\u91cd\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fastdout<\/p>\n<p>4. \u4e3e\u4f8b:<br \/>\n\u5047\u8bbe\u6709\u811a\u672ctest.sh\uff0c\u5185\u5bb9\u5982\u4e0b\uff0ct\u662f\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u547d\u4ee4\uff0c\u6267\u884c\u811a\u672c\u8fdb\u884c\u4e0b\u9762\u6d4b\u8bd5\u3002<br \/>\n# cat test.sh<br \/>\nt<br \/>\ndate<\/p>\n<p>\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230log\uff0c\u9519\u8bef\u4fe1\u606f\u8f93\u51fa\u5230\u7ec8\u7aef\u4e0a\uff0c\u5982\u4e0b\uff1a<br \/>\n# .\/test.sh &gt; log<br \/>\n.\/test.sh: line 1: t: command not found<br \/>\n# cat log<br \/>\nThu Oct 23 22:53:02 CST 2008<\/p>\n<p>\u5220\u9664log\u6587\u4ef6\uff0c\u91cd\u65b0\u6267\u884c\uff0c\u8fd9\u6b21\u662f\u628a\u6807\u51c6\u8f93\u51fa\u5b9a\u5411\u5230log\uff0c\u9519\u8bef\u4fe1\u606f\u5b9a\u5411\u5230\u6587\u4ef61<br \/>\n# .\/test.sh &gt; log 2&gt;1<br \/>\n#<br \/>\n# cat log<br \/>\nThu Oct 23 22:56:20 CST 2008<br \/>\n# cat 1<br \/>\n.\/test.sh: line 1: t: command not found<br \/>\n#<\/p>\n<p>\u628a\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230log\u6587\u4ef6\uff0c\u628a\u6807\u51c6\u9519\u8bef\u91cd\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fa<br \/>\n# .\/test.sh &gt; log 2&gt;&amp;1<br \/>\n#<br \/>\n# cat log<br \/>\n.\/test.sh: line 1: t: command not found<br \/>\nThu Oct 23 22:58:54 CST 2008<br \/>\n#<\/p>\n<p>\u628a\u9519\u8bef\u4fe1\u606f\u91cd\u5b9a\u5411\u5230\u7a7a\u8bbe\u5907<br \/>\n# .\/test.sh 2&gt;\/dev\/null<br \/>\nThu Oct 23 23:01:07 CST 2008<br \/>\n#<\/p>\n<p>\u628a\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u7a7a\u8bbe\u5907<br \/>\n# .\/test.sh &gt;\/dev\/null<br \/>\n.\/test.sh: line 1: t: command not found<\/p>\n<p>\u628a\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u5168\u91cd\u5b9a\u5411\u5230\u7a7a\u8bbe\u5907<br \/>\n#.\/test.sh &gt;\/dev\/null 2&gt;&amp;1<br \/>\n#<\/p>\n<p>\u628a\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u5168\u91cd\u5b9a\u5411\u5230\u7a7a\u8bbe\u5907<br \/>\n#.\/test.sh &gt;\/dev\/null 2&gt;&amp;1<\/p>\n<div id=\"post_detail\">\n<div class=\"post\">\n<div class=\"postText\">\n<div id=\"cnblogs_post_body\">\n<div id=\"cnblogs_post_body\">\n<p>2&gt;&amp;1\u4f7f\u7528<\/p>\n<p>\u4e00 \u76f8\u5173\u77e5\u8bc6<\/p>\n<p>1\uff09\u9ed8\u8ba4\u5730\uff0c\u6807\u51c6\u7684\u8f93\u5165\u4e3a\u952e\u76d8\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u6765\u81ea\u6587\u4ef6\u6216\u7ba1\u9053\uff08pipe |\uff09\u3002<br \/>\n2\uff09\u9ed8\u8ba4\u5730\uff0c\u6807\u51c6\u7684\u8f93\u51fa\u4e3a\u7ec8\u7aef\uff08terminal)\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\uff0c\u7ba1\u9053\u6216\u540e\u5f15\u53f7\uff08backquotes `\uff09\u3002<br \/>\n3) \u9ed8\u8ba4\u5730\uff0c\u6807\u51c6\u7684\u9519\u8bef\u8f93\u51fa\u5230\u7ec8\u7aef\uff0c\u4f46\u662f\u4e5f\u53ef\u4ee5\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\u3002<br \/>\n4\uff09\u6807\u51c6\u7684\u8f93\u5165\uff0c\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u5206\u522b\u8868\u793a\u4e3aSTDIN,STDOUT,STDERR\uff0c\u4e5f\u53ef\u4ee5\u75280\uff0c1\uff0c2\u6765\u8868\u793a\u3002<br \/>\n5\uff09\u5176\u5b9e\u9664\u4e86\u4ee5\u4e0a\u5e38\u7528\u76843\u4e2d\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u8fd8\u67093~9\u4e5f\u53ef\u4ee5\u4f5c\u4e3a\u6587\u4ef6\u63cf\u8ff0\u7b26\u30023~9\u4f60\u53ef\u4ee5\u8ba4\u4e3a\u662f\u6267\u884c\u67d0\u4e2a\u5730\u65b9\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u5e38\u88ab\u7528\u6765\u4f5c\u4e3a\u4e34\u65f6\u7684\u4e2d\u95f4\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>\u4e8c \u5b9e\u4f8b<\/p>\n<p>1\uff09command 2&gt;errfile : command\u7684\u9519\u8bef\u91cd\u5b9a\u5411\u5230\u6587\u4ef6errfile\u3002<br \/>\n2\uff09command 2&gt;&amp;1 | &#8230;: command\u7684\u9519\u8bef\u91cd\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fa\uff0c\u9519\u8bef\u548c\u6807\u51c6\u8f93\u51fa\u90fd\u901a\u8fc7\u7ba1\u9053\u4f20\u7ed9\u4e0b\u4e2a\u547d\u4ee4\u3002<br \/>\n3\uff09var=`command 2&gt;&amp;1`: command\u7684\u9519\u8bef\u91cd\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fa\uff0c\u9519\u8bef\u548c\u6807\u51c6\u8f93\u51fa\u90fd\u8d4b\u503c\u7ed9var\u3002<br \/>\n4\uff09command 3&gt;&amp;2 2&gt;&amp;1 1&gt;&amp;3 | &#8230;:\u5b9e\u73b0\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u7684\u4ea4\u6362\u3002<br \/>\n5\uff09var=`command 3&gt;&amp;2 2&gt;&amp;1 1&gt;&amp;3`:\u5b9e\u73b0\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u7684\u4ea4\u6362\u3002<br \/>\n6\uff09command 2&gt;&amp;1 1&gt;&amp;2 | &#8230;\u00a0\u00a0\u00a0\u00a0 (wrong&#8230;) :\u8fd9\u4e2a\u4e0d\u80fd\u5b9e\u73b0\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\u8f93\u51fa\u7684\u4ea4\u6362\u3002\u56e0\u4e3ashell\u4ece\u5de6\u5230\u53f3\u6267\u884c\u547d\u4ee4\uff0c\u5f53\u6267\u884c\u5b8c2&gt;&amp;1\u540e\uff0c\u9519\u8bef\u8f93\u51fa\u5df2\u7ecf\u548c\u6807\u51c6\u8f93\u51fa\u4e00\u6837\u7684\uff0c\u518d\u6267\u884c 1&gt;&amp;2\u4e5f\u6ca1\u6709\u610f\u4e49\u3002<\/p>\n<p>\u4e09 &#8220;2&gt;&amp;1 file&#8221;\u548c &#8220;&gt; file 2&gt;&amp;1&#8243;\u533a\u522b<\/p>\n<p>1\uff09cat food 2&gt;&amp;1 &gt;file \uff1a\u9519\u8bef\u8f93\u51fa\u5230\u7ec8\u7aef\uff0c\u6807\u51c6\u8f93\u51fa\u88ab\u91cd\u5b9a\u5411\u5230\u6587\u4ef6file\u3002<br \/>\n2\uff09cat food &gt;file 2&gt;&amp;1 \uff1a\u6807\u51c6\u8f93\u51fa\u88ab\u91cd\u5b9a\u5411\u5230\u6587\u4ef6file\uff0c\u7136\u540e\u9519\u8bef\u8f93\u51fa\u4e5f\u91cd\u5b9a\u5411\u5230\u548c\u6807\u51c6\u8f93\u51fa\u4e00\u6837\uff0c\u6240\u4ee5\u4e5f\u9519\u8bef\u8f93\u51fa\u5230\u6587\u4ef6file\u3002<\/p>\n<p>\u56db \u6ce8\u610f<br \/>\n\u901a \u5e38\u6253\u5f00\u7684\u6587\u4ef6\u5728\u8fdb\u7a0b\u63a8\u51fa\u7684\u65f6\u5019\u81ea\u52a8\u7684\u5173\u95ed\uff0c\u4f46\u662f\u66f4\u597d\u7684\u529e\u6cd5\u662f\u5f53\u4f60\u4f7f\u7528\u5b8c\u4ee5\u540e\u7acb\u5373\u5173\u95ed\u3002\u7528m&lt;&amp;-\u6765\u5173\u95ed\u8f93\u5165\u6587\u4ef6\u63cf\u8ff0\u7b26m\uff0c\u7528 m&gt;&amp;-\u6765\u5173\u95ed\u8f93\u51fa\u6587\u4ef6\u63cf\u8ff0\u7b26m\u3002\u5982\u679c\u4f60\u9700\u8981\u5173\u95ed\u6807\u51c6\u8f93\u5165\u7528&lt;&amp;-; &gt;&amp;- \u88ab\u7528\u6765\u5173\u95ed\u6807\u51c6\u8f93\u51fa\u3002<\/p>\n<p>\u4e94 \u540c\u65f6\u8f93\u51fa\u5230\u7ec8\u7aef\u548c\u6587\u4ef6 copy source dest | tee.exe copyerror.txt<\/p>\n<p>\u516d \u53c2\u8003<\/p>\n<p>1\uff09http:\/\/docstore.mik.ua\/orelly\/unix\/upt\/ch45_21.htm<br \/>\n2\uff09http:\/\/www.unix.com\/shell-programming-scripting\/34011-meaning-dev-null-2-1-a.html<br \/>\n3\uff09http:\/\/docstore.mik.ua\/orelly\/unix\/upt\/ch08_13.htm<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. \u6807\u51c6\u8f93\u5165stdin\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a0\uff0c\u6807\u51c6\u8f93\u51fastdout\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a1\uff0c\u6807\u51c6\u9519\u8befstderr\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a2 2. \/dev\/null \u7a7a\u8bbe\u5907\uff0c\u76f8\u5f53\u4e8e\u5783\u573e\u6876 3. \u91cd\u5b9a\u5411\u7b26\u53f7\uff1a&gt; 3. 2&gt;1 \u4e0e 2&gt;&amp;1 \u7684\u533a\u522b 2&gt;1, \u628a\u6807\u51c6\u9519\u8befstderr\u91cd\u5b9a\u5411\u5230\u6587\u4ef61\u4e2d 2&gt;&amp;1,\u628a\u6807\u51c6\u9519\u8befstderr\u91cd\u5b9a\u5411\u5230\u6807\u51c6\u8f93\u51fastdout 4. \u4e3e\u4f8b: \u5047\u8bbe\u6709\u811a\u672ctest.sh\uff0c\u5185\u5bb9\u5982\u4e0b\uff0ct\u662f\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u547d\u4ee4\uff0c\u6267\u884c\u811a\u672c\u8fdb\u884c\u4e0b\u9762\u6d4b\u8bd5\u3002 # cat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-590","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=590"}],"version-history":[{"count":0,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/590\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}