{"id":1475,"date":"2016-11-24T21:33:49","date_gmt":"2016-11-24T13:33:49","guid":{"rendered":"http:\/\/cn.hostease.com\/xueyuan\/?p=1475"},"modified":"2016-11-24T21:33:49","modified_gmt":"2016-11-24T13:33:49","slug":"shell-%e5%9f%ba%e6%9c%ac%e8%bf%90%e7%ae%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/cn.hostease.com\/xueyuan\/hemanual\/shell-%e5%9f%ba%e6%9c%ac%e8%bf%90%e7%ae%97%e7%ac%a6\/","title":{"rendered":"Shell \u57fa\u672c\u8fd0\u7b97\u7b26"},"content":{"rendered":"<div class=\"article-body\">\n<div id=\"content\" class=\"article-intro\">\n<h1>Shell \u57fa\u672c\u8fd0\u7b97\u7b26<\/h1>\n<p>Shell \u548c\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e00\u6837\uff0c\u652f\u6301\u591a\u79cd\u8fd0\u7b97\u7b26\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u7b97\u6570\u8fd0\u7b97\u7b26<\/li>\n<li>\u5173\u7cfb\u8fd0\u7b97\u7b26<\/li>\n<li>\u5e03\u5c14\u8fd0\u7b97\u7b26<\/li>\n<li>\u5b57\u7b26\u4e32\u8fd0\u7b97\u7b26<\/li>\n<li>\u6587\u4ef6\u6d4b\u8bd5\u8fd0\u7b97\u7b26<\/li>\n<\/ul>\n<p>\u539f\u751fbash\u4e0d\u652f\u6301\u7b80\u5355\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u4f46\u662f\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u547d\u4ee4\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982 awk \u548c expr\uff0cexpr \u6700\u5e38\u7528\u3002<\/p>\n<p>expr \u662f\u4e00\u6b3e\u8868\u8fbe\u5f0f\u8ba1\u7b97\u5de5\u5177\uff0c\u4f7f\u7528\u5b83\u80fd\u5b8c\u6210\u8868\u8fbe\u5f0f\u7684\u6c42\u503c\u64cd\u4f5c\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4e24\u4e2a\u6570\u76f8\u52a0(<strong>\u6ce8\u610f\u4f7f\u7528\u7684\u662f\u53cd\u5f15\u53f7 ` \u800c\u4e0d\u662f\u5355\u5f15\u53f7 &#8216;<\/strong>)\uff1a<\/p>\n<pre class=\"prettyprint\">#!\/bin\/bash\r\n\r\nval=`expr 2 + 2`\r\necho \"\u4e24\u6570\u4e4b\u548c\u4e3a : $val\"\r\n<\/pre>\n<p><a class=\"showbtn\" href=\"https:\/\/www.runoob.com\/try\/runcode.php?filename=add2data&amp;type=bash\" target=\"_blank\">\u8fd0\u884c\u5b9e\u4f8b \u00bb<\/a><\/p>\n<p>\u6267\u884c\u811a\u672c\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"prettyprint\">\u4e24\u6570\u4e4b\u548c\u4e3a : 4\r\n<\/pre>\n<p>\u4e24\u70b9\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u8868\u8fbe\u5f0f\u548c\u8fd0\u7b97\u7b26\u4e4b\u95f4\u8981\u6709\u7a7a\u683c\uff0c\u4f8b\u5982 2+2 \u662f\u4e0d\u5bf9\u7684\uff0c\u5fc5\u987b\u5199\u6210 2 + 2\uff0c\u8fd9\u4e0e\u6211\u4eec\u719f\u6089\u7684\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e0d\u4e00\u6837\u3002<\/li>\n<li>\u5b8c\u6574\u7684\u8868\u8fbe\u5f0f\u8981\u88ab ` ` \u5305\u542b\uff0c\u6ce8\u610f\u8fd9\u4e2a\u5b57\u7b26\u4e0d\u662f\u5e38\u7528\u7684\u5355\u5f15\u53f7\uff0c\u5728 Esc \u952e\u4e0b\u8fb9\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2>\u7b97\u672f\u8fd0\u7b97\u7b26<\/h2>\n<p>\u4e0b\u8868\u5217\u51fa\u4e86\u5e38\u7528\u7684\u7b97\u672f\u8fd0\u7b97\u7b26\uff0c\u5047\u5b9a\u53d8\u91cf a \u4e3a 10\uff0c\u53d8\u91cf b \u4e3a 20\uff1a<\/p>\n<table class=\"reference\">\n<tbody>\n<tr>\n<th>\u8fd0\u7b97\u7b26<\/th>\n<th>\u8bf4\u660e<\/th>\n<th>\u4e3e\u4f8b<\/th>\n<\/tr>\n<tr>\n<td>+<\/td>\n<td>\u52a0\u6cd5<\/td>\n<td>`expr $a + $b` \u7ed3\u679c\u4e3a\u00a030\u3002<\/td>\n<\/tr>\n<tr>\n<td>&#8211;<\/td>\n<td>\u51cf\u6cd5<\/td>\n<td>`expr $a &#8211; $b` \u7ed3\u679c\u4e3a -10\u3002<\/td>\n<\/tr>\n<tr>\n<td>*<\/td>\n<td>\u4e58\u6cd5<\/td>\n<td>`expr $a \\* $b` \u7ed3\u679c\u4e3a \u00a0200\u3002<\/td>\n<\/tr>\n<tr>\n<td>\/<\/td>\n<td>\u9664\u6cd5<\/td>\n<td>`expr $b \/ $a` \u7ed3\u679c\u4e3a\u00a02\u3002<\/td>\n<\/tr>\n<tr>\n<td>%<\/td>\n<td>\u53d6\u4f59<\/td>\n<td>`expr $b % $a` \u7ed3\u679c\u4e3a\u00a00\u3002<\/td>\n<\/tr>\n<tr>\n<td>=<\/td>\n<td>\u8d4b\u503c<\/td>\n<td>a=$b \u5c06\u628a\u53d8\u91cf b \u7684\u503c\u8d4b\u7ed9 a\u3002<\/td>\n<\/tr>\n<tr>\n<td>==<\/td>\n<td>\u76f8\u7b49\u3002\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u6570\u5b57\uff0c\u76f8\u540c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ $a == $b ] \u8fd4\u56de\u00a0false\u3002<\/td>\n<\/tr>\n<tr>\n<td>!=<\/td>\n<td>\u4e0d\u76f8\u7b49\u3002\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u6570\u5b57\uff0c\u4e0d\u76f8\u540c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ $a != $b ] \u8fd4\u56de true\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>\u6ce8\u610f\uff1a<\/b>\u6761\u4ef6\u8868\u8fbe\u5f0f\u8981\u653e\u5728\u65b9\u62ec\u53f7\u4e4b\u95f4\uff0c\u5e76\u4e14\u8981\u6709\u7a7a\u683c\uff0c\u4f8b\u5982: <b>[$a==$b]<\/b> \u662f\u9519\u8bef\u7684\uff0c\u5fc5\u987b\u5199\u6210 <b>[ $a == $b ]<\/b>\u3002<\/p>\n<h3>\u5b9e\u4f8b<\/h3>\n<p>\u7b97\u672f\u8fd0\u7b97\u7b26\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint\">#!\/bin\/bash\r\n# author:\u83dc\u9e1f\u6559\u7a0b\r\n# url:www.runoob.com\r\n\r\na=10\r\nb=20\r\n\r\nval=`expr $a + $b`\r\necho \"a + b : $val\"\r\n\r\nval=`expr $a - $b`\r\necho \"a - b : $val\"\r\n\r\nval=`expr $a \\* $b`\r\necho \"a * b : $val\"\r\n\r\nval=`expr $b \/ $a`\r\necho \"b \/ a : $val\"\r\n\r\nval=`expr $b % $a`\r\necho \"b % a : $val\"\r\n\r\nif [ $a == $b ]\r\nthen\r\n   echo \"a \u7b49\u4e8e b\"\r\nfi\r\nif [ $a != $b ]\r\nthen\r\n   echo \"a \u4e0d\u7b49\u4e8e b\"\r\nfi\r\n<\/pre>\n<p>\u6267\u884c\u811a\u672c\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"prettyprint\">a + b : 30\r\na - b : -10\r\na * b : 200\r\nb \/ a : 2\r\nb % a : 0\r\na \u4e0d\u7b49\u4e8e b\r\n<\/pre>\n<blockquote><p><b>\u6ce8\u610f\uff1a<\/b><\/p>\n<ul>\n<li>\u4e58\u53f7(*)\u524d\u8fb9\u5fc5\u987b\u52a0\u53cd\u659c\u6760(\\)\u624d\u80fd\u5b9e\u73b0\u4e58\u6cd5\u8fd0\u7b97\uff1b<\/li>\n<li>if&#8230;then&#8230;fi \u662f\u6761\u4ef6\u8bed\u53e5\uff0c\u540e\u7eed\u5c06\u4f1a\u8bb2\u89e3\u3002<\/li>\n<li>\u5728 MAC \u4e2d shell \u7684 expr \u8bed\u6cd5\u662f\uff1a<strong>$((\u8868\u8fbe\u5f0f))<\/strong>\uff0c\u6b64\u5904\u8868\u8fbe\u5f0f\u4e2d\u7684 &#8220;*&#8221; \u4e0d\u9700\u8981\u8f6c\u4e49\u7b26\u53f7 &#8220;\\&#8221; \u3002<\/li>\n<\/ul>\n<\/blockquote>\n<hr \/>\n<h2>\u5173\u7cfb\u8fd0\u7b97\u7b26<\/h2>\n<p>\u5173\u7cfb\u8fd0\u7b97\u7b26\u53ea\u652f\u6301\u6570\u5b57\uff0c\u4e0d\u652f\u6301\u5b57\u7b26\u4e32\uff0c\u9664\u975e\u5b57\u7b26\u4e32\u7684\u503c\u662f\u6570\u5b57\u3002<\/p>\n<p>\u4e0b\u8868\u5217\u51fa\u4e86\u5e38\u7528\u7684\u5173\u7cfb\u8fd0\u7b97\u7b26\uff0c\u5047\u5b9a\u53d8\u91cf a \u4e3a 10\uff0c\u53d8\u91cf b \u4e3a 20\uff1a<\/p>\n<table class=\"reference\">\n<tbody>\n<tr>\n<th>\u8fd0\u7b97\u7b26<\/th>\n<th>\u8bf4\u660e<\/th>\n<th>\u4e3e\u4f8b<\/th>\n<\/tr>\n<tr>\n<td>-eq<\/td>\n<td>\u68c0\u6d4b\u4e24\u4e2a\u6570\u662f\u5426\u76f8\u7b49\uff0c\u76f8\u7b49\u8fd4\u56de true\u3002<\/td>\n<td>[ $a -eq $b ] \u8fd4\u56de\u00a0false\u3002<\/td>\n<\/tr>\n<tr>\n<td>-ne<\/td>\n<td>\u68c0\u6d4b\u4e24\u4e2a\u6570\u662f\u5426\u76f8\u7b49\uff0c\u4e0d\u76f8\u7b49\u8fd4\u56de true\u3002<\/td>\n<td>[ $a -ne $b ] \u8fd4\u56de true\u3002<\/td>\n<\/tr>\n<tr>\n<td>-gt<\/td>\n<td>\u68c0\u6d4b\u5de6\u8fb9\u7684\u6570\u662f\u5426\u5927\u4e8e\u53f3\u8fb9\u7684\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ $a -gt $b ] \u8fd4\u56de false\u3002<\/td>\n<\/tr>\n<tr>\n<td>-lt<\/td>\n<td>\u68c0\u6d4b\u5de6\u8fb9\u7684\u6570\u662f\u5426\u5c0f\u4e8e\u53f3\u8fb9\u7684\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ $a -lt $b ] \u8fd4\u56de true\u3002<\/td>\n<\/tr>\n<tr>\n<td>-ge<\/td>\n<td>\u68c0\u6d4b\u5de6\u8fb9\u7684\u6570\u662f\u5426\u5927\u4e8e\u7b49\u4e8e\u53f3\u8fb9\u7684\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ $a -ge $b ] \u8fd4\u56de false\u3002<\/td>\n<\/tr>\n<tr>\n<td>-le<\/td>\n<td>\u68c0\u6d4b\u5de6\u8fb9\u7684\u6570\u662f\u5426\u5c0f\u4e8e\u7b49\u4e8e\u53f3\u8fb9\u7684\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ $a -le $b ] \u8fd4\u56de true\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5b9e\u4f8b<\/h3>\n<p>\u5173\u7cfb\u8fd0\u7b97\u7b26\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint\">#!\/bin\/bash\r\n# author:\u83dc\u9e1f\u6559\u7a0b\r\n# url:www.runoob.com\r\n\r\na=10\r\nb=20\r\n\r\nif [ $a -eq $b ]\r\nthen\r\n   echo \"$a -eq $b : a \u7b49\u4e8e b\"\r\nelse\r\n   echo \"$a -eq $b: a \u4e0d\u7b49\u4e8e b\"\r\nfi\r\nif [ $a -ne $b ]\r\nthen\r\n   echo \"$a -ne $b: a \u4e0d\u7b49\u4e8e b\"\r\nelse\r\n   echo \"$a -ne $b : a \u7b49\u4e8e b\"\r\nfi\r\nif [ $a -gt $b ]\r\nthen\r\n   echo \"$a -gt $b: a \u5927\u4e8e b\"\r\nelse\r\n   echo \"$a -gt $b: a \u4e0d\u5927\u4e8e b\"\r\nfi\r\nif [ $a -lt $b ]\r\nthen\r\n   echo \"$a -lt $b: a \u5c0f\u4e8e b\"\r\nelse\r\n   echo \"$a -lt $b: a \u4e0d\u5c0f\u4e8e b\"\r\nfi\r\nif [ $a -ge $b ]\r\nthen\r\n   echo \"$a -ge $b: a \u5927\u4e8e\u6216\u7b49\u4e8e b\"\r\nelse\r\n   echo \"$a -ge $b: a \u5c0f\u4e8e b\"\r\nfi\r\nif [ $a -le $b ]\r\nthen\r\n   echo \"$a -le $b: a \u5c0f\u4e8e\u6216\u7b49\u4e8e b\"\r\nelse\r\n   echo \"$a -le $b: a \u5927\u4e8e b\"\r\nfi\r\n<\/pre>\n<p>\u6267\u884c\u811a\u672c\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"prettyprint\">10 -eq 20: a \u4e0d\u7b49\u4e8e b\r\n10 -ne 20: a \u4e0d\u7b49\u4e8e b\r\n10 -gt 20: a \u4e0d\u5927\u4e8e b\r\n10 -lt 20: a \u5c0f\u4e8e b\r\n10 -ge 20: a \u5c0f\u4e8e b\r\n10 -le 20: a \u5c0f\u4e8e\u6216\u7b49\u4e8e b\r\n<\/pre>\n<hr \/>\n<h2>\u5e03\u5c14\u8fd0\u7b97\u7b26<\/h2>\n<p>\u4e0b\u8868\u5217\u51fa\u4e86\u5e38\u7528\u7684\u5e03\u5c14\u8fd0\u7b97\u7b26\uff0c\u5047\u5b9a\u53d8\u91cf a \u4e3a 10\uff0c\u53d8\u91cf b \u4e3a 20\uff1a<\/p>\n<table class=\"reference\">\n<tbody>\n<tr>\n<th>\u8fd0\u7b97\u7b26<\/th>\n<th>\u8bf4\u660e<\/th>\n<th>\u4e3e\u4f8b<\/th>\n<\/tr>\n<tr>\n<td>!<\/td>\n<td>\u975e\u8fd0\u7b97\uff0c\u8868\u8fbe\u5f0f\u4e3a true \u5219\u8fd4\u56de false\uff0c\u5426\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ ! false ] \u8fd4\u56de true\u3002<\/td>\n<\/tr>\n<tr>\n<td>-o<\/td>\n<td>\u6216\u8fd0\u7b97\uff0c\u6709\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e3a true \u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ $a -lt 20 -o $b -gt 100 ] \u8fd4\u56de\u00a0true\u3002<\/td>\n<\/tr>\n<tr>\n<td>-a<\/td>\n<td>\u4e0e\u8fd0\u7b97\uff0c\u4e24\u4e2a\u8868\u8fbe\u5f0f\u90fd\u4e3a true \u624d\u8fd4\u56de true\u3002<\/td>\n<td>[ $a -lt 20 -a $b -gt 100 ] \u8fd4\u56de\u00a0false\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5b9e\u4f8b<\/h3>\n<p>\u5e03\u5c14\u8fd0\u7b97\u7b26\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint\">#!\/bin\/bash\r\n# author:\u83dc\u9e1f\u6559\u7a0b\r\n# url:www.runoob.com\r\n\r\na=10\r\nb=20\r\n\r\nif [ $a != $b ]\r\nthen\r\n   echo \"$a != $b : a \u4e0d\u7b49\u4e8e b\"\r\nelse\r\n   echo \"$a != $b: a \u7b49\u4e8e b\"\r\nfi\r\nif [ $a -lt 100 -a $b -gt 15 ]\r\nthen\r\n   echo \"$a -lt 100 -a $b -gt 15 : \u8fd4\u56de true\"\r\nelse\r\n   echo \"$a -lt 100 -a $b -gt 15 : \u8fd4\u56de false\"\r\nfi\r\nif [ $a -lt 100 -o $b -gt 100 ]\r\nthen\r\n   echo \"$a -lt 100 -o $b -gt 100 : \u8fd4\u56de true\"\r\nelse\r\n   echo \"$a -lt 100 -o $b -gt 100 : \u8fd4\u56de false\"\r\nfi\r\nif [ $a -lt 5 -o $b -gt 100 ]\r\nthen\r\n   echo \"$a -lt 100 -o $b -gt 100 : \u8fd4\u56de true\"\r\nelse\r\n   echo \"$a -lt 100 -o $b -gt 100 : \u8fd4\u56de false\"\r\nfi\r\n<\/pre>\n<p>\u6267\u884c\u811a\u672c\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"prettyprint\">10 != 20 : a \u4e0d\u7b49\u4e8e b\r\n10 -lt 100 -a 20 -gt 15 : \u8fd4\u56de true\r\n10 -lt 100 -o 20 -gt 100 : \u8fd4\u56de true\r\n10 -lt 100 -o 20 -gt 100 : \u8fd4\u56de false\r\n<\/pre>\n<hr \/>\n<h2>\u903b\u8f91\u8fd0\u7b97\u7b26<\/h2>\n<p>\u4ee5\u4e0b\u4ecb\u7ecd Shell \u7684\u903b\u8f91\u8fd0\u7b97\u7b26\uff0c\u5047\u5b9a\u53d8\u91cf a \u4e3a 10\uff0c\u53d8\u91cf b \u4e3a 20:<\/p>\n<table class=\"reference\">\n<tbody>\n<tr>\n<th>\u8fd0\u7b97\u7b26<\/th>\n<th>\u8bf4\u660e<\/th>\n<th>\u4e3e\u4f8b<\/th>\n<\/tr>\n<tr>\n<td>&amp;&amp;<\/td>\n<td>\u903b\u8f91\u7684 AND<\/td>\n<td>[[ $a -lt 100 &amp;&amp; $b -gt 100 ]] \u8fd4\u56de false<\/td>\n<\/tr>\n<tr>\n<td>||<\/td>\n<td>\u903b\u8f91\u7684 OR<\/td>\n<td>[[ $a -lt 100 || $b -gt 100 ]] \u8fd4\u56de true<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5b9e\u4f8b<\/h3>\n<p>\u903b\u8f91\u8fd0\u7b97\u7b26\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint\">#!\/bin\/bash\r\n# author:\u83dc\u9e1f\u6559\u7a0b\r\n# url:www.runoob.com\r\n\r\na=10\r\nb=20\r\n\r\nif [[ $a -lt 100 &amp;&amp; $b -gt 100 ]]\r\nthen\r\n   echo \"\u8fd4\u56de true\"\r\nelse\r\n   echo \"\u8fd4\u56de false\"\r\nfi\r\n\r\nif [[ $a -lt 100 || $b -gt 100 ]]\r\nthen\r\n   echo \"\u8fd4\u56de true\"\r\nelse\r\n   echo \"\u8fd4\u56de false\"\r\nfi\r\n<\/pre>\n<p>\u6267\u884c\u811a\u672c\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"prettyprint\">\u8fd4\u56de false\r\n\u8fd4\u56de true\r\n<\/pre>\n<hr \/>\n<h2>\u5b57\u7b26\u4e32\u8fd0\u7b97\u7b26<\/h2>\n<p>\u4e0b\u8868\u5217\u51fa\u4e86\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u8fd0\u7b97\u7b26\uff0c\u5047\u5b9a\u53d8\u91cf a \u4e3a &#8220;abc&#8221;\uff0c\u53d8\u91cf b \u4e3a &#8220;efg&#8221;\uff1a<\/p>\n<table class=\"reference\">\n<tbody>\n<tr>\n<th>\u8fd0\u7b97\u7b26<\/th>\n<th>\u8bf4\u660e<\/th>\n<th>\u4e3e\u4f8b<\/th>\n<\/tr>\n<tr>\n<td>=<\/td>\n<td>\u68c0\u6d4b\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\uff0c\u76f8\u7b49\u8fd4\u56de true\u3002<\/td>\n<td>[ $a = $b ] \u8fd4\u56de false\u3002<\/td>\n<\/tr>\n<tr>\n<td>!=<\/td>\n<td>\u68c0\u6d4b\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\uff0c\u4e0d\u76f8\u7b49\u8fd4\u56de true\u3002<\/td>\n<td>[ $a != $b ] \u8fd4\u56de\u00a0true\u3002<\/td>\n<\/tr>\n<tr>\n<td>-z<\/td>\n<td>\u68c0\u6d4b\u5b57\u7b26\u4e32\u957f\u5ea6\u662f\u5426\u4e3a0\uff0c\u4e3a0\u8fd4\u56de true\u3002<\/td>\n<td>[ -z $a ] \u8fd4\u56de false\u3002<\/td>\n<\/tr>\n<tr>\n<td>-n<\/td>\n<td>\u68c0\u6d4b\u5b57\u7b26\u4e32\u957f\u5ea6\u662f\u5426\u4e3a0\uff0c\u4e0d\u4e3a0\u8fd4\u56de true\u3002<\/td>\n<td>[ -n $a ] \u8fd4\u56de true\u3002<\/td>\n<\/tr>\n<tr>\n<td>str<\/td>\n<td>\u68c0\u6d4b\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a\uff0c\u4e0d\u4e3a\u7a7a\u8fd4\u56de true\u3002<\/td>\n<td>[ $a ] \u8fd4\u56de\u00a0true\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5b9e\u4f8b<\/h3>\n<p>\u5b57\u7b26\u4e32\u8fd0\u7b97\u7b26\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"prettyprint\">#!\/bin\/bash\r\n# author:\u83dc\u9e1f\u6559\u7a0b\r\n# url:www.runoob.com\r\n\r\na=\"abc\"\r\nb=\"efg\"\r\n\r\nif [ $a = $b ]\r\nthen\r\n   echo \"$a = $b : a \u7b49\u4e8e b\"\r\nelse\r\n   echo \"$a = $b: a \u4e0d\u7b49\u4e8e b\"\r\nfi\r\nif [ $a != $b ]\r\nthen\r\n   echo \"$a != $b : a \u4e0d\u7b49\u4e8e b\"\r\nelse\r\n   echo \"$a != $b: a \u7b49\u4e8e b\"\r\nfi\r\nif [ -z $a ]\r\nthen\r\n   echo \"-z $a : \u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a 0\"\r\nelse\r\n   echo \"-z $a : \u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u4e3a 0\"\r\nfi\r\nif [ -n $a ]\r\nthen\r\n   echo \"-n $a : \u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u4e3a 0\"\r\nelse\r\n   echo \"-n $a : \u5b57\u7b26\u4e32\u957f\u5ea6\u4e3a 0\"\r\nfi\r\nif [ $a ]\r\nthen\r\n   echo \"$a : \u5b57\u7b26\u4e32\u4e0d\u4e3a\u7a7a\"\r\nelse\r\n   echo \"$a : \u5b57\u7b26\u4e32\u4e3a\u7a7a\"\r\nfi\r\n<\/pre>\n<p>\u6267\u884c\u811a\u672c\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"prettyprint\">abc = efg: a \u4e0d\u7b49\u4e8e b\r\nabc != efg : a \u4e0d\u7b49\u4e8e b\r\n-z abc : \u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u4e3a 0\r\n-n abc : \u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u4e3a 0\r\nabc : \u5b57\u7b26\u4e32\u4e0d\u4e3a\u7a7a\r\n<\/pre>\n<hr \/>\n<h2>\u6587\u4ef6\u6d4b\u8bd5\u8fd0\u7b97\u7b26<\/h2>\n<p>\u6587\u4ef6\u6d4b\u8bd5\u8fd0\u7b97\u7b26\u7528\u4e8e\u68c0\u6d4b Unix \u6587\u4ef6\u7684\u5404\u79cd\u5c5e\u6027\u3002<\/p>\n<p>\u5c5e\u6027\u68c0\u6d4b\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n<table class=\"reference\">\n<tbody>\n<tr>\n<th>\u64cd\u4f5c\u7b26<\/th>\n<th>\u8bf4\u660e<\/th>\n<th>\u4e3e\u4f8b<\/th>\n<\/tr>\n<tr>\n<td>-b file<\/td>\n<td>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u662f\u5757\u8bbe\u5907\u6587\u4ef6\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ -b $file ] \u8fd4\u56de false\u3002<\/td>\n<\/tr>\n<tr>\n<td>-c file<\/td>\n<td>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u662f\u5b57\u7b26\u8bbe\u5907\u6587\u4ef6\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ -c $file ] \u8fd4\u56de\u00a0false\u3002<\/td>\n<\/tr>\n<tr>\n<td>-d file<\/td>\n<td>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u662f\u76ee\u5f55\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ -d $file ] \u8fd4\u56de false\u3002<\/td>\n<\/tr>\n<tr>\n<td>-f file<\/td>\n<td>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u662f\u666e\u901a\u6587\u4ef6\uff08\u65e2\u4e0d\u662f\u76ee\u5f55\uff0c\u4e5f\u4e0d\u662f\u8bbe\u5907\u6587\u4ef6\uff09\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ -f $file ] \u8fd4\u56de\u00a0true\u3002<\/td>\n<\/tr>\n<tr>\n<td>-g file<\/td>\n<td>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u8bbe\u7f6e\u4e86 SGID \u4f4d\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ -g $file ] \u8fd4\u56de\u00a0false\u3002<\/td>\n<\/tr>\n<tr>\n<td>-k file<\/td>\n<td>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u8bbe\u7f6e\u4e86\u7c98\u7740\u4f4d(Sticky Bit)\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ -k $file ] \u8fd4\u56de\u00a0false\u3002<\/td>\n<\/tr>\n<tr>\n<td>-p file<\/td>\n<td>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u662f\u6709\u540d\u7ba1\u9053\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ -p $file ] \u8fd4\u56de\u00a0false\u3002<\/td>\n<\/tr>\n<tr>\n<td>-u file<\/td>\n<td>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u8bbe\u7f6e\u4e86 SUID \u4f4d\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ -u $file ] \u8fd4\u56de\u00a0false\u3002<\/td>\n<\/tr>\n<tr>\n<td>-r file<\/td>\n<td>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u53ef\u8bfb\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ -r $file ] \u8fd4\u56de\u00a0true\u3002<\/td>\n<\/tr>\n<tr>\n<td>-w file<\/td>\n<td>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u53ef\u5199\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ -w $file ] \u8fd4\u56de\u00a0true\u3002<\/td>\n<\/tr>\n<tr>\n<td>-x file<\/td>\n<td>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u53ef\u6267\u884c\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ -x $file ] \u8fd4\u56de\u00a0true\u3002<\/td>\n<\/tr>\n<tr>\n<td>-s file<\/td>\n<td>\u68c0\u6d4b\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\uff08\u6587\u4ef6\u5927\u5c0f\u662f\u5426\u5927\u4e8e0\uff09\uff0c\u4e0d\u4e3a\u7a7a\u8fd4\u56de true\u3002<\/td>\n<td>[ -s $file ] \u8fd4\u56de\u00a0true\u3002<\/td>\n<\/tr>\n<tr>\n<td>-e file<\/td>\n<td>\u68c0\u6d4b\u6587\u4ef6\uff08\u5305\u62ec\u76ee\u5f55\uff09\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u662f\uff0c\u5219\u8fd4\u56de true\u3002<\/td>\n<td>[ -e $file ] \u8fd4\u56de\u00a0true\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5b9e\u4f8b<\/h3>\n<p>\u53d8\u91cf file \u8868\u793a\u6587\u4ef6&#8221;\/var\/www\/runoob\/test.sh&#8221;\uff0c\u5b83\u7684\u5927\u5c0f\u4e3a100\u5b57\u8282\uff0c\u5177\u6709 rwx \u6743\u9650\u3002\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u5c06\u68c0\u6d4b\u8be5\u6587\u4ef6\u7684\u5404\u79cd\u5c5e\u6027\uff1a<\/p>\n<pre class=\"prettyprint\">#!\/bin\/bash\r\n# author:\u83dc\u9e1f\u6559\u7a0b\r\n# url:www.runoob.com\r\n\r\nfile=\"\/var\/www\/runoob\/test.sh\"\r\nif [ -r $file ]\r\nthen\r\n   echo \"\u6587\u4ef6\u53ef\u8bfb\"\r\nelse\r\n   echo \"\u6587\u4ef6\u4e0d\u53ef\u8bfb\"\r\nfi\r\nif [ -w $file ]\r\nthen\r\n   echo \"\u6587\u4ef6\u53ef\u5199\"\r\nelse\r\n   echo \"\u6587\u4ef6\u4e0d\u53ef\u5199\"\r\nfi\r\nif [ -x $file ]\r\nthen\r\n   echo \"\u6587\u4ef6\u53ef\u6267\u884c\"\r\nelse\r\n   echo \"\u6587\u4ef6\u4e0d\u53ef\u6267\u884c\"\r\nfi\r\nif [ -f $file ]\r\nthen\r\n   echo \"\u6587\u4ef6\u4e3a\u666e\u901a\u6587\u4ef6\"\r\nelse\r\n   echo \"\u6587\u4ef6\u4e3a\u7279\u6b8a\u6587\u4ef6\"\r\nfi\r\nif [ -d $file ]\r\nthen\r\n   echo \"\u6587\u4ef6\u662f\u4e2a\u76ee\u5f55\"\r\nelse\r\n   echo \"\u6587\u4ef6\u4e0d\u662f\u4e2a\u76ee\u5f55\"\r\nfi\r\nif [ -s $file ]\r\nthen\r\n   echo \"\u6587\u4ef6\u4e0d\u4e3a\u7a7a\"\r\nelse\r\n   echo \"\u6587\u4ef6\u4e3a\u7a7a\"\r\nfi\r\nif [ -e $file ]\r\nthen\r\n   echo \"\u6587\u4ef6\u5b58\u5728\"\r\nelse\r\n   echo \"\u6587\u4ef6\u4e0d\u5b58\u5728\"\r\nfi\r\n<\/pre>\n<p>\u6267\u884c\u811a\u672c\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"prettyprint\">\u6587\u4ef6\u53ef\u8bfb\r\n\u6587\u4ef6\u53ef\u5199\r\n\u6587\u4ef6\u53ef\u6267\u884c\r\n\u6587\u4ef6\u4e3a\u666e\u901a\u6587\u4ef6\r\n\u6587\u4ef6\u4e0d\u662f\u4e2a\u76ee\u5f55\r\n\u6587\u4ef6\u4e0d\u4e3a\u7a7a\r\n\u6587\u4ef6\u5b58\u5728\r\n\r\n\r\n\r\n\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/www.runoob.com\/linux\/linux-shell-basic-operators.html\r\n<\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Shell \u57fa\u672c\u8fd0\u7b97\u7b26 Shell \u548c\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u4e00\u6837\uff0c\u652f\u6301\u591a\u79cd\u8fd0\u7b97\u7b26\uff0c\u5305\u62ec\uff1a \u7b97\u6570\u8fd0\u7b97\u7b26 \u5173\u7cfb\u8fd0\u7b97\u7b26 \u5e03\u5c14\u8fd0 &#8230; <a title=\"Shell \u57fa\u672c\u8fd0\u7b97\u7b26\" class=\"read-more\" href=\"https:\/\/cn.hostease.com\/xueyuan\/hemanual\/shell-%e5%9f%ba%e6%9c%ac%e8%bf%90%e7%ae%97%e7%ac%a6\/\" aria-label=\"\u9605\u8bfb Shell \u57fa\u672c\u8fd0\u7b97\u7b26\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,49],"tags":[468],"class_list":["post-1475","post","type-post","status-publish","format-standard","hentry","category-hemanual","category-linux","tag-linuxshell"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/1475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/comments?post=1475"}],"version-history":[{"count":1,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/1475\/revisions"}],"predecessor-version":[{"id":1476,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/posts\/1475\/revisions\/1476"}],"wp:attachment":[{"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/media?parent=1475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/categories?post=1475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn.hostease.com\/xueyuan\/wp-json\/wp\/v2\/tags?post=1475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}