{"id":2341,"date":"2011-01-28T18:49:47","date_gmt":"2011-01-28T10:49:47","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=2341"},"modified":"2011-02-04T22:15:47","modified_gmt":"2011-02-04T14:15:47","slug":"shell%e7%bc%96%e7%a8%8b%e4%b8%ad%e7%9a%84%e5%88%a4%e6%96%ad%e8%af%ad%e5%8f%a5","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/2341","title":{"rendered":"shell\u7f16\u7a0b\u4e2d\u7684\u5224\u65ad\u8bed\u53e5"},"content":{"rendered":"<p>\u5728shell\u7f16\u7a0b\u4e2d\uff0c\u901a\u8fc7if\u547d\u4ee4\u6765\u5b9e\u73b0\u5224\u65ad\u8bed\u53e5\uff1bif\u547d\u4ee4\u4e2d\u7684\u5224\u65ad\u6761\u4ef6\u5219\u901a\u8fc7test\u547d\u4ee4\u6765\u5b8c\u6210\u3002\u56e0\u6b64\uff0c\u672c\u6587\u5c06\u96c6\u4e2d\u4ecb\u7ecdif\u547d\u4ee4\u548ctest\u547d\u4ee4\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<h4>1. \u9000\u51fa\u72b6\u6001<\/h4>\n<p>\u5bf9\u4e8e\u6bcf\u4e00\u6761\u547d\u4ee4\u800c\u8a00\uff0c\u5b83\u5728\u6267\u884c\u5b8c\u6bd5\u540e\u5c31\u6709\u76f8\u5e94\u7684\u8fd4\u56de\u72b6\u6001\uff0c\u8fd9\u4e2a\u8fd4\u56de\u72b6\u6001\u4fdd\u5b58\u5728\u7279\u6b8a\u53d8\u91cf?\u4e2d\u3002\u5f53\u4e00\u6761\u547d\u4ee4\u6210\u529f\u6267\u884c\u540e\uff0c\u8be5\u547d\u4ee4\u7684\u8fd4\u56de\u72b6\u6001\u4e3a0\uff1b\u5426\u5219\uff0c\u8be5\u547d\u4ee4\u7684\u8fd4\u56de\u72b6\u6001\u4e3a\u975e0\u3002\u6bd4\u5982\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ cat nothisfile\r\ncat: nothisfile: \u6ca1\u6709\u90a3\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\r\nedsionte@edsionte-laptop:~\/shelltest$ echo $?\r\n1\r\nedsionte@edsionte-laptop:~\/shelltest$ echo $?\r\n0<\/pre>\n<p>\u7531\u4e8ecat\u547d\u4ee4\u6267\u884c\u5931\u8d25\uff0c\u901a\u8fc7echo\u547d\u4ee4\u53ef\u83b7\u5f97cat\u547d\u4ee4\u7684\u8fd4\u56de\u503c\u4e3a1\uff1b\u518d\u6b21echo\u7279\u6b8a\u53d8\u91cf\uff1f\u7684\u503c\u53ef\u4ee5\u770b\u5230\u8fd4\u56de\u72b6\u6001\u4e3a0\uff0c\u8fd9\u6b63\u597d\u8bf4\u660e\u4e86\u7b2c\u4e00\u6b21echo\u547d\u4ee4\u6210\u529f\u6267\u884c\u3002<\/p>\n<h4>2.if\u547d\u4ee4<\/h4>\n<p>if\u547d\u4ee4\u7684\u7684\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">if command_t\r\nthen\r\n\tcommands\r\nfi<\/pre>\n<p>command_t\u662f\u7528\u6765\u8fdb\u884c\u903b\u8f91\u5224\u65ad\u7684\u547d\u4ee4\u3002\u5982\u679c\u8be5\u547d\u4ee4\u7684\u7684\u8fd4\u56de\u72b6\u6001\u4e3a0\uff0c\u5219\u6267\u884cthen\u548cfi\u4e4b\u95f4\u7684\u547d\u4ee4\u3002\u9664\u4e86\u4e0a\u8ff0\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u5916\uff0cif\u547d\u4ee4\u8fd8\u6709\u4ee5\u4e0b\u4e24\u79cd\u7ed3\u6784\u3002<\/p>\n<pre class=\"brush:c\">\/\/if-else\r\nif command_t\r\nthen\r\n\tcommands\r\nelse\r\n\tcommands\r\nfi\r\n\/\/if-elif-else\r\nif command_t1\r\nthen\r\n\tcommands\r\nelif command_t2\r\nthen\r\n\tcommands\r\nelse\r\n\tcommands\r\nfi<\/pre>\n<p>\u4e0a\u8ff0\u4e24\u79cd\u7ed3\u6784\u7406\u89e3\u8d77\u6765\u4e0d\u4f1a\u5f88\u96be\uff0c\u5176\u4f7f\u7528\u65b9\u6cd5\u53ef\u53c2\u770btest\u547d\u4ee4\u4e2d\u7684\u4e3e\u4f8b\u3002<\/p>\n<h4>3. test\u547d\u4ee4<\/h4>\n<p>\u6b63\u5982\u672c\u6587\u4e00\u5f00\u59cb\u8bf4\u7684\u90a3\u6837\uff0ctest\u547d\u4ee4\u5e38\u7528\u4f5cif\u547d\u4ee4\u4e2d\u7684\u903b\u8f91\u5224\u65ad\u3002\u5176\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p><strong><em>test expression<\/em><\/strong><\/p>\n<p>\u5176\u4e2dexpression\u8868\u793a\u8981\u6d4b\u8bd5\u7684\u6761\u4ef6\uff0c\u5982\u679c\u8be5\u6761\u4ef6\u4e3a\u771f\uff0c\u5219test\u547d\u4ee4\u7684\u8fd4\u56de\u72b6\u6001\u4e3a0\uff1b\u5426\u5219\u8fd4\u56de\u975e0\u3002<\/p>\n<p><strong>\u5b57\u7b26\u4e32\u64cd\u4f5c\u7b26<\/strong><\/p>\n<p>\u64cd\u4f5c\u7b26=\u7528\u6765\u5224\u65ad\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\uff0c\u5176\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a  <strong><em>test str1 = str2<\/em><\/strong> \u6ce8\u610ftest\u547d\u4ee4\u7684\u4e09\u4e2a\u53c2\u6570\u5fc5\u987b\u7528\u7a7a\u683c\u9694\u5f00\uff0c\u8fd9\u6837test\u624d\u80fd\u6b63\u786e\u7684\u63a5\u6536\u5230\u4e09\u4e2a\u53c2\u6570\u3002\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u8fdb\u4e0a\u6587\u7684lu\u7a0b\u5e8f\uff0c\u589e\u52a0\u53c2\u6570\u5224\u65ad\u529f\u80fd\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ cat lu\r\n# look someone up in the info book\r\nif test \"$1\" = \"\"\r\nthen\r\n\techo \"usage: .\/lu arg\"\r\nelse\r\n\tgrep \"^$1:\" info\r\nfi\r\nedsionte@edsionte-laptop:~\/shelltest$ .\/lu\r\nusage: .\/lu arg<\/pre>\n<p>\u5982\u679c\u672a\u8f93\u5165\u53c2\u6570\uff0c\u5219\u63d0\u793a\u4f7f\u7528\u65b9\u6cd5\u3002\u7531\u4e8e=\u7528\u6765\u5224\u65ad\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\uff0c\u56e0\u6b64\u8be5\u5224\u65ad\u7b26\u5bf9\u4e8e\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c\u5f88\u654f\u611f\u3002\u6bd4\u5982\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ name=\"edsionte \"\r\nedsionte@edsionte-laptop:~\/shelltest$ test \"$name\" = \"edsionte\"\r\nedsionte@edsionte-laptop:~\/shelltest$ echo $?\r\n1<\/pre>\n<p>\u7531\u4e8e\u5bf9name\u8d4b\u503c\u65f6edsionte\u540e\u6709\u4e00\u4e2a\u7a7a\u683c\uff0c\u56e0\u6b64\u8fd9\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e0d\u76f8\u7b49\u3002test\u547d\u4ee4\u7684\u8fd4\u56de\u503c\u4e3a1\u3002  \u5728\u4e0a\u8ff0\u7684test\u547d\u4ee4\u4e2d\uff0c\u53ea\u8981\u6d89\u53ca\u5230\u5bf9\u53d8\u91cf\u7684\u5f15\u7528\u90fd\u4f1a\u52a0\u4e0a\u53cc\u5f15\u53f7\u3002\u901a\u8fc7\u4e0b\u8ff0\u4e3e\u4f8b\u53ef\u4ee5\u770b\u5230\u8fd9\u79cd\u4f7f\u7528\u7684\u597d\u5904\u3002<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ name=\r\nedsionte@edsionte-laptop:~\/shelltest$ test $name = \"edsionte\"\r\nbash: test: =: \u9700\u8981\u5355\u4e2a\u53c2\u6570\r\nedsionte@edsionte-laptop:~\/shelltest$ test \"$name\" = \"edsionte\"\r\nedsionte@edsionte-laptop:~\/shelltest$ echo $?\r\n1<\/pre>\n<p>\u5f53\u7ed9name\u8d4b\u503c\u4e3a\u7a7a\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u4f7f\u7528\u53cc\u5f15\u53f7\u5f15\u7528\u8be5\u53d8\u91cf\uff0c\u5219shell\u5728\u6267\u884ctest\u547d\u4ee4\u65f6\u4f1a\u8ba4\u4e3a\u7f3a\u5c11\u4e86\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570\u3002<\/p>\n<p><strong>\u53e6\u4e00\u79cd\u683c\u5f0f<\/strong><\/p>\n<p>\u7531\u4e8eif\u547d\u4ee4\u5728shell\u7f16\u7a0b\u4e2d\u9891\u7e41\u4f7f\u7528\uff0c\u56e0\u6b64\u4e5f\u8bde\u751f\u4e86\u4e00\u79cd\u7b80\u6613\u7684test\u4f7f\u7528\u683c\u5f0f\uff1a<\/p>\n<p><strong><em>[ expression ]<\/em><\/strong><\/p>\n<p>\u5176\u5b9e\u4e0a\u8ff0\u683c\u5f0f\u4e2d\u7684[\u548c]\u5c31\u7b49\u4ef7\u4e8e\u547d\u4ee4\u540d\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cexpression\u524d\u540e\u9700\u8981\u548c\u76f8\u5e94\u4e2d\u62ec\u53f7\u7528\u7a7a\u683c\u9694\u5f00\u3002\u6bd4\u5982\u4e0a\u8ff0\u4e3e\u4f8b\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ [  \"$name\" = \"edsionte\"  ]\r\nedsionte@edsionte-laptop:~\/shelltest$ echo $?\r\n1<\/pre>\n<p><strong>\u6574\u6570\u64cd\u4f5c\u7b26<\/strong><\/p>\n<p><strong><\/strong> test\u547d\u4ee4\u9664\u4e86\u6709\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u7684\u64cd\u4f5c\u7b26\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u5217\u9488\u5bf9\u6574\u6570\u7684\u64cd\u4f5c\u7b26\u3002\u4e0b\u5217\u7684\u6587\u5b57\u63cf\u8ff0\u53ea\u9488\u5bf9\u8fd4\u56de\u503c\u4e3a0\u65f6\u7684\u60c5\u51b5\uff1a<\/p>\n<p>int1 -eq int2 \uff1aint1\u7b49\u4e8eint2<br \/>\nint1 -ne int2 \uff1aint1\u4e0d\u7b49\u4e8eint2<br \/>\nint1 -ge int2 \uff1aint1\u5927\u4e8e\u7b49\u4e8eint2<br \/>\nint1 -gt int2 \uff1aint1\u5927\u4e8eint2<br \/>\nint1 -le int2 \uff1aint1\u5c0f\u4e8e\u7b49\u4e8eint2<br \/>\nint1 -lt int2 \uff1aint1\u5c0f\u4e8eint2<\/p>\n<p>\u5982\u679c\u5728\u4e0a\u8ff0\u5224\u65ad\u6761\u4ef6\u524d\u52a0\u4e0a\u903b\u8f91\u975e\u7b26\u53f7!\uff0c\u5219\u8868\u793a\u5bf9\u5f53\u524d\u8fd4\u56de\u503c\u53d6\u53cd\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u8be5\u5224\u65ad\u6761\u4ef6\u4e0d\u6ee1\u8db3\u65f6\uff0c\u8fd4\u56de\u503c\u4e3a0\u3002\u6bd4\u5982\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ [ 1 -ge 3 ]\r\nedsionte@edsionte-laptop:~\/shelltest$ echo $?\r\n1\r\nedsionte@edsionte-laptop:~\/shelltest$ [ ! 1 -ge 3 ]\r\nedsionte@edsionte-laptop:~\/shelltest$ echo $?\r\n0<\/pre>\n<p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u8fd9\u4e48\u6539\u8fdb\u4e0a\u6587\u4e2d\u7684lu\u7a0b\u5e8f\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ .\/lu\r\nusage: .\/lu arg\r\nedsionte@edsionte-laptop:~\/shelltest$ cat lu\r\n# look someone up in the info book\r\nif [ $# -ne 1 ]\r\nthen\r\n\techo \"usage: .\/lu arg\"\r\nelse\r\n\tgrep \"^$1:\" info\r\nfi<\/pre>\n<p><strong>\u903b\u8f91\u64cd\u4f5c\u7b26<\/strong><\/p>\n<p>\u4e0a\u8ff0\u6240\u4e3e\u7684\u4f8b\u5b50\u90fd\u662f\u9488\u5bf9\u4e00\u6761\u6d4b\u8bd5\u6761\u4ef6\u7684\uff0c\u5982\u679ctest\u547d\u4ee4\u7684\u6d4b\u8bd5\u6761\u4ef6\u8d85\u8fc7\u4e00\u6761\u65f6\uff0c\u5c31\u5e94\u8be5\u4f7f\u7528\u903b\u8f91\u64cd\u4f5c\u7b26-a\u548c-o\u3002\u524d\u8005\u6807\u793a\u903b\u8f91\u4e0e\uff0c\u800c\u8fd9\u6807\u793a\u903b\u8f91\u6216\u3002\u5176\u4f7f\u7528\u65b9\u6cd5\u53ef\u53c2\u8003\u4e0b\u4f8b\uff1a<\/p>\n<pre class=\"brush:c\">edsionte@edsionte-laptop:~\/shelltest$ [ ! 1 -ge 3 -a 3 -ge 1 ]\r\nedsionte@edsionte-laptop:~\/shelltest$ echo $?\r\n0\r\nedsionte@edsionte-laptop:~\/shelltest$ [ ! 1 -ge 3 -a 3 -le 1 ]\r\nedsionte@edsionte-laptop:~\/shelltest$ echo $?\r\n1\r\nedsionte@edsionte-laptop:~\/shelltest$ [ ! 1 -ge 3 -o 3 -le 1 ]\r\nedsionte@edsionte-laptop:~\/shelltest$ echo $?\r\n0<\/pre>\n<p>\u5173\u4e8etest\u547d\u4ee4\u66f4\u591a\u7684\u4f7f\u7528\u65b9\u6cd5\u53ef\u4ee5\u53c2\u8003man\u624b\u518c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728shell\u7f16\u7a0b\u4e2d\uff0c\u901a\u8fc7if\u547d\u4ee4\u6765\u5b9e\u73b0\u5224\u65ad\u8bed\u53e5\uff1bif\u547d\u4ee4\u4e2d\u7684\u5224\u65ad\u6761\u4ef6\u5219\u901a\u8fc7test\u547d\u4ee4\u6765\u5b8c\u6210\u3002\u56e0\u6b64\uff0c\u672c\u6587\u5c06\u96c6\u4e2d [&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-2341","post","type-post","status-publish","format-standard","hentry","category-shell","tag-shell","tag-linux"],"views":6405,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2341","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=2341"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2341\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=2341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=2341"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=2341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}