{"id":2330,"date":"2011-01-27T21:17:38","date_gmt":"2011-01-27T13:17:38","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=2330"},"modified":"2011-02-04T22:15:38","modified_gmt":"2011-02-04T14:15:38","slug":"shell%e7%bc%96%e7%a8%8b%e4%b8%ad%e7%9a%84%e5%8f%82%e6%95%b0%e4%bc%a0%e9%80%92","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/2330","title":{"rendered":"shell\u7f16\u7a0b\u4e2d\u7684\u53c2\u6570\u4f20\u9012"},"content":{"rendered":"<p>\u5728shell\u7a0b\u5e8f\u4e2d\u5f15\u5165\u53c2\u6570\uff0cshell\u7a0b\u5e8f\u5c31\u4f1a\u7075\u6d3b\u5f88\u591a\u3002\u672c\u6587\u9996\u5148\u7b80\u5355\u4ecb\u7ecd\u53c2\u6570\u4f20\u9012\u4e2d\u7684\u4e00\u4e9b\u57fa\u672c\u77e5\u8bc6\u70b9\uff0c\u518d\u901a\u8fc7\u4e00\u4e2a\u901a\u8baf\u5f55\u7684\u4e3e\u4f8b\u6765\u52a0\u6df1\u5bf9shell\u7f16\u7a0b\u4e2d\u53c2\u6570\u4f20\u9012\u7684\u7406\u89e3\u3002<\/p>\n<h4>1. \u53c2\u6570\u7684\u5f15\u7528<\/h4>\n<p>\u5f53\u8fd0\u884cshell\u7a0b\u5e8f\u65f6\uff0cshell\u4f1a\u81ea\u52a8\u5c06\u547d\u4ee4\u884c\u4e2d\u7b2c\u4e00\u4e2a\u53c2\u6570\u4fdd\u5b58\u5728\u53d8\u91cf1\u4e2d\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4fdd\u5b58\u5728\u53d8\u91cf2\u4e2d\uff0c\u4f9d\u6b21\u7c7b\u63a8\u3002\u56e0\u6b64\uff0c\u5f53\u9700\u8981\u5728shell\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u53c2\u6570\u65f6\uff0c\u4fbf\u53ef\u4ee5\u901a\u8fc7$1\u548c$2\u8fd9\u6837\u7684\u65b9\u6cd5\u6765\u5f15\u7528\u3002\u6bd4\u5982\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ cat myargs\r\necho \"\\\"$1\\\" \\\"$2\\\" was passed\"\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/myargs edsionte wu\r\n\"edsionte\" \"wu\" was passed\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/myargs edsionte\r\n\"edsionte\" \"\" was passed\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/myargs edsionte wu me\r\n\"edsionte\" \"wu\" was passed<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4e3e\u4f8b\u53ef\u4ee5\u770b\u5230\uff0c\u547d\u4ee4\u884c\u4e2d\u7684\u7b2cn\u4e2a\u53c2\u6570\u4f1a\u81ea\u52a8\u4f20\u9012\u7ed9\u76f8\u5e94\u7684\u7279\u6b8a\u53d8\u91cfn\u3002\u901a\u8fc7$n\u5c31\u53ef\u4ee5\u5728shell\u7a0b\u5e8f\u4e2d\u5bf9\u5176\u8fdb\u884c\u5f15\u7528\uff1b\u5f53\u5f15\u7528\u672a\u8d4b\u503c\u7684\u7279\u6b8a\u53d8\u91cf\u65f6\uff0c\u5176\u7ed3\u679c\u5c06\u4f1a\u663e\u793a\u7a7a\u3002<\/p>\n<h4>2. #\u53d8\u91cf<\/h4>\n<p>\u6bcf\u5f53shell\u7a0b\u5e8f\u8fd0\u884c\u65f6\uff0c\u7279\u6b8a\u53d8\u91cf#\u4e2d\u5c31\u4f1a\u4fdd\u5b58\u6b64\u6b21shell\u7a0b\u5e8f\u4e2d\u952e\u5165\u7684\u53c2\u6570\u4e2a\u6570\u3002\u6bd4\u5982\uff1a<\/p>\n<pre class=\"brush:c\">echo \"There are $# arguments passed\"\r\necho \"\\\"$1\\\" \\\"$2\\\" was passed\"\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/myargs edsionte\r\nThere are 1 arguments passed\r\n\"edsionte\" \"\" was passed<\/pre>\n<p>\u547d\u4ee4\u884c\u4e2d\u7684\u53c2\u6570\u662f\u4ee5\u7a7a\u683c\u4f5c\u4e3a\u95f4\u9694\u3002\u56e0\u6b64\uff0c\u4e0b\u9762\u8fd9\u79cd\u7ed3\u679c\u4e5f\u5f88\u5408\u7406\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ .\/myargs \"edsionte wu\"\r\nThere are 1 arguments passed\r\n\"edsionte wu\" \"\" was passed\r\nedsionte@edsionte-laptop:~\/shelltest$ name=\"edsionte wu\"\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/myargs $name\r\nThere are 2 arguments passed\r\n\"edsionte\" \"wu\" was passed<\/pre>\n<h4>3. *\u53d8\u91cf<\/h4>\n<p>\u7279\u6b8a\u53d8\u91cf*\u4e2d\u4f1a\u4fdd\u5b58shell\u7a0b\u5e8f\u8fd0\u884c\u65f6\u7528\u6237\u952e\u5165\u7684\u6240\u6709\u53c2\u6570\u3002\u5728\u53c2\u6570\u4e0d\u786e\u5b9a\u6216\u53c2\u6570\u6570\u76ee\u6613\u53d8\u65f6\uff0c\u8be5\u53d8\u91cf\u5c31\u5f88\u6709\u7528\u3002<\/p>\n<pre class=\"brush:c\">\r\nedsionte@edsionte-laptop:~\/shelltest$ cat myargs \r\necho \"There are $# arguments passed\"\r\necho \"\\\"$*\\\" was passed\"\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/myargs edsionte wu is me\r\nThere are 4 arguments passed\r\n\"edsionte wu is me\" was passed\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/myargs edsionte wu\r\nThere are 2 arguments passed\r\n\"edsionte wu\" was passed<\/pre>\n<h4>4. \u901a\u4fe1\u5f55\u4e3e\u4f8b<\/h4>\n<p><strong>\u67e5\u627e\u7528\u6237<\/strong><\/p>\n<p>\u73b0\u5728\u9700\u8981\u901a\u8fc7\u7528\u6237\u59d3\u540d\u4ece\u901a\u4fe1\u5f55info\u4e2d\u67e5\u627e\u4e00\u4e2a\u7528\u6237\u7684\u4e2a\u4eba\u4fe1\u606f\u3002\u6211\u4eec\u901a\u8fc7\u7f16\u5199shell\u7a0b\u5e8flu\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002\u6700\u7b80\u5355\u7684\u7a0b\u5e8f\u4e3a\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ cat info\r\nedsionte wu:male:xian:18717375182\r\nsen wang:female:hanzhong:15678675545\r\nting li:male:guangzhou:15784859345\r\njedsionte:male:xian:13556567884\r\nedison:male:hongkong:1384742488\r\nyun zhang:female:beijing:18654737473\r\nedsionte@edsionte-laptop:~\/shelltest$ cat lu\r\n# look someone up in the info book\r\ngrep $1 info\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/lu edsionte\r\nedsionte wu:male:xian:18717375182\r\njedsionte:male:xian:13556567884<\/pre>\n<p>\u8fd9\u4e2a\u7ed3\u679c\u4f3c\u4e4e\u5e76\u4e0d\u4ee4\u4eba\u6ee1\u610f\uff0c\u56e0\u4e3a\u6211\u4eec\u60f3\u627e\u7528\u6237\u540d\u4e3aedsionte\u7684\u7528\u6237\u3002\u4e0d\u8fc7\u8fd9\u4e2a\u95ee\u9898\u5f88\u597d\u89e3\u51b3\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u6539\u8fdb\u7a0b\u5e8f\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ cat lu\r\n# look someone up in the info book\r\ngrep ^$1 info\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/lu edsionte\r\nedsionte wu:male:xian:18717375182<\/pre>\n<p>\u5982\u679c\u6211\u4eec\u60f3\u901a\u8fc7\u5168\u540dedsionte  wu\u6765\u67e5\u627e\u7528\u6237\u4fe1\u606f\uff0c\u5c31\u5e94\u8be5\u7528\u53cc\u5f15\u53f7\u5c06\u5168\u540d\u4f5c\u4e3a\u4e00\u4e2a\u6574\u4f53\u5f15\u5165\u5176\u4e2d\u3002\u8fd9\u6837\u770b\u4f3c\u5f88\u5b8c\u7f8e\uff0c\u53ef\u662f\u7ed3\u679c\u5374\u4e0d\u5c3d\u4eba\u610f\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ .\/lu \"edsionte wu\"\r\ngrep: wu: \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\r\ninfo:edsionte wu:male:xian:18717375182<\/pre>\n<p>\u8fd9\u662f\u56e0\u4e3a\u867d\u7136edsionte  wu\u4f5c\u4e3a\u4e00\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9\u7279\u6b8a\u53d8\u91cf1\uff0c\u4f46\u662fshell\u5c06\u5176\u66ff\u6362\u540e\uff0cgrep\u547d\u4ee4\u5e76\u4e0d\u8ba4\u4e3aedsionte  wu\u662f\u4e00\u4e2a\u53c2\u6570\uff0c\u800c\u662f\u5c06wu\u4f5c\u4e3a\u76ee\u6807\u6587\u4ef6\u4e4b\u4e00\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u5e94\u8be5\u8fd9\u6837\u6539\u8fdb\u7a0b\u5e8f\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ cat lu\r\n# look someone up in the info book\r\ngrep \"^$1\" info\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/lu \"edsionte wu\"\r\nedsionte wu:male:xian:18717375182<\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u6ee1\u8db3\u6211\u4eec\u8981\u6c42\u4e86\u3002\u4f46\u662f\uff0c\u5982\u679cinfo\u4e2d\u7684\u4fe1\u606f\u5982\u4e0b\u6240\u793a\uff0c\u90a3\u4e48\u4e0a\u8ff0lu\u7a0b\u5e8f\u8fd8\u662f\u4e0d\u7b97\u5b8c\u7f8e\u3002\u6bd4\u5982\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ cat info\r\nedsionte wu:male:xian:18717375182\r\nsen wang:female:hanzhong:15678675545\r\nedsionte wuee:male:xian:18717375182\r\nting li:male:guangzhou:15784859345\r\njedsionte:male:xian:13556567884\r\nedison:male:hongkong:1384742488\r\nyun zhang:female:beijing:18654737473\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/lu \"edsionte wu\"\r\nedsionte wu:male:xian:18717375182\r\nedsionte wuee:male:xian:18717375182<\/pre>\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u4e5f\u5e94\u8be5\u5728\u53c2\u6570\u7684\u5c3e\u90e8\u4e5f\u505a\u4e00\u4e9b\u201c\u624b\u811a\u201d\uff0c\u4f7f\u5f97grep\u547d\u4ee4\u53ea\u5339\u914d\u6211\u4eec\u6240\u4f20\u9012\u7684\u53c2\u6570\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ cat lu\r\n# look someone up in the info book\r\ngrep \"^$1:\" info\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/lu \"edsionte wu\"\r\nedsionte wu:male:xian:18717375182<\/pre>\n<p>\u81f3\u6b64\uff0c\u67e5\u627e\u7a0b\u5e8f\u5b8c\u6210\u3002<\/p>\n<p><strong>\u6dfb\u52a0\u7528\u6237<\/strong><\/p>\n<p>\u5f53\u5411\u901a\u8baf\u5f55\u4e2d\u589e\u6dfb\u65b0\u7528\u6237\u4fe1\u606f\u65f6\uff0c\u6211\u4eec\u5fc5\u987b\u518d\u7f16\u5199\u4e00\u4e2aadd\u7a0b\u5e8f\u3002\u589e\u52a0\u7528\u6237\u7a0b\u5e8f\u5f88\u7b80\u5355\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ cat add\r\n# add someone to the info book\r\necho \"$1:$2:$3:$4\" &gt;&gt; info\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/add elva female taibei 1335676567\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/lu elva\r\nelva:female:taibei:1335676567<\/pre>\n<p>&gt;&gt;\u4f7f\u5f97\u65b0\u7684\u4fe1\u606f\u4f1a\u8ffd\u52a0\u5230\u539f\u6587\u4ef6\u7684\u672b\u5c3e\u3002\u5728\u4e0a\u8ff0\u547d\u4ee4\u7684\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u518d\u589e\u52a0\u6309\u59d3\u540d\u6392\u5e8f\u7684\u529f\u80fd\uff1a<\/p>\n<pre class=\"brush:c\">\r\nedsionte@edsionte-laptop:~\/shelltest$ cat add\r\n# add someone to the info book\r\necho \"$1:$2:$3:$4\" &gt;&gt; info\r\nsort -o info info\r\nedsionte@edsionte-laptop:~\/shelltest$ cat info\r\nedison:male:hongkong:1384742488\r\nedsionte wuee:male:xian:18717375182\r\nedsionte wu:male:xian:18717375182\r\nelva:female:taibei:1335676567\r\njedsionte:male:xian:13556567884\r\nsen wang:female:hanzhong:15678675545\r\nting li:male:guangzhou:15784859345\r\nyun zhang:female:beijing:18654737473\r\n<\/pre>\n<p>\u6dfb\u52a0\u7a0b\u5e8f\u7ed3\u675f\u3002<br \/>\n<strong><\/strong><\/p>\n<p><strong>\u5220\u9664\u7528\u6237<\/strong><\/p>\n<p>\u6709\u4e86\u4e0a\u8ff0\u4e24\u4e2a\u7a0b\u5e8f\u7684\u57fa\u7840\uff0c\u5220\u9664\u7528\u6237\u7a0b\u5e8f\u5219\u53d8\u5f97\u5bb9\u6613\u5f88\u591a\u3002\u9996\u5148\u901a\u8fc7sed\u547d\u4ee4\u5c06\u5b8c\u5168\u5339\u914d\u7ed9\u5b9a\u4eba\u540d\u7684\u7528\u6237\u4fe1\u606f\u4eceinfo\u4e2d\u5220\u9664\uff1b\u56e0\u4e3ased\u547d\u4ee4\u5e76\u4e0d\u6539\u53d8info\u6587\u4ef6\u672c\u8eab\u7684\u5185\u5bb9\uff0c\u56e0\u6b64\u9700\u8981\u5c06sed\u7684\u8f93\u51fa\u7ed3\u679c\u6682\u5b58\u81f3\u7f13\u51b2\u533a\uff1b\u6700\u540e\u901a\u8fc7mv\u547d\u4ee4\u66ff\u6362\u6587\u4ef6\u540d\u5373\u53ef\u3002<\/p>\n<pre class=\"brush:c\">\r\nedsionte@edsionte-laptop:~\/shelltest$ cat del\r\n# delete the given user from info\r\nsed \"\/^$1:\/d\" info &gt; \/tmp\/info\r\nmv \/tmp\/info .\/info\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/del elva\r\nedsionte@edsionte-laptop:~\/shelltest$ cat info\r\nedison:male:hongkong:1384742488\r\nedsionte wuee:male:xian:18717375182\r\nedsionte wu:male:xian:18717375182\r\njedsionte:male:xian:13556567884\r\nsen wang:female:hanzhong:15678675545\r\nting li:male:guangzhou:15784859345\r\nyun zhang:female:beijing:18654737473\r\n<\/pre>\n<p>\u968f\u7740\u5bf9shell\u7f16\u7a0b\u7684\u6df1\u5165\u7406\u89e3\uff0c\u4e0a\u8ff0\u901a\u8baf\u5f55\u7a0b\u5e8f\u8fd8\u53ef\u4ee5\u4e0d\u65ad\u5b8c\u5584\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728shell\u7a0b\u5e8f\u4e2d\u5f15\u5165\u53c2\u6570\uff0cshell\u7a0b\u5e8f\u5c31\u4f1a\u7075\u6d3b\u5f88\u591a\u3002\u672c\u6587\u9996\u5148\u7b80\u5355\u4ecb\u7ecd\u53c2\u6570\u4f20\u9012\u4e2d\u7684\u4e00\u4e9b\u57fa\u672c\u77e5\u8bc6\u70b9\uff0c\u518d\u901a\u8fc7\u4e00\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[155,125],"class_list":["post-2330","post","type-post","status-publish","format-standard","hentry","category-shell","tag-shell","tag-linux"],"views":6848,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=2330"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2330\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=2330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=2330"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=2330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}