﻿{"id":5270,"date":"2021-01-15T10:27:19","date_gmt":"2021-01-15T02:27:19","guid":{"rendered":"https:\/\/www.linuxmi.com\/?p=5270"},"modified":"2021-01-15T11:34:03","modified_gmt":"2021-01-15T03:34:03","slug":"tee-bash","status":"publish","type":"post","link":"https:\/\/www.linuxmi.com\/tee-bash.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u91cd\u5b9a\u5411\u64cd\u4f5c\u7b26\u548ctee\u547d\u4ee4\u5c06\u6587\u672c\u5199\u5165Bash\u4e2d\u7684\u6587\u4ef6"},"content":{"rendered":"<p>\u7f16\u5199Bash\u811a\u672c\u6216\u5728Linux\u547d\u4ee4\u884c\u4e0a\u5de5\u4f5c\u65f6\uff0c\u6700\u5e38\u89c1\u7684\u4efb\u52a1\u4e4b\u4e00\u662f\u8bfb\u5199\u6587\u4ef6\u3002<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528\u91cd\u5b9a\u5411\u64cd\u4f5c\u7b26\u548ctee\u547d\u4ee4\u5c06\u6587\u672c\u5199\u5165Bash\u4e2d\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\u4f7f\u7528\u91cd\u5b9a\u5411\u8fd0\u7b97\u7b26\u5199\u5165\u6587\u4ef6<\/strong><\/p>\n<p>\u5728Bash\u4e2d\uff0c\u8f93\u51fa\u7684\u91cd\u5b9a\u5411\u4f7f\u60a8\u53ef\u4ee5\u6355\u83b7\u547d\u4ee4\u7684\u8f93\u51fa\u5e76\u5c06\u5176\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p>\u91cd\u5b9a\u5411\u5e76\u5c06\u8f93\u51fa\u5199\u5165\u6587\u4ef6\u7684\u4e00\u822c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<p>output &gt; filename<br \/>\noutput &gt;&gt; filename<\/p>\n<p>&gt;\u91cd\u5b9a\u5411\u64cd\u4f5c\u7b26\u5c06\u8f93\u51fa\u5199\u5165\u7ed9\u5b9a\u7684\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5b83\u5c06\u88ab\u622a\u65ad\u4e3a\u96f6\u957f\u5ea6\u3002\u5426\u5219\uff0c\u5c06\u521b\u5efa\u6587\u4ef6\u3002\u4f7f\u7528\u8fd9\u4e2a\u64cd\u4f5c\u7b26\u65f6\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u4f60\u53ef\u80fd\u4f1a\u8986\u76d6\u4e00\u4e2a\u91cd\u8981\u7684\u6587\u4ef6\u3002<\/p>\n<p>&gt;&gt;\u91cd\u5b9a\u5411\u64cd\u4f5c\u7b26\u5c06\u8f93\u51fa\u8ffd\u52a0\u5230\u6307\u5b9a\u7684\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u8be5\u6587\u4ef6\u3002<\/p>\n<p>\u60a8\u9700\u8981\u5bf9\u8be5\u6587\u4ef6\u5177\u6709\u5199\u6743\u9650\u3002\u5426\u5219\uff0c\u60a8\u5c06\u6536\u5230\u4e00\u4e2a\u6743\u9650\u88ab\u62d2\u7edd\u7684\u9519\u8bef\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u663e\u793a\u4e86\u5982\u4f55\u5c06echo\u547d\u4ee4\u7684\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\uff1a<\/p>\n<p>linuxmi@linuxmi:~$ echo &#8220;welcome to www.linuxmi.com&#8221; &gt; linuxmi.txt<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5272 size-full\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/01\/036.png\" alt=\"\" width=\"866\" height=\"297\" \/><\/p>\n<p>\u4e3a\u4e86\u9632\u6b62\u8986\u76d6\u73b0\u6709\u6587\u4ef6\uff0c\u8bf7\u4f7f\u7528set\u5185\u7f6e\u529f\u80fd\u542f\u7528\u201c noclobber\u201d\u9009\u9879\uff1a<\/p>\n<p>linuxmi@linuxmi:~$ set -o noclobber<br \/>\nlinuxmi@linuxmi:~$ echo &#8220;welcome to www.linuxmi.com&#8221; &gt; linuxmi.txt<\/p>\n<p>\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>bash: linuxmi.txt\uff1a\u65e0\u6cd5\u8986\u76d6\u5df2\u5b58\u5728\u7684\u6587\u4ef6<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5271\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/01\/037.png\" alt=\"\" width=\"866\" height=\"210\" \/><\/p>\n<p>&gt;|\u64cd\u4f5c\u7b26\u5141\u8bb8\u4f60\u8986\u76d6Bash\u7684&#8221; noclobber &#8220;\u9009\u9879:<\/p>\n<p>linuxmi@linuxmi:~$ set -o noclobber<\/p>\n<p>linuxmi@linuxmi:~$ echo &#8220;welcome to www.linuxmi.com&#8221; &gt;| linuxmi.txt<\/p>\n<p>&gt;&gt;\u64cd\u4f5c\u7b26\u5c06\u8f93\u51fa\u8ffd\u52a0\u5230\u6587\u4ef6\u7684\u672b\u5c3e\uff0c\u800c\u4e0d\u662f\u8986\u76d6\u6587\u4ef6:<\/p>\n<p>linuxmi@linuxmi:~$ echo &#8220;welcome to www.linuxmi.com&#8221; &gt;&gt; linuxmi.txt<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5273\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/01\/038.png\" alt=\"\" width=\"866\" height=\"252\" \/><\/p>\n<p>\u4f7f\u7528printf\u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u590d\u6742\u7684\u8f93\u51fa:<\/p>\n<p>printf &#8220;Hello, I&#8217;m %s.\\n&#8221; $USER &gt; Linuxmi.com.txt<\/p>\n<p>\u5982\u679c\u8981\u5c06\u591a\u4e2a\u884c\u5199\u5165\u6587\u4ef6\uff0c\u8bf7\u53c2\u8003<a href=\"https:\/\/www.linuxmi.com\/bash-heredoc.html\" target=\"_blank\" rel=\"noopener\">Here document<\/a>\uff08Heredoc\uff09\u91cd\u5b9a\u5411\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5c06\u5185\u5bb9\u4f20\u9012\u7ed9cat\u547d\u4ee4\u5e76\u5c06\u5176\u5199\u5165\u6587\u4ef6\uff1a<\/p>\n<p>cat &lt;&lt; EOF &gt; linuxmi.com.txt<br \/>\n\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\u662f: $PWD<br \/>\n\u60a8\u4ee5\u4ee5\u4e0b\u8eab\u4efd\u767b\u5f55 $(whoami)<br \/>\nEOF<\/p>\n<p>\u8981\u9644\u52a0\u8fd9\u4e9b\u884c\uff0c\u5728\u6587\u4ef6\u540d\u4e4b\u524d\u7528&gt;&gt;\u4fee\u6539&gt;:<\/p>\n<p>cat &lt;&lt; EOF &gt;&gt; linuxmi.txt<br \/>\n\u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55\u662f: $PWD<br \/>\n\u60a8\u4ee5\u4ee5\u4e0b\u8eab\u4efd\u767b\u5f55 $(whoami)<br \/>\nEOF<\/p>\n<p>\u60a8\u53ef\u4ee5\u5c06\u4efb\u4f55\u547d\u4ee4\u7684\u8f93\u51fa\u5199\u5165\u6587\u4ef6\uff1a<\/p>\n<p>linuxmi@linuxmi:~$ date +&#8221;Year: %Y, Month: %m, Day: %d&#8221; &gt; xxvi.txt<\/p>\n<p>date\u547d\u4ee4\u7684\u8f93\u51fa\u5c06\u88ab\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5274\" src=\"https:\/\/www.linuxmi.com\/wp-content\/uploads\/2021\/01\/040.png\" alt=\"\" width=\"882\" height=\"188\" \/><\/p>\n<p><strong>\u4f7f\u7528tee\u547d\u4ee4\u5199\u5165\u6587\u4ef6<\/strong><\/p>\n<p>tee\u547d\u4ee4\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u6570\u636e\uff0c\u5e76\u540c\u65f6\u5411\u6807\u51c6\u8f93\u51fa\u548c\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u5199\u5165\u6570\u636e\u3002<\/p>\n<p>linuxmi@linuxmi:~$ echo &#8220;welcome to www.linuxmi.com&#8221; | tee linuxmi.txt<\/p>\n<p>\u8be5tee\u547d\u4ee4\u7684\u9ed8\u8ba4\u884c\u4e3a\u662f\u8986\u76d6\u6307\u5b9a\u6587\u4ef6\uff0c\u4e0e&gt;\u64cd\u4f5c\u5458\u76f8\u540c\u3002\u8981\u5c06\u8f93\u51fa\u9644\u52a0\u5230\u6587\u4ef6\uff0c\u8bf7\u4f7f\u7528-a\uff08&#8211;append\uff09\u9009\u9879\u8c03\u7528\u547d\u4ee4\uff1a<\/p>\n<p>linuxmi@linuxmi:~$ echo &#8220;welcome to www.linuxmi.com&#8221; | tee -a linuxmi.com.txt<\/p>\n<p>\u5982\u679c\u60a8\u4e0d\u5e0c\u671btee\u5199\u5165\u6807\u51c6\u8f93\u51fa\uff0c\u5219\u53ef\u4ee5\u5c06\u5176\u91cd\u5b9a\u5411\u5230\/dev\/null\uff1a<\/p>\n<p>linuxmi@linuxmi:~$ echo &#8220;welcome to www.linuxmi.com&#8221; | tee www.linuxmi.com.txt &gt;\/dev\/null<\/p>\n<p>\u8981\u5c06\u6587\u672c\u5199\u5230\u591a\u4e2a\u6587\u4ef6\u4e2d\uff0c\u8bf7\u6307\u5b9a\u6587\u4ef6\u4f5c\u4e3atee\u547d\u4ee4\u7684\u53c2\u6570\uff1a<\/p>\n<p>linuxmi@linuxmi:~$ echo &#8220;welcome to www.linuxmi.com&#8221; | tee file_1.txt file_2.txt file_3.txt<\/p>\n<p>\u8be5tee\u547d\u4ee4\u7684\u53e6\u4e00\u4e2a\u4f18\u70b9\u662f\uff0c\u60a8\u53ef\u4ee5\u5c06\u5176\u4e0esudo\u5176\u4ed6\u7528\u6237\u62e5\u6709\u7684\u6587\u4ef6\u7ed3\u5408\u4f7f\u7528\u5e76\u5199\u5165\u5176\u4e2d\u3002\u8981\u5c06\u6587\u672c\u8ffd\u52a0\u5230\u60a8\u6ca1\u6709\u5199\u8bb8\u53ef\u6743\u7684\u6587\u4ef6\u4e2d\uff0c\u8bf7sudo\u5728\u4e4b\u524d\u52a0\u4e0atee\uff1a<\/p>\n<p>linuxmi@linuxmi:~$ echo &#8220;welcome to www.linuxmi.com&#8221; | sudo tee linuxmi.txt<\/p>\n<p>echo\u547d\u4ee4\u8f93\u51fa\u4f5c\u4e3a\u8f93\u5165\u4f20\u9012\u7ed9tee, tee\u5c06\u63d0\u5347sudo\u6743\u9650\u5e76\u5c06\u6587\u672c\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>\u5728Linux\u4e2d\uff0c\u8981\u5c06\u6587\u672c\u5199\u5165\u6587\u4ef6\uff0c\u8bf7\u4f7f\u7528&gt;\u548c&gt;&gt;\u91cd\u5b9a\u5411\u8fd0\u7b97\u7b26\u6216tee\u547d\u4ee4\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6709\u8fd8\u6709\u5176\u4ed6\u7591\u95ee\u6216\u53cd\u9988\uff0c\u8bf7\u5728\u4e0b\u9762\u7684\u8bc4\u8bba\u680f\u91cc\u7ed9\u6211\u4eec\u7559\u8a00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u5199Bash\u811a\u672c\u6216\u5728Linux\u547d\u4ee4\u884c\u4e0a\u5de5\u4f5c\u65f6\uff0c\u6700\u5e38\u89c1\u7684\u4efb\u52a1\u4e4b\u4e00\u662f\u8bfb\u5199\u6587\u4ef6\u3002 \u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528\u91cd\u5b9a\u5411 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[252],"class_list":["post-5270","post","type-post","status-publish","format-standard","hentry","category-linux","tag-tee"],"aioseo_notices":[],"views":628,"_links":{"self":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/5270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/comments?post=5270"}],"version-history":[{"count":3,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/5270\/revisions"}],"predecessor-version":[{"id":5278,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/posts\/5270\/revisions\/5278"}],"wp:attachment":[{"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/media?parent=5270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/categories?post=5270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.linuxmi.com\/wp-json\/wp\/v2\/tags?post=5270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}