{"id":162455,"date":"2019-11-25T08:00:21","date_gmt":"2019-11-25T00:00:21","guid":{"rendered":"https:\/\/gulass.cn\/?p=162455"},"modified":"2019-10-25T09:46:55","modified_gmt":"2019-10-25T01:46:55","slug":"shell-input-redirection","status":"publish","type":"post","link":"https:\/\/gulass.cn\/shell-input-redirection.html","title":{"rendered":"Shell \u8f93\u5165\u91cd\u5b9a\u5411"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u548c\u8f93\u51fa\u91cd\u5b9a\u5411\u4e00\u6837\uff0cUnix \u547d\u4ee4\u4e5f\u53ef\u4ee5\u4ece\u6587\u4ef6\u83b7\u53d6\u8f93\u5165.<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u548c\u8f93\u51fa\u91cd\u5b9a\u5411\u4e00\u6837\uff0cUnix \u547d\u4ee4\u4e5f\u53ef\u4ee5\u4ece\u6587\u4ef6\u83b7\u53d6\u8f93\u5165\uff0c\u8bed\u6cd5\u4e3a\uff1a<\/p>\n

command1 < file1<\/pre>\n

\u8fd9\u6837\uff0c\u672c\u6765\u9700\u8981\u4ece\u952e\u76d8\u83b7\u53d6\u8f93\u5165\u7684\u547d\u4ee4\u4f1a\u8f6c\u79fb\u5230\u6587\u4ef6\u8bfb\u53d6\u5185\u5bb9\u3002<\/p>\n

\u6ce8\u610f\uff1a\u8f93\u51fa\u91cd\u5b9a\u5411\u662f\u5927\u4e8e\u53f7(>)\uff0c\u8f93\u5165\u91cd\u5b9a\u5411\u662f\u5c0f\u4e8e\u53f7(< )\u3002<\/p>\n

\u5b9e\u4f8b
\n\u63a5\u7740\u4ee5\u4e0a\u5b9e\u4f8b\uff0c\u6211\u4eec\u9700\u8981\u7edf\u8ba1 users \u6587\u4ef6\u7684\u884c\u6570,\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n

$ wc -l users\r\n       2 users<\/pre>\n

\u4e5f\u53ef\u4ee5\u5c06\u8f93\u5165\u91cd\u5b9a\u5411\u5230 users \u6587\u4ef6\uff1a<\/p>\n

$  wc -l < users\r\n       2<\/pre>\n

\u6ce8\u610f\uff1a\u4e0a\u9762\u4e24\u4e2a\u4f8b\u5b50\u7684\u7ed3\u679c\u4e0d\u540c\uff1a\u7b2c\u4e00\u4e2a\u4f8b\u5b50\uff0c\u4f1a\u8f93\u51fa\u6587\u4ef6\u540d\uff1b\u7b2c\u4e8c\u4e2a\u4e0d\u4f1a\uff0c\u56e0\u4e3a\u5b83\u4ec5\u4ec5\u77e5\u9053\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u5185\u5bb9\u3002<\/p>\n

command1 < infile > outfile<\/pre>\n

\u540c\u65f6\u66ff\u6362\u8f93\u5165\u548c\u8f93\u51fa\uff0c\u6267\u884ccommand1\uff0c\u4ece\u6587\u4ef6infile\u8bfb\u53d6\u5185\u5bb9\uff0c\u7136\u540e\u5c06\u8f93\u51fa\u5199\u5165\u5230outfile\u4e2d\u3002<\/p>\n

\u91cd\u5b9a\u5411\u6df1\u5165\u8bb2\u89e3<\/strong>
\n\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2a Unix\/Linux \u547d\u4ee4\u8fd0\u884c\u65f6\u90fd\u4f1a\u6253\u5f00\u4e09\u4e2a\u6587\u4ef6\uff1a<\/p>\n

\u6807\u51c6\u8f93\u5165\u6587\u4ef6(stdin)\uff1astdin\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a0\uff0cUnix\u7a0b\u5e8f\u9ed8\u8ba4\u4ecestdin\u8bfb\u53d6\u6570\u636e\u3002
\n\u6807\u51c6\u8f93\u51fa\u6587\u4ef6(stdout)\uff1astdout \u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a1\uff0cUnix\u7a0b\u5e8f\u9ed8\u8ba4\u5411stdout\u8f93\u51fa\u6570\u636e\u3002
\n\u6807\u51c6\u9519\u8bef\u6587\u4ef6(stderr)\uff1astderr\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a2\uff0cUnix\u7a0b\u5e8f\u4f1a\u5411stderr\u6d41\u4e2d\u5199\u5165\u9519\u8bef\u4fe1\u606f\u3002
\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0ccommand > file \u5c06 stdout \u91cd\u5b9a\u5411\u5230 file\uff0ccommand < file \u5c06stdin \u91cd\u5b9a\u5411\u5230 file\u3002<\/p>\n

\u5982\u679c\u5e0c\u671b stderr \u91cd\u5b9a\u5411\u5230 file\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n

$ command 2 > file<\/pre>\n

\u5982\u679c\u5e0c\u671b stderr \u8ffd\u52a0\u5230 file \u6587\u4ef6\u672b\u5c3e\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n

$ command 2 >> file<\/pre>\n

2 \u8868\u793a\u6807\u51c6\u9519\u8bef\u6587\u4ef6(stderr)\u3002<\/p>\n

\u5982\u679c\u5e0c\u671b\u5c06 stdout \u548c stderr \u5408\u5e76\u540e\u91cd\u5b9a\u5411\u5230 file\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n

$ command > file 2>&1\r\n\r\n\u6216\u8005\r\n\r\n$ command >> file 2>&1<\/pre>\n

\u5982\u679c\u5e0c\u671b\u5bf9 stdin \u548c stdout \u90fd\u91cd\u5b9a\u5411\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n

$ command < file1 >file2<\/pre>\n

command \u547d\u4ee4\u5c06 stdin \u91cd\u5b9a\u5411\u5230 file1\uff0c\u5c06 stdout \u91cd\u5b9a\u5411\u5230 file2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u548c\u8f93\u51fa\u91cd\u5b9a\u5411\u4e00\u6837\uff0cUnix \u547d\u4ee4\u4e5f\u53ef\u4ee5\u4ece\u6587\u4ef6\u83b7\u53d6\u8f93\u5165\uff0c\u8bed\u6cd5\u4e3a\uff1a command1 < file1 \u8fd9\u6837\uff0c […]<\/p>\n","protected":false},"author":1470,"featured_media":162457,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-162455","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/162455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=162455"}],"version-history":[{"count":4,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/162455\/revisions"}],"predecessor-version":[{"id":162516,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/162455\/revisions\/162516"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/162457"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=162455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=162455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=162455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}