{"id":774,"date":"2009-09-17T15:44:28","date_gmt":"2009-09-17T07:44:28","guid":{"rendered":"http:\/\/orczhou.com\/?p=774"},"modified":"2025-01-21T10:40:01","modified_gmt":"2025-01-21T02:40:01","slug":"linux-shell-script","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2009\/09\/linux-shell-script\/","title":{"rendered":"\u8fe5\u5f02\u7684Linux Shell Script"},"content":{"rendered":"\n\n\n\n<h4 class=\"wp-block-heading\">\u53d1\u5e03\u8bf4\u660e<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>2025\u5e7401\u6708\uff0c\u65b0\u589e\u8865\u5145\u4e00\u4e9b\u81ea\u5df1\u79ef\u7d2f\u7684\u6709\u7528\u7684 Linux \u7684\u547d\u4ee4<\/li>\n\n\n\n<li>2024\u5e7409\u6708\uff0c\u66f4\u65b0\u8bf4\u660e\u4e3a\u4ec0\u4e48Linux Shell\uff08\u51c6\u786e\u7684\u8bf4\uff0c\u5e94\u8be5\u662fShell\u6216Unix Shell\uff09\u4f1a\u663e\u5f97\u201c\u8fe5\u5f02\u201d<\/li>\n\n\n\n<li>2009\u5e74\uff0c\u521d\u6b21\u53d1\u5e03<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>\u6458\u8981\uff1a<\/strong>\u5982\u679c\u4f60\u5df2\u7ecf\u4e60\u60ef\u7f16\u5199Shell\u811a\u672c\uff0c\u90a3\u4e48\u4e00\u5207\u90fd\u663e\u5f97\u5f88\u81ea\u7136\u4e86\u3002\u4f46\u50cf\u6211\u8fd9\u6837\uff0c\u4e4b\u524d\u5df2\u7ecf\u4e60\u60ef\u4e86C\u8bed\u8a00(\u98ce\u683c)\u7684\u7f16\u7a0b\uff0c\u5c31\u89c9\u5f97Shell\u5e76\u4e0d\u662f\u90a3\u4e48\u53cb\u597d\u3002\u8fd9\u91cc\u5c06\u8bb0\u5f55\u4e00\u4e9bShell\u548c&#8221;C\u98ce\u683c&#8221;<strong>\u8fe5\u5f02<\/strong>\u7684\u5730\u65b9\uff0c\u4ee5\u4f9b\u53c2\u8003\u3002\u672c\u6587\u6d4b\u8bd5\u73af\u5883\uff1aGNU bash, version 3.00.15(1)-release (i386-redhat-linux-gnu)\u3002\u6587\u7ae0\u4e2d\u5f88\u591a\u5b9e\u4f8b\u76f4\u63a5\u6765\u5728\u53c2\u8003\u6587\u4ef6[1]\u3001[2]\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"http:\/\/www.flickr.com\/photos\/26825745@N06\/3930881408\/\"><img decoding=\"async\" src=\"http:\/\/farm3.static.flickr.com\/2622\/3930881408_5033a56de1_o.png\" alt=\"Linux\"\/><\/a><\/figure>\n\n\n\n<!--more-->\n\n\n\n<h4 class=\"wp-block-heading\"><span style=\"font-size: medium;\">1\u3001\u53d8\u91cf\u5b9a\u4e49\u7b49\u53f7\u4e24\u8fb9\u4e0d\u80fd\u6709\u7a7a\u683c<\/span><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">#!\/bin\/bash\nmyvar=3 #\u6b63\u786e\n<del datetime=\"2009-09-16T08:58:09+00:00\">myvar = 3;<\/del> #\u7b49\u53f7\u8fb9\u591a\u4e86\u7a7a\u683c\uff0c\u662f\u9519\u8bef\u7684\uff01\u5f88<strong>\u8fe5\u5f02<\/strong>\u5427<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><span style=\"font-size: medium;\">1.1\u3001\u8fd8\u662f\u7a7a\u683c\uff0c\u8fd9\u6b21\u662f\u4e0d\u80fd\u6ca1\u6709<\/span><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">#!\/bin\/sh\nmyVar=\"OFF\"\nif [   $myVar = 'OFF'    ];then  \n#\u8fd9\u91cc[]\u4e2d\u62ec\u8d77\u6765\u7684\u5185\u5bb9<strong>\u4e24\u7aef\u5fc5\u987b\u6709<\/strong>\u7a7a\u683c\uff0c<del datetime=\"2009-09-17T07:30:53+00:00\">if [$myVar = 'OFF']<\/del>\u662f\u4e0d\u80fd\u6b63\u5e38\u5de5\u4f5c\u7684\u3002\u4e5f\u5f88<strong>\u8fe5\u5f02<\/strong>\u5427\n#\u6ce8\u610f if \u548c [ \u4e4b\u95f4\u4e5f\u662f\u6709\u7a7a\u683c\u7684\uff01\n        echo \"works\"\nelse\n        echo \"Not works\"\nfi<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><span style=\"font-size: medium;\">2\u3001\u53cc\u5f15\u53f7\u548c\u5355\u5f15\u53f7<\/span><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">testvar=5\nmyvar='Haha$test' #shell<strong>\u4e0d<\/strong>\u4f1a\u89e3\u91ca$test\nmyvar2=\"Haha$test\" #shell\u4f1a\u89e3\u91ca$test\necho $myvar $myvar2 #\u8f93\u51fa\uff1aHaha$test Haha5<\/code><\/pre>\n\n\n\n<p>\u53cc\u5f15\u53f7\u4e2d\u7684\u5b57\u7b26\u5982\u679c\u6709\u53d8\u91cf\uff0cshell\u4f1a\u5c1d\u8bd5\u89e3\u91ca\u5b83\uff0c\u5355\u5f15\u53f7\u4e2d\u4e0d\u4f1a\u3002\u6240\u4ee5\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u8981\u89e3\u91ca\u7684\u53d8\u91cf\u5c3d\u91cf\u4f7f\u7528\u5355\u5f15\u53f7\uff0c\u636e\u8bf4\u901f\u5ea6\u4f1a\u5feb\u4e9b\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u5230\u4e0d\u7b97\u5f88\u8fe5\u5f02\uff0c\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u7684\u8bed\u8a00\u4e5f\u90fd\u6709\u8fd9\u6837\u7684\u7ea6\u5b9a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span style=\"font-size: medium;\">3\u3001\u5947\u602a\u7684\u7b97\u672f\u8fd0\u7b97<\/span><\/h4>\n\n\n\n<p>shell\u4e2d\u7b97\u672f\u8fd0\u7b97\u9700\u8981\u4f7f\u7528$((\u548c))\u5c06\u7b97\u672f\u8fd0\u7b97\u62ec\u8d77\u6765<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">$(( $myvar + 12 )) #\u8fd9\u4e2a\u975e\u5e38<strong>\u8fe5\u5f02<\/strong><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><span style=\"font-size: medium;\">4\u3001case\u8bed\u53e5<\/span><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">case \"${x##*.}\" in\n      gz)\n            gzunpack ${SROOT}\/${x}\n            ;;\n      bz2)\n            bz2unpack ${SROOT}\/${x}\n            ;;\n      *)\n            echo \"Archive format not recognized.\"\n            exit\n            ;;\nesac           #\u8fd9\u4e2a\u6bd4\u8f83<strong>\u8fe5\u5f02<\/strong> \"esac\"\u3001\";;\"\u3001\"bz2)\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><span style=\"font-size: medium;\">5\u3001\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u4f5c\u7528\u8303\u56f4<\/span><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\">#!\/usr\/bin\/env bash \nmyvar=\"hello\" \nmyfunc() {\n     myvar=\"one two three\"\n     for x in $myvar\n     do\n         echo $x\n     done\n} \nmyfunc \necho $myvar $x\n\u8f93\u51fa\uff1a\none\ntwo\nthree\none two three three  #\u51fd\u6570myfunc\u4e2d\u7684\u53d8\u91cf\uff0c\u5728\u51fd\u6570\u4e4b\u5916\u4ecd\u7136\u5b58\u5728\u3002\n#\u4f60\u53ef\u4ee5\u901a\u8fc7\u5173\u952e\u5b57 local \u9650\u5236\u53d8\u91cf\u53ea\u5728\u51fd\u6570\u4e2d\u6709\u6548\u679c #\u8fd9\u4e2a\u6bd4\u8f83<strong>\u8fe5\u5f02<\/strong>\n<\/code><\/pre>\n\n\n\n<p>\u8fd8\u6709\u66f4\u8fe5\u5f02\u7684\u5417\uff1f<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48Linux Shell\u4f1a\u663e\u5f97\u8fe5\u5f02<\/h4>\n\n\n\n<p>\u66f4\u65b0\u4e8e202409\u6708\uff1a\u8fd9\u7bc7\u6587\u7ae0\u6700\u521d\u5199\u4e8e2009\u5e74\uff0c\u56e0\u4e3a\u5de5\u4f5c\u4e2d\u9700\u8981\u975e\u5e38\u591a\u7684\u4f7f\u7528Linux Shell\uff08\u5176\u5b9e\u662fBash\uff09\uff0c\u5f53\u65f6\u521a\u521a\u5f00\u59cb\u8f83\u4e3a\u6df1\u5165\u7684\u4f7f\u7528\u4e00\u4e9bBash\u7684\u7279\u6027\u3002\u5982\u679c\uff0c\u4f60\u548c\u6211\u4e00\u6837\uff0c\u5728\u6df1\u5165Linux Shell\u4e4b\u524d\uff0c\u66fe\u5b66\u8fc7\u4e00\u4e9b\u9ad8\u7ea7\u8bed\u8a00\uff0c\u90a3\u4e48\u518d\u6765\u4f7f\u7528Shell\u662f\u5f88\u5bb9\u6613\u89c9\u5f97Shell\u5f88\u8fe5\u5f02\u7684\uff0c\u73b0\u5728\u60f3\u60f3\uff0c\u539f\u56e0\u5927\u6982\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Shell\uff08\u6216\u8005Bash\uff09\u662f\u7528\u6237\u4e0eLinux\u5185\u6838\u76f4\u63a5\u6253\u4ea4\u9053\u7684\u4e00\u4e2a\u201c\u63a5\u53e3\u201d\uff0c\u6240\u4ee5\u5728\u4f7f\u7528\u4e0a\uff0c\u5b83\u4e0e\u5f88\u591aLinux\u5e95\u5c42\u7279\u6027\u6709\u5173\uff0c\u4f8b\u5982\u5f88\u591a\u6982\u5ff5\u4e0e<code>Posix<\/code>\u6807\u51c6\u7c7b\u4f3c\uff0c\u4f8b\u5982\u6807\u51c6\u8f93\u51fa\u3001\u6807\u51c6\u8f93\u5165\u7b49\u3002<\/li>\n\n\n\n<li>Shell\u662f\u4e00\u4e2a\u6bd4\u8f83\u201c\u5e95\u5c42\u201d\u7684\u5de5\u5177\uff0c\u4e0e\u5404\u79cd\u201c\u9ad8\u7ea7\u8bed\u8a00\u201d\u4e0d\u540c\uff0c\u5404\u79cd\u201c\u9ad8\u7ea7\u8bed\u8a00\u201d\u901a\u5e38\u4f1a\u5728\u53d1\u5c55\u4e2d\uff0c\u5c1d\u8bd5\u517c\u5bb9\u4e00\u4e9b\u8bed\u8a00\u4e0a\u7684\u201c\u81ea\u7136\u201d\u8868\u73b0\uff0c\u6240\u4ee5\uff0c\u201c\u9ad8\u7ea7\u8bed\u8a00\u201d\u4f1a\u66f4\u81ea\u7136\u4e00\u4e9b<\/li>\n\n\n\n<li>Shell\u53d1\u5c55\u975e\u5e38\u65e9\uff0c\u6211\u8fd9\u7bc7\u6587\u7ae0\u6807\u9898\u662f\u201cLinux Shell\u201d\uff0c\u800c\u4e8b\u5b9e\u4e0a\uff0cShell\u662f\u8fdc\u8fdc\u65e9\u4e8eLinux\u7684\u3002\u6700\u65e9\u7684<a href=\"https:\/\/en.wikipedia.org\/wiki\/Thompson_shell\">Thompson shell<\/a>\u53d1\u5e03\u4e0e1971\u5e74\uff0c\u5373\u4fbf\u662f\u73b0\u5728\u6700\u4e3a\u5e7f\u6cdb\u4f7f\u7528<a href=\"https:\/\/en.wikipedia.org\/wiki\/Bash_(Unix_shell)\">Bash\u4e5f\u662f\u6700\u65e9\u53d1\u5e03\u4e8e1989\u5e74<\/a>\uff0c\u800cLinux\u5219\u662f\u4e8e1991\u5e74\u53d1\u5e03\u3002\u4e3a\u4e86\u4fdd\u6301\u5411\u540e\u7684\u517c\u5bb9\u6027\uff0c\u6240\u4ee5\u8d8a\u65e9\u53d1\u5e03\u7684\u8bed\u8a00\uff0c\u5219\u4f1a\u4fdd\u6301\u8d8a\u591a\u201c\u5386\u53f2\u79ef\u7d2f\u201d\u7684\u7279\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u81ea\u7528\u3001\u6709\u7528\u7684 Linux \u547d\u4ee4<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u67e5\u770b\u78c1\u76d8\u7a7a\u95f4\u4f7f\u7528<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">du -hs *|sort -h -k 1<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u53c2\u6570<code>-h<\/code>\u8868\u793a human readable\uff0c\u4f8b\u5982\uff0c\u6587\u4ef6\/\u76ee\u5f55\u5927\u5c0f\u81ea\u52a8\u5316\u7684\u7528k\u3001M\u3001G\u4f5c\u4e3a\u5355\u4f4d<\/p>\n\n\n\n<p>sort \u547d\u4ee4\u4e2d\u7684 <code>-k<\/code>\u8868\u793a\u6309\u7167\u54ea\u4e00\u5217\u8fdb\u884c\u6392\u5e8f\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u7684\u67e5\u770b\u5f53\u524d\u76ee\u5f55\u54ea\u4e2a\u76ee\u5f55\u6700\u5927<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u53c2\u8003\u6587\u732e<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>http:\/\/www.ibm.com\/developerworks\/cn\/linux\/shell\/bash\/bash-1\/index.html<\/li>\n\n\n\n<li>http:\/\/www.ibm.com\/developerworks\/cn\/linux\/shell\/bash\/bash-2\/index.html<\/li>\n\n\n\n<li>http:\/\/www.ibm.com\/developerworks\/cn\/linux\/shell\/bash\/bash-3\/index.html<\/li>\n\n\n\n<li>http:\/\/www.linuxsir.org\/main\/?q=node\/135<\/li>\n\n\n\n<li>BASH Programming \u2212 Introduction HOW\u2212TO<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u53d1\u5e03\u8bf4\u660e \u6458\u8981\uff1a\u5982\u679c\u4f60\u5df2\u7ecf\u4e60\u60ef\u7f16\u5199Shell\u811a\u672c\uff0c\u90a3\u4e48\u4e00\u5207\u90fd\u663e\u5f97\u5f88\u81ea\u7136\u4e86\u3002\u4f46\u50cf\u6211\u8fd9\u6837\uff0c\u4e4b\u524d\u5df2\u7ecf\u4e60\u60ef\u4e86C\u8bed\u8a00(\u98ce\u683c)\u7684\u7f16\u7a0b\uff0c\u5c31\u89c9\u5f97Shell\u5e76\u4e0d\u662f\u90a3\u4e48\u53cb\u597d\u3002\u8fd9\u91cc\u5c06\u8bb0\u5f55\u4e00\u4e9bShell\u548c&#8221;C\u98ce\u683c&#8221;\u8fe5\u5f02\u7684\u5730\u65b9\uff0c\u4ee5\u4f9b\u53c2\u8003\u3002\u672c\u6587\u6d4b\u8bd5\u73af\u5883\uff1aGNU bash, version 3.00.15(1)-release (i386-redhat-linux-gnu)\u3002\u6587\u7ae0\u4e2d\u5f88\u591a\u5b9e\u4f8b\u76f4\u63a5\u6765\u5728\u53c2\u8003\u6587\u4ef6[1]\u3001[2]\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[27,4],"tags":[121,25],"class_list":["post-774","post","type-post","status-publish","format-standard","hentry","category-linux","category-code-detail","tag-linux","tag-shell"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/774","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=774"}],"version-history":[{"count":46,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/774\/revisions"}],"predecessor-version":[{"id":17233,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/774\/revisions\/17233"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}