{"id":3127,"date":"2012-06-09T11:01:22","date_gmt":"2012-06-09T03:01:22","guid":{"rendered":"http:\/\/jpuyy.com\/?p=3127"},"modified":"2013-03-30T00:04:06","modified_gmt":"2013-03-29T16:04:06","slug":"shell-scripts-examples","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=3127","title":{"rendered":"shell\u57fa\u7840\u77e5\u8bc6"},"content":{"rendered":"<p><a href=\"#environment\" id=\"sph_id4\">\u73af\u5883\u53d8\u91cf<\/a><br \/>\n<a href=\"#cut_string\">\u622a\u65ad\u5b57\u7b26\u4e32<\/a><\/p>\n<p><a id=\"environment\" href=\"#sph_id4\">\u73af\u5883\u53d8\u91cf<\/a><br \/>\n\u5728 bash \u4e2d\u5b9a\u4e49\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<pre>myvar='This is my environment variable!'<\/pre>\n<p>\u4ee5\u4e0a\u547d\u4ee4\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a &#8220;myvar&#8221; \u7684\u73af\u5883\u53d8\u91cf\uff0c\u5e76\u5305\u542b\u5b57\u7b26\u4e32 &#8220;This is my environment variable!&#8221;\u3002<\/p>\n<p>\u6709\u51e0\u70b9\u6ce8\u610f\uff1a<\/p>\n<p>1.\u5728\u7b49\u53f7 &#8220;=&#8221; \u7684\u4e24\u8fb9\u6ca1\u6709\u7a7a\u683c\uff1b<\/p>\n<p>2.\u867d\u7136\u5728\u5b9a\u4e49\u4e00\u4e2a\u5b57\u65f6\u80fd\u591f\u7701\u7565\u5f15\u53f7\uff0c\u4f46\u662f\u5f53\u5b9a\u4e49\u7684\u73af\u5883\u53d8\u91cf\u503c\u591a\u4e8e\u4e00\u4e2a\u5b57\u65f6\uff08\u5305\u542b\u7a7a\u683c\u6216tab\uff09\uff0c\u5f15\u53f7\u662f\u5fc5\u987b\u7684\uff1b<\/p>\n<p>3.\u867d\u7136\u901a\u5e38\u80fd\u591f\u7528\u53cc\u5f15\u53f7\u6765\u66ff\u4ee3\u5355\u5f15\u53f7\uff0c\u4f46\u5728\u4e0a\u4f8b\u4e2d\uff0c\u8fd9\u6837\u505a\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u56e0\u4e3a\u4f7f\u7528\u5355\u5f15\u53f7\u7981\u7528\u4e86\u79f0\u4e3a\u6269\u5c55\u7684 bash \u7279\u6027\uff0c\u5176\u4e2d\uff0c\u7279\u522b\u5b57\u7b26\u548c\u5b57\u7b26\u7cfb\u5217\u7531\u503c\u66ff\u6362\u3002\u4f8b\u5982\uff0c&#8221;!&#8221; \u5b57\u7b26\u662f\u5386\u53f2\u6269\u5c55\u5b57\u7b26\uff0cbash \u901a\u5e38\u5c06\u5176\u66ff\u6362\u4e3a\u524d\u9762\u8f93\u5165\u7684\u547d\u4ee4\u3002\u5c3d\u7ba1\u8fd9\u4e2a\u7c7b\u4f3c\u4e8e\u5b8f\u7684\u529f\u80fd\u5f88\u4fbf\u5229\uff0c\u4f46\u6211\u4eec\u73b0\u5728\u53ea\u60f3\u5728\u73af\u5883\u53d8\u91cf\u540e\u9762\u52a0\u4e0a\u4e00\u4e2a\u7b80\u5355\u7684\u611f\u53f9\u53f7\uff0c\u800c\u4e0d\u662f\u5b8f\u3002<\/p>\n<h2>\u73af\u5883\u53d8\u91cf\u7684\u4f7f\u7528<\/h2>\n<pre>$ echo foo$myvarbar<\/pre>\n<p>bash\u4f1a\u56f0\u60d1\uff0c\u5230\u5e95\u6269\u5c55$m\u3001$my\u3001$myvar\u3001$myvarbar&#8230;\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u8981\u7528\u663e\u5f0f\u7684\u82b1\u62ec\u53f7\u5c06\u4ed6\u62ec\u8d77\u3002<\/p>\n<pre>$ echo foo${myvar}bar<\/pre>\n<p>\u4e00\u5b9a\u8981\u8bb0\u4f4f\uff1a\u5f53\u73af\u5883\u53d8\u91cf\u6ca1\u6709\u7528\u7a7a\u767d\uff08\u7a7a\u683c\u6216\u5236\u8868\u952e\uff09\u548c\u5468\u56f4\u6587\u672c\u5206\u5f00\u65f6\uff0c\u8981\u4f7f\u7528\u66f4\u660e\u786e\u7684<code>\u82b1\u62ec\u53f7<\/code>\u5f62\u5f0f\u3002<\/p>\n<p>\u5f53\u4e00\u4e2a\u73af\u5883\u53d8\u91cf\u88abexport\u65f6\uff0c\u4ed6\u80fd\u591f\u81ea\u52a8\u5730\u7531\u4ee5\u540e\u8fd0\u884c\u7684\u4efb\u4f55\u811a\u672c\u6216\u53ef\u6267\u884c\u73af\u5883\u4f7f\u7528\u3002shell\u811a\u672c\u80fd\u591f\u4f7f\u7528shell\u7684\u5185\u7f6e\u73af\u5883\u53d8\u91cf\u652f\u6301\u201c\u5230\u8fbe\u201d\u73af\u5883\u53d8\u91cf\uff0c\u800cC\u8bed\u8a00\u80fd\u591f\u4f7f\u7528<code>getenv()<\/code>\u51fd\u6570\u8c03\u7528\u3002\u5982\u4e0bC\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\nint main(void) {\r\n    char *myenvvar=getenv(\"myvar\");\r\n    printf(\"The myvar environment variable is %s \",myenvvar);\r\n}<\/pre>\n<p>\u76f4\u63a5\u6267\u884c\uff0c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>The myvar environment variable is (null)<\/pre>\n<p>\u5728<code>export myvar<\/code>\u540e\uff0c\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>The myvar environment variable is This is my environment variable!<\/pre>\n<p>\u4f7f\u7528<code>unset myvar<\/code>\u9664\u53bb\u73af\u5883\u53d8\u91cf\u540e\uff0c\u6267\u884c\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>The myvar environment variable is (null)<\/pre>\n<p>\u53e6\u5916\uff0c\u4e5f\u80fd\u591f\u5728\u4e00\u884c\u5b9a\u4e49\u5e76export\u73af\u5883\u53d8\u91cf\uff0c\u5982\uff1a<\/p>\n<pre>export myvar=abc<\/pre>\n<h2 id=\"cut_string\">\u622a\u65ad\u5b57\u7b26\u4e32<\/h2>\n<p>\u622a\u65ad\u5b57\u7b26\u4e32\u662f\u5c06\u521d\u59cb\u5b57\u7b26\u4e32\u622a\u65ad\u6210\u8f83\u5c0f\u7684\u5355\u72ec\u5757\uff0c\u4ed6\u662f\u4e00\u822c shell \u811a\u672c\u6bcf\u5929\u6267\u884c\u7684\u4efb\u52a1\u4e4b\u4e00\u3002<\/p>\n<p>\u8fd9\u91cc\u6709basename\u548cdirname\uff0cbasename\u8fd4\u56de\u8def\u5f84\u7684\u5c3e\uff0cdirname\u8fd4\u56debasename\u4e22\u5f03\u7684\u201c\u53e6\u201d\u4e00\u4e2a\u90e8\u5206\u8def\u5f84\u3002<\/p>\n<pre>$ basename \/etc\/nginx\/sites-available\/default \r\ndefault\r\n$ dirname \/etc\/nginx\/sites-available\/default \r\n\/etc\/nginx\/sites-available<\/pre>\n<h2>\u547d\u4ee4\u66ff\u6362<\/h2>\n<p>\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u53ef\u6267\u884c\u547d\u4ee4\u7ed3\u679c\u7684\u73af\u5883\u53d8\u91cf\uff0c\u80fd\u591f\u901a\u8fc7\u5982\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<pre>$ MYDIR=`dirname \/usr\/local\/share\/doc\/foo\/foo.txt`\r\n$ echo $MYDIR\r\n\/usr\/local\/share\/doc\/foo<\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u5728\u7b2c\u4e00\u884c\uff0c\u5c06\u8981\u6267\u884c\u7684\u547d\u4ee4\u4ee5 \u53cd\u5f15\u53f7 \u62ec\u8d77\u3002\u4e0d\u662f\u6807\u51c6\u7684\u5355\u5f15\u53f7\uff0c\u800c\u662f\u952e\u76d8\u4e2d\u901a\u5e38\u4f4d\u4e8eTab\u952e\u4e4b\u4e0a\u7684\u5355\u5f15\u53f7\u3002(\u6ce8\uff1a\u5355\u5f15\u53f7\u4e2d\u7684\u5185\u5bb9\u5c06\u4f1a\u88ab\u5f3a\u5236\u663e\u793a\uff0c\u4e5f\u5c31\u662f\u8bf4\u5355\u5f15\u53f7\u4e2d\u7684\u547d\u4ee4\u4e0d\u4f1a\u88ab\u66ff\u6362)<br \/>\n\u9664\u4e86\u53cd\u5f15\u53f7&#8220;\u5916\uff0c\u8fd8\u80fd\u591f\u4f7f\u7528$()\u6765\u5b8c\u6210\u540c\u6837\u64cd\u4f5c<\/p>\n<pre>$ MYDIR=$(dirname \/usr\/local\/share\/doc\/foo\/foo.txt)\r\n$ echo $MYDIR\r\n\/usr\/local\/share\/doc\/foo<\/pre>\n<p>\u4f7f\u7528\u547d\u4ee4\u66ff\u6362\u80fd\u591f\u5c06\u4efb\u4f55\u547d\u4ee4\u6216\u547d\u4ee4\u7ba1\u9053\u653e\u5728&#8220;\u6216$()\u4e4b\u95f4\uff0c\u5e76\u5c06\u5176\u5206\u914d\u7ed9\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<h2>\u50cf\u4e13\u4e1a\u4eba\u5458\u90a3\u6837\u622a\u65ad\u5b57\u7b26\u4e32<\/h2>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u6267\u884c\u66f4\u9ad8\u7ea7\u7684\u5b57\u7b26\u4e32&#8221;\u622a\u65ad&#8221;\uff0c\u5982\u4e0b\u4f8b\u5b50\uff1a<\/p>\n<pre>$ MYVAR=foodforthought.jpg\r\n$ echo ${MYVAR##*fo}\r\nrthought.jpg\r\n$ echo ${MYVAR#*fo}\r\nodforthought.jpg<\/pre>\n<p>\u7b2c\u4e00\u4e2aecho\uff0cbash\u53d6\u5f97MYVAR\uff0c\u627e\u5230\u4ece\u5b57\u7b26\u4e32&#8221;foodforthought.jpg&#8221; \u5f00\u59cb\u5904\u5f00\u59cb\u3001\u4e14\u5339\u914d\u901a\u914d\u7b26 &#8220;*fo&#8221; \u7684<code>\u6700\u957f\u5b50\u5b57\u7b26\u4e32<\/code>\uff0c\u7136\u540e\u5c06\u5176\u4ece\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u5904\u622a\u53bb\u3002<br \/>\n\u7b2c\u4e8c\u4e2aecho\uff0cbash\u53d6\u5f97MYVAR\uff0c\u627e\u5230\u4ece\u5b57\u7b26\u4e32&#8221;foodforthought.jpg&#8221; \u5f00\u59cb\u5904\u5f00\u59cb\u3001\u4e14\u5339\u914d\u901a\u914d\u7b26 &#8220;*fo&#8221; \u7684<code>\u6700\u77ed\u5b50\u5b57\u7b26\u4e32<\/code>\uff0c\u7136\u540e\u5c06\u5176\u4ece\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u5904\u622a\u53bb\u3002<\/p>\n<p>\u8bb0\u5fc6\u65b9\u6cd5\uff1a\u5f53\u641c\u7d22\u6700\u957f\u5339\u914d\u65f6\uff0c\u4f7f\u7528 ##\uff08\u56e0\u4e3a ## \u6bd4 # \u957f\uff09\u3002\u5f53\u641c\u7d22\u6700\u77ed\u5339\u914d\u65f6\uff0c\u4f7f\u7528 #\u3002\u5982\u4f55\u8bb0\u4f4f\u4f7f\u7528&#8221;#&#8221;\u5b57\u7b26\u6765\u4ece\u5b57\u7b26\u4e32\u5f00\u59cb\u90e8\u5206\u51fa\u53bb\uff1f\u5728\u7f8e\u56fd\u952e\u76d8\u4e0a\uff0cshift-4 \u662f &#8220;$&#8221;\uff0c\u4ed6\u662f bash \u53d8\u91cf\u6269\u5c55\u5b57\u7b26\u3002\u5728\u952e\u76d8\u4e0a\uff0c\u7d27\u9760 &#8220;$&#8221; \u5de6\u8fb9\u7684\u662f &#8220;#&#8221;\u3002\u8fd9\u6837\uff0c\u80fd\u591f\u770b\u5230\uff1a&#8221;#&#8221; \u4f4d\u4e8e &#8220;$&#8221; \u7684\u201c\u5f00\u59cb\u5904\u201d\uff0c\u56e0\u6b64\uff08\u6839\u636e\u6211\u4eec\u7684\u8bb0\u5fc6\u6cd5\uff09\uff0c&#8221;#&#8221; \u4ece\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u5904\u9664\u53bb\u5b57\u7b26\u3002<\/p>\n<p>\u540c\u7406\uff0c\u4f7f\u7528&#8221;%&#8221;\u6765\u4ece\u5c3e\u90e8\u622a\u53bb\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>$ MYFOO=\"chickensoup.tar.gz\"\r\n$ echo ${MYFOO%%.*}\r\nchickensoup\r\n$ echo ${MYFOO%.*}\r\nchickensoup.tar<\/pre>\n<p>\u5047\u5982\u5fd8\u8bb0\u4e86\u5e94\u8be5\u4f7f\u7528&#8221;#&#8221; \u8fd8\u662f &#8220;%&#8221;\uff0c\u5219\u770b\u4e00\u4e0b\u952e\u76d8\u4e0a\u7684 3\u30014 \u548c 5 \u952e\uff0c\u7136\u540e\u731c\u51fa\u6765\u3002<br \/>\n\u66f4\u6709\u53e6\u4e00\u79cd\u5f62\u5f0f\u7684\u53d8\u91cf\u6269\u5c55\uff0c\u6765\u9009\u62e9\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<pre>$ EXCLAIM=cowabunga\r\n$ echo ${EXCLAIM:0:3}\r\ncow\r\n$ echo ${EXCLAIM:3:7}\r\nabunga<\/pre>\n<p>\u8bf7\u6ce8\u610f\u547d\u4ee4\u66ff\u6362$()\u548c\u622a\u65ad\u5b57\u7b26\u4e32${}\u7684\u533a\u522b<\/p>\n<h2>\u5e94\u7528\u5b57\u7b26\u4e32\u622a\u65ad<\/h2>\n<p>\u4e0b\u9762\u662f\u4e2a\u7b80\u5355\u7684shell\u811a\u672c\uff0c\u8fd9\u4e2a\u811a\u672c\u63a5\u53d7\u4e00\u4e2a\u6587\u6863\u4f5c\u4e3a\u81ea\u53d8\u91cf\uff0c\u7136\u540e\u6253\u5370\uff1a\u8be5\u6587\u6863\u662f\u5426\u662f\u4e2atar\u6587\u6863\u3002<\/p>\n<pre>#!\/bin\/bash\r\n if [ \"${1##*.}\" = \"tar\" ]\r\n then\r\n echo This appears to be a tarball.\r\n else\r\n echo At first glance, this does not appear to be a tarball.\r\n fi<\/pre>\n<p>\u770b\u4e00\u4e0b\u4e0a\u4f8b\u4f7f\u7528\u7684&#8221;if&#8221;\u8bed\u53e5\u3002\u8bed\u53e5\u4e2d\u4f7f\u7528\u4e86\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\u3002\u5728bash\u4e2d\uff0c&#8221;=&#8221;\u6bd4\u8f83\u8fd0\u7b97\u7b26\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\u3002\u5728bash\u4e2d\uff0c\u4efb\u4f55\u5e03\u5c14\u8868\u8fbe\u5f0f\u90fd\u7528\u65b9\u62ec\u53f7\u62ec\u8d77\u3002<br \/>\n($1\u662f\u4f20\u7ed9\u811a\u672c\u7684\u7b2c\u4e00\u4e2a\u547d\u4ee4\u884c\u81ea\u53d8\u91cf\uff0c$2\u662f\u7b2c\u4e8c\u4e2a\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002)<\/p>\n<p>If\u8bed\u53e5<\/p>\n<pre>if [ condition ]\r\nthen\r\naction\r\nfi<\/pre>\n<p>\u53ea\u6709\u5f53condition\u4e3a\u771f\u65f6\uff0c\u8be5\u8bed\u53e5\u624d\u6267\u884c\u64cd\u4f5c\uff0c\u5426\u5219\u4e0d\u6267\u884c\u64cd\u4f5c\uff0c\u5e76\u7ee7\u7eed\u6267\u884c&#8221;fi&#8221;\u4e4b\u540e\u7684\u8bed\u53e5\u3002<\/p>\n<pre>if [ condition ]\r\n then\r\n action\r\n elif [ condition2 ]\r\n then\r\n action2\r\n .\r\n .\r\n .\r\n elif [ condition3 ]\r\n then\r\n else\r\n actionx\r\n fi<\/pre>\n<p>\u4ee5\u4e0a&#8221;elif&#8221;\u5f62\u5f0f\u5c06\u8fde\u7eed\u6d4b\u8bd5\u6bcf\u4e2a\u6761\u4ef6\uff0c\u5e76\u6267\u884c\u7b26\u5408\u7b2c\u4e00\u4e2a\u771f\u6761\u4ef6\u7684\u64cd\u4f5c\u3002\u5047\u5982\u6ca1\u6709\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u5c06\u6267\u884c&#8221;else&#8221;\u64cd\u4f5c\uff0c\u5047\u5982\u6709\u4e00\u4e2a\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u7ee7\u7eed\u6267\u884c\u6574\u4e2a&#8221;if,elif,else&#8221;\u8bed\u53e5\u4e4b\u540e\u7684\u884c\u3002<\/p>\n<p><span style=\"color: #000000; font-weight: bold;\">\u63a5\u6536\u81ea\u53d8\u91cf<\/span><\/p>\n<p>\u770b\u4e00\u4e0b\u5982\u4e0b\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre>#! \/bin\/bash\r\necho name of script. is $ 0\r\necho first argument is $ 1\r\necho second argument is $ 2\r\necho seventeenth argument is $ 17\r\necho number of arguments is $#<\/pre>\n<p>Bash\u4e2d\u5c06&#8221; $ 0 &#8220;\u6269\u5c55\u6210\u4ece\u547d\u4ee4\u884c\u8c03\u7528\u7684\u811a\u672c\u540d\u79f0\uff0c&#8221;$#&#8221;\u88ab\u6269\u5c55\u6210\u4f20\u9012\u7ed9\u811a\u672c\u7684\u81ea\u53d8\u91cf\u6570\u76ee\u3002(\u8bf7\u6ce8\u610f$ \u548c 0\u662f\u8fde\u8d77\u6765\u7684)<\/p>\n<p>\u5728Bash\u7f16\u7a0b\u4e2d\u6709\u65f6\u5019\u9700\u8981\u4e00\u6b21\u5f15\u7528\u4efb\u4f55\u547d\u4ee4\u884c\u81ea\u53d8\u91cf\uff0c\u9488\u5bf9\u8fd9\u79cd\u7528\u9014\uff0cbash\u5b9e\u73b0\u4e86\u53d8\u91cf&#8221;$@&#8221;\uff0c\u4ed6\u88ab\u6269\u5c55\u6210\u4efb\u4f55\u7528\u7a7a\u683c\u5206\u5f00\u7684\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<h2>Bash\u7f16\u7a0b\u7ed3\u6784<\/h2>\n<p>\u5728\u5404\u79cd\u7f16\u7a0b\u8bed\u8a00\u4e2d\u51fa\u73b0\u7684&#8221;if&#8221;\u8bed\u53e5\u548c&#8221;for&#8221;\u5faa\u73af\u7b49\u6807\u51c6\u7f16\u7a0b\u7ed3\u6784\uff0cBash\u6709\u81ea\u5df1\u7684\u7248\u672c\u3002\u4ee5\u4e0b\u51e0\u8282\uff0c\u5c06\u4ecb\u7ecd\u51e0\u79cdbash\u7ed3\u6784\uff0c\u5e76\u6f14\u793a\u8fd9\u4e9b\u7ed3\u6784\u53ca\u5176\u548c\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7ed3\u6784\u7684\u5dee\u5f02\u3002<\/p>\n<p>\u5728C\u8bed\u8a00\u4e2d\uff0c\u8981\u6bd4\u8f83\u7279\u5b9a\u6587\u6863\u662f\u5426\u6bd4\u53e6\u4e00\u4e2a\u6587\u6863\u65b0\u5fc5\u987b\u4f7f\u7528\u4e24\u4e2astat()\u8c03\u7528\u548c\u4e24\u4e2astat\u7ed3\u6784\u6765\u8fdb\u884c\u624b\u5de5\u6bd4\u8f83\u3002\u800c\u5728bash\u4e2d\uff0c\u56e0\u4e3a\u5176\u5185\u7f6e\u4e86\u6807\u51c6\u6587\u6863\u6bd4 \u8f83\u8fd0\u7b97\u7b26\uff0c\u56e0\u6b64\uff0c\u786e\u5b9a&#8221;\/tmp\/myfile \u662f\u5426\u53ef\u8bfb&#8221;\u548c\u67e5\u770b&#8221;$myvar \u662f\u5426\u5927\u4e8e 4&#8243;\u76f8\u540c\u5bb9\u6613\u3002 \u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre>if [ -z \"$myvar\" ]\r\nthen\r\necho \"myvar is not defined\"\r\nfi<\/pre>\n<p>\u4e0a\u4f8b\u8868\u793a\u5047\u5982$myvar\u6ca1\u6709\u5b9a\u4e49(\u5373$myvar\u4e3a0)\uff0c\u5219echo &#8220;&#8230;&#8230;&#8230;.&#8221;<\/p>\n<p>bash\u4e2d\u53ef\u4f7f\u7528\u7684\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5982\u4e0b\uff1a<\/p>\n<p>\u6587\u6863\u6bd4\u8f83\u8fd0\u7b97\u7b26<br \/>\n-e filename \u00a0 \u00a0\u5047\u5982 filename\u5b58\u5728\uff0c\u5219\u4e3a\u771f \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 [ -e \/var\/log\/syslog ]<br \/>\n-d filename \u00a0 \u00a0\u5047\u5982 filename\u4e3a\u76ee\u5f55\uff0c\u5219\u4e3a\u771f \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 [ -d \/tmp\/mydir ]<br \/>\n-f filename \u00a0 \u00a0 \u5047\u5982 filename\u4e3a\u5e38\u89c4\u6587\u6863\uff0c\u5219\u4e3a\u771f \u00a0 \u00a0 \u00a0 \u00a0 \u00a0[ -f \/usr\/bin\/grep ]<br \/>\n-L filename \u00a0 \u00a0\u5047\u5982 filename\u4e3a\u7b26\u53f7\u94fe\u63a5\uff0c\u5219\u4e3a\u771f \u00a0 \u00a0 \u00a0 \u00a0 \u00a0[ -L \/usr\/bin\/grep ]<br \/>\n-r filename \u00a0 \u00a0 \u5047\u5982 filename\u53ef\u8bfb\uff0c\u5219\u4e3a\u771f \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 [ -r \/var\/log\/syslog ]<br \/>\n-w filename \u00a0 \u00a0\u5047\u5982 filename\u53ef\u5199\uff0c\u5219\u4e3a\u771f \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0[ -w \/var\/mytmp.txt ]<br \/>\n-x filename \u00a0 \u00a0 \u5047\u5982 filename\u53ef\u6267\u884c\uff0c\u5219\u4e3a\u771f \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0[ -L \/usr\/bin\/grep ]<br \/>\nfilename1-nt filename2 \u5047\u5982 filename1\u6bd4 filename2\u65b0\uff0c\u5219\u4e3a\u771f<\/p>\n<p>\u4f8b\u5982 [ \/tmp\/install\/etc\/services -nt \/etc\/services ]<br \/>\nfilename1-ot filename2 \u5047\u5982 filename1\u6bd4 filename2\u65e7\uff0c\u5219\u4e3a\u771f<\/p>\n<p>\u4f8b\u5982 [ \/boot\/bzImage -ot arch\/i386\/boot\/bzImage ]<\/p>\n<p>\u5b57\u7b26\u4e32\u6bd4\u8f83\u8fd0\u7b97\u7b26 \uff08\u8bf7\u6ce8\u610f\u5f15\u53f7\u7684\u4f7f\u7528\uff0c\u8fd9\u662f\u9632\u6b62\u7a7a\u683c\u6270\u4e71\u4ee3\u7801\u7684\u597d\u65b9\u6cd5\uff09<\/p>\n<p>-z string \u5047\u5982 string\u957f\u5ea6\u4e3a\u96f6\uff0c\u5219\u4e3a\u771f [ -z &#8220;$myvar&#8221; ]<br \/>\n-n string \u5047\u5982 string\u957f\u5ea6\u975e\u96f6\uff0c\u5219\u4e3a\u771f [ -n &#8220;$myvar&#8221; ]<br \/>\nstring1= string2 \u5047\u5982 string1\u548c string2\u76f8\u540c\uff0c\u5219\u4e3a\u771f [ &#8220;$myvar&#8221; = &#8220;one two three&#8221; ]<br \/>\nstring1!= string2 \u5047\u5982 string1\u548c string2\u4e0d\u540c\uff0c\u5219\u4e3a\u771f [ &#8220;$myvar&#8221; != &#8220;one two three&#8221; ]<\/p>\n<p>\u7b97\u672f\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/p>\n<p>num1-eq num2 \u7b49\u4e8e [ 3 -eq $mynum ]<br \/>\nnum1-ne num2 \u4e0d\u7b49\u4e8e [ 3 -ne $mynum ]<br \/>\nnum1-lt num2 \u5c0f\u4e8e [ 3 -lt $mynum ]<br \/>\nnum1-le num2 \u5c0f\u4e8e\u6216\u7b49\u4e8e [ 3 -le $mynum ]<br \/>\nnum1-gt num2 \u5927\u4e8e [ 3 -gt $mynum ]<br \/>\nnum1-ge num2 \u5927\u4e8e\u6216\u7b49\u4e8e [ 3 -ge $mynum ]<\/p>\n<p>\u6709\u65f6\uff0c\u6709\u51e0\u79cd\u4e0d\u540c\u65b9\u6cd5\u6765\u8fdb\u884c\u7279\u5b9a\u6bd4\u8f83\u3002\u5982\u4e0b\uff1a<\/p>\n<pre> if [ \"$myvar\" -eq 3 ]\r\n then\r\n echo \"myvar equals 3\"\r\n fi\r\n if [ \"$myvar\" = \"3\" ]\r\n then\r\n echo \"myvar equals 3\"\r\n fi<\/pre>\n<p>\u4e0a\u9762\u4e24\u4e2a\u6bd4\u8f83\u6267\u884c\u76f8\u540c\u7684\u529f\u80fd\uff0c\u4f46\u662f\u7b2c\u4e00\u4e2a\u4f7f\u7528\u7b97\u672f\u6bd4\u8f83\u8fd0\u7b97\u7b26\uff0c\u800c\u7b2c\u4e8c\u4e2a\u4f7f\u7528\u5b57\u7b26\u4e32\u6bd4\u8f83\u8fd0\u7b97\u7b26\u3002<\/p>\n<p>\u5b57\u7b26\u4e32\u6bd4\u8f83\u8bf4\u660e<\/p>\n<p>\u5927\u591a\u6570\u65f6\u5019\uff0c\u867d\u7136\u80fd\u591f\u4e0d\u4f7f\u7528\u62ec\u8d77\u5b57\u7b26\u4e32\u548c\u5b57\u7b26\u4e32\u53d8\u91cf\u7684\u53cc\u5f15\u53f7\uff0c\u4f46\u8fd9\u5e76\u4e0d\u662f\u597d\u4e3b\u610f\u3002\u56e0\u4e3a\u5047\u5982\u73af\u5883\u53d8\u91cf\u4e2d\u6070\u5de7\u6709\u4e00\u4e2a\u7a7a\u683c\u6216\u5236\u8868\u952e\uff0cbash \u5c06\u65e0\u6cd5\u5206\u8fa8\uff0c\u4ece\u800c\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u9519\u8bef\u7684\u6bd4\u8f83\u793a\u4f8b\uff1a<\/p>\n<pre>if [ $myvar = \"foo bar oni\" ]\r\n then\r\n echo \"yes\"\r\n fi<\/pre>\n<p>\u5728\u4e0a\u4f8b\u4e2d\uff0c\u5047\u5982 myvar \u7b49\u4e8e &#8220;foo&#8221;\uff0c\u5219\u4ee3\u7801\u5c06\u6309\u9884\u60f3\u5de5\u4f5c\uff0c\u4e0d\u8fdb\u884c\u6253\u5370\u3002\u4f46\u662f\uff0c\u5047\u5982 myvar \u7b49\u4e8e &#8220;foo bar oni&#8221;\uff0c\u5219\u4ee3\u7801\u5c06\u56e0\u4ee5\u4e0b\u9519\u8bef\u5931\u8d25\uff1a<br \/>\n[: too many arguments<\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c&#8221;$myvar&#8221;\uff08\u7b49\u4e8e &#8220;foo bar oni&#8221;\uff09\u4e2d\u7684\u7a7a\u683c\u8ff7\u60d1\u4e86 bash\u3002bash \u6269\u5c55 &#8220;$myvar&#8221; \u4e4b\u540e\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<br \/>\n[ foo bar ni = &#8220;foo bar oni&#8221; ]<br \/>\n\u56e0\u4e3a\u73af\u5883\u53d8\u91cf\u6ca1\u653e\u5728\u53cc\u5f15\u53f7\u4e2d\uff0c\u6240\u4ee5 bash \u8ba4\u4e3a\u65b9\u62ec\u53f7\u4e2d\u7684\u81ea\u53d8\u91cf\u8fc7\u591a\u3002\u80fd\u591f\u7528\u53cc\u5f15\u53f7\u5c06\u5b57\u7b26\u4e32\u81ea\u53d8\u91cf\u62ec\u8d77\u6765\u6d88\u9664\u8be5\u95ee\u9898\u3002\u8bf7\u8bb0\u4f4f\uff0c\u5047\u5982\u517b\u6210\u5c06\u4efb\u4f55\u5b57\u7b26\u4e32\u81ea\u53d8\u91cf\u7528\u53cc\u5f15\u53f7\u62ec\u8d77\u7684\u4e60\u60ef\uff0c\u5c06\u9664\u53bb\u5f88\u591a\u7c7b\u4f3c\u7684\u7f16\u7a0b\u9519\u8bef\u3002&#8221;foo bar oni&#8221; \u6bd4\u8f83 \u5e94\u8be5\u5199\u6210\uff1a<\/p>\n<pre>if [ \"$myvar\" = \"foo bar oni\" ]\r\n then\r\n echo \"yes\"\r\n fi<\/pre>\n<p>\u5728\u8c03\u7528\u73af\u5883\u53d8\u91cf\u7684\u65f6\u5019\u6700\u597d\u4f7f\u7528&#8221;&#8221;\u5c06\u73af\u5883\u53d8\u91cf\u62ec\u8d77\u6765\u3002(\u6ce8\u610f\uff1a\u5047\u5982\u60f3\u5f15\u7528\u73af\u5883\u53d8\u91cf\u7684\u503c\uff0c\u5219\u4e0d\u8981\u4f7f\u7528\u5355\u5f15\u53f7\uff0c\u56e0\u4e3a\u5355\u5f15\u53f7\u4f1a\u7981\u7528\u53d8\u91cf(\u548c\u5386\u53f2)\u6269\u5c55\u3002\u5982\u5728\u4e0a\u4f8b\u4e2d\u4f1a\u53ea\u63a5\u53d7\u7a7a\u683c\u4ee5\u524d\u7684\u5b57\u7b26)<\/p>\n<p>\u5faa\u73af\u7ed3\u6784\uff1a&#8221;for&#8221;\u00a0\u5148\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre>#! \/bin\/bash\r\nfor x in one two three four\r\n do\r\n echo number $x\r\n done<\/pre>\n<p>\u8f93\u51fa\uff1a<br \/>\nnumber one<br \/>\nnumber two<br \/>\nnumber three<br \/>\nnumber four<br \/>\n\u8fd9\u4e2a\u4f8b\u5b50\u4e2d&#8221;for x&#8221;\u90e8\u5206\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a&#8221;$x&#8221;\u7684\u65b0\u73af\u5883\u53d8\u91cf(\u4e5f\u79f0\u4e3a\u5faa\u73af\u63a7\u5236\u53d8\u91cf)\uff0c\u4ed6\u7684\u503c\u88ab\u4f9d\u6b21\u914d\u7f6e\u4e3a&#8221;one&#8221;\u3001&#8221;two&#8221;\u3001&#8221;three&#8221;\u548c&#8221;four&#8221;\u3002\u6bcf\u4e00\u6b21 \u8d4b\u503c\u4e4b\u540e\uff0c\u6267\u884c\u4e00\u6b21\u5faa\u73af\u4f53(&#8220;do&#8221;\u548c&#8221;done&#8221;\u4e4b\u95f4\u7684\u4ee3\u7801)\u3002\u5728\u5faa\u73af\u4f53\u5185\uff0c\u8c61\u5176\u4ed6\u73af\u5883\u53d8\u91cf\u76f8\u540c\uff0c\u4f7f\u7528\u6807\u51c6\u7684\u53d8\u91cf\u6269\u5c55\u8bed\u6cd5\u6765\u5f15\u7528\u5faa\u73af\u63a7\u5236\u53d8\u91cf&#8221;$x&#8221;\u3002 \u8fd8\u8981\u6ce8\u610f\uff0c&#8221;for&#8221;\u5faa\u73af\u603b\u662f\u63a5\u53d7&#8221;in&#8221;\u8bed\u53e5\u4e4b\u540e\u7684\u67d0\u79cd\u7c7b\u578b\u7684\u5b57\u5217\u8868\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6307\u5b9a\u4e86\u56db\u4e2a\u82f1\u8bed\u5b57\u6bcd\uff0c\u4f46\u662f\u5b57\u5217\u8868\u4e5f\u80fd\u591f\u5f15\u7528\u78c1\u76d8\u4e0a\u7684\u6587\u6863\uff0c\u751a\u81f3\u6587\u6863\u901a\u914d\u7b26\u3002\u518d\u770b\u4e00\u4e2a\u4f7f\u7528\u6807\u51c6shell\u901a\u914d\u7b26\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre>#! \/bin\/bash\r\n for myfile in \/etc\/r*\r\n do\r\n if [ -d \"$myfile\" ]\r\n then\r\n echo \"$myfile (dir)\"\r\n else\r\n echo \"$myfile\"\r\n fi\r\n done<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>\/etc\/rc.d (dir)\r\n\/etc\/resolv.conf\r\n\/etc\/resolv.conf~\r\n\/etc\/rpc<\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5217\u51fa\u5728 \/etc \u4e2d\u6bcf\u4e2a\u4ee5 &#8220;r&#8221; \u5f00\u5934\u7684\u6587\u6863\u3002\u8981\u505a\u5230\u8fd9\u70b9\uff0cbash \u5728\u6267\u884c\u5faa\u73af\u4e4b\u524d\u9996\u5148\u53d6\u5f97\u901a\u914d\u7b26 \/etc\/r*\uff0c\u7136\u540e\u6269\u5c55\u4ed6\uff0c\u7528\u5b57\u7b26\u4e32 \/etc\/rc.d \/etc\/resolv.conf \/etc\/resolv.conf~ \/etc\/rpc \u66ff\u6362\u3002\uff08\u4e5f\u5373\u5faa\u73af\u8bed\u53e5\u88ab\u66ff\u6362\u6210for myfile in \/etc\/rc.d \/etc\/resolv.conf \/etc\/resolv.conf~ \/etc\/rpc \uff09\u4e00\u65e6\u8fdb\u5165\u5faa\u73af\uff0c\u6839\u636e myfile \u662f\u5426\u4e3a\u76ee\u5f55\uff0c&#8221;-d&#8221; \u6761\u4ef6\u8fd0\u7b97\u7b26\u7528\u6765\u6267\u884c\u4e24\u4e2a\u4e0d\u540c\u64cd\u4f5c\u3002\u5047\u5982\u662f\u76ee\u5f55\uff0c\u5219\u5c06 &#8220;(dir)&#8221; \u9644\u52a0\u5230\u8f93\u51fa\u884c\u3002<br \/>\n\u8fd8\u80fd\u591f\u5728\u5b57\u5217\u8868\u4e2d\u4f7f\u7528\u591a\u4e2a\u901a\u914d\u7b26\u3001\u751a\u81f3\u662f\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<pre>for x in \/etc\/r--? \/var\/lo* \/home\/drobbins\/mystuff\/* \/tmp\/${MYPATH}\/*\r\n do\r\n cp $x \/mnt\/mydir\r\n done<\/pre>\n<p>\u867d\u7136\u4efb\u4f55\u901a\u914d\u7b26\u6269\u5c55\u793a\u4f8b\u4f7f\u7528\u4e86 \u7edd\u5bf9\u8def\u5f84\uff0c\u4f46\u4e5f\u80fd\u591f\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>for x in ..\/* mystuff\/*\r\n do\r\n echo $x is a silly file\r\n done<\/pre>\n<p>\u518d\u770b\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>for x in \/var\/log\/*\r\n do\r\n echo `basename $x` is a file living in \/var\/log\r\n done<\/pre>\n<p>\u770b\u770b\u5982\u4f55\u4f7f\u7528&#8221;$@&#8221;\u7684\uff1a<\/p>\n<pre>#!\/usr\/bin\/env bash<\/pre>\n<pre>for thing in \"$@\"\r\n do\r\n echo you typed ${thing}.\r\n done<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>$ allargs hello there you silly\r\n you typed hello.\r\n you typed there.\r\n you typed you.\r\n you typed silly.<\/pre>\n<p>Shell\u7b97\u672f<\/p>\n<p>\u5728\u5b66\u4e60\u53e6\u4e00\u7c7b\u578b\u7684\u5faa\u73af\u7ed3\u6784\u4e4b\u524d\uff0c\u6700\u597d\u5148\u719f\u6089\u5982\u4f55\u6267\u884c shell \u7b97\u672f\u3002\u80fd\u591f\u4f7f\u7528 shell \u7ed3\u6784\u6765\u6267\u884c\u7b80\u5355\u7684\u6574\u6570\u8fd0\u7b97\u3002\u53ea\u9700\u5c06\u7279\u5b9a\u7684\u7b97\u672f\u8868\u8fbe\u5f0f\u7528 &#8220;$((&#8221; \u548c &#8220;))&#8221; \u62ec\u8d77\uff0cbash \u5c31\u80fd\u591f\u8ba1\u7b97\u8868\u8fbe\u5f0f\u3002\u8fd9\u91cc\u6709\u4e00\u4e9b\u4f8b\u5b50\uff1a<\/p>\n<pre>$ echo $(( 100 \/ 3 ))\r\n 33\r\n $ myvar=\"56\"\r\n $ echo $(( $myvar + 12 ))\r\n 68\r\n $ echo $(( $myvar - $myvar ))\r\n 0 $ myvar=$(( $myvar + 1 ))\r\n $ echo $myvar\r\n 57<\/pre>\n<p>\u66f4\u591a\u7684\u5faa\u73af\u7ed3\u6784\uff1a&#8221;while&#8221; \u548c &#8220;until&#8221;<br \/>\n\u53ea\u8981\u7279\u5b9a\u6761\u4ef6\u4e3a\u771f\uff0c&#8221;while&#8221; \u8bed\u53e5\u5c31\u4f1a\u6267\u884c\uff0c\u5176\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre>while [ condition ]\r\n do\r\n statements\r\n done<\/pre>\n<p>\u901a\u5e38\u4f7f\u7528 &#8220;While&#8221; \u8bed\u53e5\u6765\u5faa\u73af\u4e00\u5b9a\u6b21\u6570\uff0c\u6bd4\u5982\uff0c\u4e0b\u4f8b\u5c06\u5faa\u73af 10 \u6b21\uff1a<\/p>\n<pre>myvar=0\r\n while [ $myvar -ne 10 ]\r\n do\r\n echo $myvar\r\n myvar=$(( $myvar + 1 ))\r\n done<\/pre>\n<p>\u80fd\u591f\u770b\u5230\uff0c\u4e0a\u4f8b\u4f7f\u7528\u4e86\u7b97\u672f\u8868\u8fbe\u5f0f\u6765\u4f7f\u6761\u4ef6\u6700\u7ec8\u4e3a\u5047\uff0c\u5e76\u5bfc\u81f4\u5faa\u73af\u7ec8\u6b62\u3002<br \/>\n&#8220;Until&#8221; \u8bed\u53e5\u63d0\u4f9b\u4e86\u548c &#8220;while&#8221; \u8bed\u53e5\u76f8\u53cd\u7684\u529f\u80fd\uff1a\u53ea\u8981\u7279\u5b9a\u6761\u4ef6\u4e3a \u5047 \uff0c\u4ed6\u4eec\u5c31\u91cd\u590d\u3002\u4e0b\u9762\u662f\u4e2a\u548c\u524d\u9762\u7684 &#8220;while&#8221; \u5faa\u73af\u5177\u5907\u540c\u7b49\u529f\u80fd\u7684 &#8220;until&#8221; \u5faa\u73af\uff1a<\/p>\n<pre>myvar=0\r\n until [ $myvar -eq 10 ]\r\n do\r\n echo $myvar\r\n myvar=$(( $myvar + 1 ))\r\n done<\/pre>\n<p>Case \u8bed\u53e5<br \/>\nCase \u8bed\u53e5\u662f\u53e6\u4e00\u79cd\u4fbf\u5229\u7684\u6761\u4ef6\u7ed3\u6784\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u793a\u4f8b\u7247\u6bb5\uff1a<\/p>\n<pre>case \"${x##*.}\" in\r\n gz)\r\n gzunpack ${SROOT}\/${x}\r\n ;;\r\n bz2)\r\n bz2unpack ${SROOT}\/${x}\r\n ;;\r\n *)\r\n echo \"Archive format not recognized.\"\r\n exit\r\n ;;\r\n esac<\/pre>\n<p>\u5728\u4e0a\u4f8b\u4e2d\uff0cbash \u9996\u5148\u6269\u5c55 &#8220;${x##*.}&#8221;\u3002\u5728\u4ee3\u7801\u4e2d\uff0c&#8221;$x&#8221; \u662f\u6587\u6863\u7684\u540d\u79f0\uff0c&#8221;${x##.*}&#8221; \u9664\u53bb\u6587\u6863\u4e2d\u6700\u540e\u53e5\u70b9\u540e\u6587\u672c\u4e4b\u5916\u7684\u4efb\u4f55\u6587\u672c\u3002\u7136\u540e\uff0cbash \u5c06\u4ea7\u751f\u7684\u5b57\u7b26\u4e32\u548c &#8220;)&#8221; \u5de6\u8fb9\u5217\u51fa\u7684\u503c\u505a\u6bd4\u8f83\u3002\u5728\u672c\u4f8b\u4e2d\uff0c&#8221;${x##.*}&#8221; \u5148\u548c &#8220;gz&#8221; \u6bd4\u8f83\uff0c\u7136\u540e\u662f &#8220;bz2&#8243;\uff0c\u6700\u540e\u662f &#8220;*&#8221;\u3002\u5047\u5982 &#8220;${x##.*}&#8221; \u548c\u8fd9\u4e9b\u5b57\u7b26\u4e32\u6216\u6a21\u5f0f\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\u5339\u914d\uff0c\u5219\u6267\u884c\u7d27\u63a5 &#8220;)&#8221; \u4e4b\u540e\u7684\u884c\uff0c\u76f4\u5230 &#8220;;;&#8221; \u4e3a\u6b62\uff0c\u7136\u540e bash \u7ee7\u7eed\u6267\u884c\u7ed3\u675f\u7b26 &#8220;esac&#8221; \u4e4b\u540e\u7684\u884c\u3002\u5047\u5982\u4e0d\u5339\u914d\u4efb\u4f55\u6a21\u5f0f\u6216\u5b57\u7b26\u4e32\uff0c\u5219\u4e0d\u6267\u884c\u4efb\u4f55\u4ee3\u7801\u884c\uff0c\u5728\u8fd9\u4e2a\u7279\u522b\u7684\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u81f3\u5c11\u8981\u6267\u884c\u4e00\u4e2a\u4ee3\u7801\u5757\uff0c\u56e0\u4e3a\u4efb\u4f55\u4e0d\u548c &#8220;gz&#8221; \u6216 &#8220;bz2&#8221; \u5339\u914d\u7684\u5b57\u7b26\u4e32\u90fd\u5c06\u548c &#8220;*&#8221; \u6a21\u5f0f\u5339\u914d\u3002<\/p>\n<h2>\u51fd\u6570<\/h2>\n<p>\u5728bash\u4e2d\u4e5f\u80fd\u591f\u5b9a\u4e49\u51fd\u6570\u3002\u51fd\u6570\u751a\u81f3\u80fd\u591f\u4f7f\u7528\u548c\u811a\u672c\u63a5\u53d7\u547d\u4ee4\u884c\u81ea\u53d8\u91cf\u7c7b\u4f3c\u7684\u65b9\u5f0f\u6765\u63a5\u53d7\u81ea\u53d8\u91cf\u3002<\/p>\n<pre>tarview() {\r\n echo -n \"Displaying contents of $1 \"\r\n if [ ${1##*.} = tar ]\r\n then\r\n echo \"(uncompressed tar)\"\r\n tar tvf $1\r\n elif [ ${1##*.} = gz ]\r\n then\r\n echo \"(gzip-compressed tar)\"\r\n tar tzvf $1\r\n elif [ ${1##*.} = bz2 ]\r\n then\r\n echo \"(bzip2-compressed tar)\"\r\n cat $1 | bzip2 -d | tar tvf -\r\n fi\r\n }\r\n \uff08echo -n \u4e0d\u6362\u884c\uff09<\/pre>\n<p>\u4e0a\u9762\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a &#8220;tarview&#8221; \u7684\u51fd\u6570\uff0c\u4ed6\u63a5\u6536\u4e00\u4e2a\u81ea\u53d8\u91cf\uff0c\u5373\u67d0\u79cd\u7c7b\u578b\u7684 tar \u6587\u6863\u3002\u5728\u6267\u884c\u8be5\u51fd\u6570\u65f6\uff0c\u4ed6\u786e\u5b9a\u81ea\u53d8\u91cf\u662f\u54ea\u79cd tar \u6587\u6863\u7c7b\u578b\uff08\u672a\u538b\u7f29\u7684\u3001gzip \u538b\u7f29\u7684\u6216 bzip2 \u538b\u7f29\u7684\uff09\uff0c\u6253\u5370\u4e00\u884c\u4fe1\u606f\u6027\u6d88\u606f\uff0c\u7136\u540e\u663e\u793a tar \u6587\u6863\u7684\u5185\u5bb9\u3002<\/p>\n<pre>$ tarview shorten.tar.gz\r\n Displaying contents of shorten.tar.gz (gzip-compressed tar)\r\n drwxr-xr-x ajr\/abbot 0 1999-02-27 16:17 shorten-2.3a\/\r\n -rw-r--r-- ajr\/abbot 1143 1997-09-04 04:06 shorten-2.3a\/Makefile\r\n -rw-r--r-- ajr\/abbot 1199 1996-02-04 12:24 shorten-2.3a\/INSTALL\r\n -rw-r--r-- ajr\/abbot 839 1996-05-29 00:19 shorten-2.3a\/LICENSE\r\n ....<\/pre>\n<h2>\u540d\u79f0\u7a7a\u95f4<\/h2>\n<p>\u7ecf\u5e38\u9700\u8981\u5728\u51fd\u6570\u4e2d\u521b\u5efa\u73af\u5883\u53d8\u91cf\u3002\u867d\u7136\u6709\u53ef\u80fd\uff0c\u4f46\u662f\u66f4\u6709\u4e00\u4e2a\u6280\u672f\u7ec6\u8282\u5e94\u8be5\u4e86\u89e3\u3002\u5728\u5927\u591a\u6570\u7f16\u8bd1\u8bed\u8a00\uff08\u5982 C\uff09\u4e2d\uff0c\u5f53\u5728\u51fd\u6570\u5185\u90e8\u521b\u5efa\u53d8\u91cf\u65f6\uff0c\u53d8\u91cf\u88ab\u653e\u7f6e\u5728\u5355\u72ec\u7684\u5c40\u90e8\u540d\u79f0\u7a7a\u95f4\u4e2d\u3002\u56e0\u6b64\uff0c\u5047\u5982\u5728 C \u4e2d\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a myfunction \u7684\u51fd\u6570\uff0c\u5e76\u5728\u8be5\u51fd\u6570\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a &#8220;x&#8221; \u7684\u81ea\u53d8\u91cf\uff0c\u5219\u4efb\u4f55\u540d\u4e3a &#8220;x&#8221; \u7684\u5168\u5c40\u53d8\u91cf\uff08\u51fd\u6570\u4e4b\u5916\u7684\u53d8\u91cf\uff09\u5c06\u4e0d\u53d7\u4ed6\u7684\u5370\u8c61\uff0c\u4ece\u800c\u6d88\u9664\u4e86\u8d1f\u4f5c\u7528\u3002<br \/>\n\u5728 C \u4e2d\u662f\u8fd9\u6837\uff0c\u4f46\u5728 bash \u4e2d\u5374\u4e0d\u662f\u3002\u5728 bash \u4e2d\uff0c\u6bcf\u5f53\u5728\u51fd\u6570\u5185\u90e8\u521b\u5efa\u73af\u5883\u53d8\u91cf\uff0c\u5c31\u5c06\u5176\u6dfb\u52a0\u5230 \u5168\u5c40\u540d\u79f0\u7a7a\u95f4\u3002\u8fd9\u610f\u5473\u7740\uff0c\u8be5\u53d8\u91cf\u5c06\u91cd\u5199\u51fd\u6570\u4e4b\u5916\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u5728\u51fd\u6570\u9000\u51fa\u4e4b\u540e\u7ee7\u7eed\u5b58\u5728\uff1a<\/p>\n<pre>#!\/usr\/bin\/env bash\r\n myvar=\"hello\"\r\n myfunc() {\r\n myvar=\"one two three\"\r\n for x in $myvar\r\n do\r\n echo $x\r\n done\r\n }\r\n myfunc\r\n echo $myvar $x<\/pre>\n<p>\u8fd0\u884c\u6b64\u811a\u672c\u65f6\uff0c\u4ed6\u5c06\u8f93\u51fa &#8220;one two three three&#8221;\uff0c\u8fd9\u663e\u793a\u4e86\u5728\u51fd\u6570\u4e2d\u5b9a\u4e49\u7684 &#8220;$myvar&#8221; \u5982\u4f55\u5f71\u54cd\u5168\u5c40\u53d8\u91cf &#8220;$myvar&#8221;\uff0c\u8fde\u540c\u5faa\u73af\u63a7\u5236\u53d8\u91cf &#8220;$x&#8221; \u5982\u4f55\u5728\u51fd\u6570\u9000\u51fa\u4e4b\u540e\u7ee7\u7eed\u5b58\u5728\uff08\u5047\u5982 &#8220;$x&#8221; \u5168\u5c40\u53d8\u91cf\u5b58\u5728\uff0c\u4e5f\u5c06\u53d7\u5230\u5f71\u54cd\uff09\u3002<br \/>\n\u5728\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f88\u5bb9\u6613\u627e\u5230\u8be5\u9519\u8bef\uff0c\u5e76\u901a\u8fc7\u4f7f\u7528\u5176\u4ed6\u53d8\u91cf\u540d\u6765\u6539\u6b63\u9519\u8bef\u3002\u4f46\u8fd9\u4e0d\u662f\u6b63\u786e\u7684\u65b9\u6cd5\uff0c\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u6700\u597d\u65b9\u6cd5\u662f\u901a\u8fc7\u4f7f\u7528 &#8220;local&#8221; \u547d\u4ee4\uff0c\u5728\u4e00\u5f00\u59cb\u5c31\u9884\u9632\u5f71\u54cd\u5168\u5c40\u53d8\u91cf\u7684\u53ef\u80fd\u6027\u3002\u5f53\u4f7f\u7528 &#8220;local&#8221; \u5728\u51fd\u6570\u5185\u90e8\u521b\u5efa\u53d8\u91cf\u65f6\uff0c\u5c06\u628a\u4ed6\u4eec\u653e\u5728 \u5c40\u90e8\u540d\u79f0\u7a7a\u95f4\u4e2d\uff0c\u5e76\u4e14\u4e0d\u4f1a\u5f71\u54cd\u4efb\u4f55\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u91cc\u6f14\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u4e0a\u8ff0\u4ee3\u7801\uff0c\u4ee5\u4fbf\u4e0d\u91cd\u5199\u5168\u5c40\u53d8\u91cf\uff1a<\/p>\n<pre>#!\/usr\/bin\/env bash\r\n myvar=\"hello\"\r\n myfunc() {\r\n local x\r\n local myvar=\"one two three\"\r\n for x in $myvar\r\n do\r\n echo $x\r\n done\r\n }\r\n myfunc\r\n echo $myvar $x<\/pre>\n<p>\u6b64\u51fd\u6570\u5c06\u8f93\u51fa &#8220;hello&#8221; &#8212; \u4e0d\u91cd\u5199\u5168\u5c40\u53d8\u91cf &#8220;$myvar&#8221;\uff0c&#8221;$x&#8221; \u5728 myfunc \u4e4b\u5916\u4e0d\u7ee7\u7eed\u5b58\u5728\u3002\u5728\u51fd\u6570\u7684\u7b2c\u4e00\u884c\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4ee5\u540e\u8981\u4f7f\u7528\u7684\u5c40\u90e8\u53d8\u91cf x\uff0c\u800c\u5728\u7b2c\u4e8c\u4e2a\u4f8b\u5b50 (local myvar=&#8221;one two three&#8221;&#8221;) \u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u5c40\u90e8\u53d8\u91cf myvar\uff0c \u540c\u65f6 \u4e3a\u5176\u8d4b\u503c\u3002\u5728\u5c06\u5faa\u73af\u63a7\u5236\u53d8\u91cf\u5b9a\u4e49\u4e3a\u5c40\u90e8\u53d8\u91cf\u65f6\uff0c\u4f7f\u7528\u7b2c\u4e00\u79cd\u5f62\u5f0f\u5f88\u65b9\u4fbf\uff0c\u56e0\u4e3a\u4e0d\u5141\u8bb8\u8bf4\uff1a&#8221;for local x in $myvar&#8221;\u3002\u6b64\u51fd\u6570\u4e0d\u5f71\u54cd\u4efb\u4f55\u5168\u5c40\u53d8\u91cf\uff0c\u9f13\u52b1\u60a8\u7528\u8fd9\u79cd\u65b9\u5f0f\u8bbe\u8ba1\u4efb\u4f55\u7684\u51fd\u6570\u3002\u53ea\u6709\u5728\u660e\u786e\u5e0c\u671b\u8981\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u65f6\uff0c\u624d \u4e0d\u5e94\u8be5\u4f7f\u7528 &#8220;local&#8221;\u3002\u521b\u5efa\u5c40\u90e8\u73af\u5883\u53d8\u91cf\u65f6\u6700\u597d\u4f7f\u7528&#8221;local&#8221;\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u73af\u5883\u53d8\u91cf \u622a\u65ad\u5b57\u7b26\u4e32 \u73af\u5883\u53d8\u91cf \u5728 bash \u4e2d\u5b9a\u4e49\u73af\u5883\u53d8\u91cf\uff1a myvar=&#8217;This is my environment variable!&#8217; \u4ee5\u4e0a\u547d\u4ee4\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a &#8220;myvar&#8221; \u7684\u73af\u5883\u53d8\u91cf\uff0c\u5e76\u5305\u542b\u5b57\u7b26\u4e32 &#8220;This is my environment variable!&#8221;\u3002 \u6709\u51e0\u70b9\u6ce8\u610f\uff1a 1.\u5728\u7b49\u53f7 &#8220;=&#8221; \u7684\u4e24\u8fb9\u6ca1\u6709\u7a7a\u683c\uff1b 2.\u867d\u7136\u5728\u5b9a\u4e49\u4e00\u4e2a\u5b57\u65f6\u80fd\u591f\u7701\u7565\u5f15\u53f7\uff0c\u4f46\u662f\u5f53\u5b9a\u4e49\u7684\u73af\u5883\u53d8\u91cf\u503c\u591a\u4e8e\u4e00\u4e2a\u5b57\u65f6\uff08\u5305\u542b\u7a7a\u683c\u6216tab\uff09\uff0c\u5f15\u53f7\u662f\u5fc5\u987b\u7684\uff1b 3.\u867d\u7136\u901a\u5e38\u80fd\u591f\u7528\u53cc\u5f15\u53f7\u6765\u66ff\u4ee3\u5355\u5f15\u53f7\uff0c\u4f46\u5728\u4e0a\u4f8b\u4e2d\uff0c\u8fd9\u6837\u505a\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u56e0\u4e3a\u4f7f\u7528\u5355\u5f15\u53f7\u7981\u7528\u4e86\u79f0\u4e3a\u6269\u5c55\u7684 bash \u7279\u6027\uff0c\u5176\u4e2d\uff0c\u7279\u522b\u5b57\u7b26\u548c\u5b57\u7b26\u7cfb\u5217\u7531\u503c\u66ff\u6362\u3002\u4f8b\u5982\uff0c&#8221;!&#8221; \u5b57\u7b26\u662f\u5386\u53f2\u6269\u5c55\u5b57\u7b26\uff0cbash \u901a\u5e38\u5c06\u5176\u66ff\u6362\u4e3a\u524d\u9762\u8f93\u5165\u7684\u547d\u4ee4\u3002\u5c3d\u7ba1\u8fd9\u4e2a\u7c7b\u4f3c\u4e8e\u5b8f\u7684\u529f\u80fd\u5f88\u4fbf\u5229\uff0c\u4f46\u6211\u4eec\u73b0\u5728\u53ea\u60f3\u5728\u73af\u5883\u53d8\u91cf\u540e\u9762\u52a0\u4e0a\u4e00\u4e2a\u7b80\u5355\u7684\u611f\u53f9\u53f7\uff0c\u800c\u4e0d\u662f\u5b8f\u3002 \u73af\u5883\u53d8\u91cf\u7684\u4f7f\u7528 $ echo foo$myvarbar bash\u4f1a\u56f0\u60d1\uff0c\u5230\u5e95\u6269\u5c55$m\u3001$my\u3001$myvar\u3001$myvarbar&#8230;\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u8981\u7528\u663e\u5f0f\u7684\u82b1\u62ec\u53f7\u5c06\u4ed6\u62ec\u8d77\u3002 $ echo foo${myvar}bar \u4e00\u5b9a\u8981\u8bb0\u4f4f\uff1a\u5f53\u73af\u5883\u53d8\u91cf\u6ca1\u6709\u7528\u7a7a\u767d\uff08\u7a7a\u683c\u6216\u5236\u8868\u952e\uff09\u548c\u5468\u56f4\u6587\u672c\u5206\u5f00\u65f6\uff0c\u8981\u4f7f\u7528\u66f4\u660e\u786e\u7684\u82b1\u62ec\u53f7\u5f62\u5f0f\u3002 \u5f53\u4e00\u4e2a\u73af\u5883\u53d8\u91cf\u88abexport\u65f6\uff0c\u4ed6\u80fd\u591f\u81ea\u52a8\u5730\u7531\u4ee5\u540e\u8fd0\u884c\u7684\u4efb\u4f55\u811a\u672c\u6216\u53ef\u6267\u884c\u73af\u5883\u4f7f\u7528\u3002shell\u811a\u672c\u80fd\u591f\u4f7f\u7528shell\u7684\u5185\u7f6e\u73af\u5883\u53d8\u91cf\u652f\u6301\u201c\u5230\u8fbe\u201d\u73af\u5883\u53d8\u91cf\uff0c\u800cC\u8bed\u8a00\u80fd\u591f\u4f7f\u7528getenv()\u51fd\u6570\u8c03\u7528\u3002\u5982\u4e0bC\u4ee3\u7801\u793a\u4f8b\uff1a #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main(void) { char *myenvvar=getenv(&#8220;myvar&#8221;); printf(&#8220;The myvar environment variable is %s &#8220;,myenvvar); [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-3127","post","type-post","status-publish","format-standard","hentry","category-life"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/3127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3127"}],"version-history":[{"count":41,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/3127\/revisions"}],"predecessor-version":[{"id":4858,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/3127\/revisions\/4858"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}