{"id":96,"date":"2026-07-02T18:57:36","date_gmt":"2026-07-02T10:57:36","guid":{"rendered":"https:\/\/www.wangguihui.com\/?p=96"},"modified":"2026-07-02T18:57:55","modified_gmt":"2026-07-02T10:57:55","slug":"shell-%e6%b5%8b%e8%af%95%e6%9d%a1%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/www.wangguihui.com\/?p=96","title":{"rendered":"Shell \u6761\u4ef6\u6d4b\u8bd5"},"content":{"rendered":"<h3 id=\"shell-\u6d4b\u8bd5\u6761\u4ef6\uff08test-\u547d\u4ee4\u4e0e\u65b9\u62ec\u53f7-\uff09\">Shell \u6d4b\u8bd5\u6761\u4ef6\uff08test \u547d\u4ee4\u4e0e\u65b9\u62ec\u53f7 <code>[]<\/code>\uff09<\/h3>\n<h4 id=\"\u57fa\u672c\u8bed\u6cd5\">\u57fa\u672c\u8bed\u6cd5<\/h4>\n<ul>\n<li>\u4f7f\u7528\u65b9\u62ec\u53f7\u5b9a\u4e49\u6d4b\u8bd5\u6761\u4ef6\uff1a<code>[ \u6761\u4ef6 ]<\/code><\/li>\n<li><strong>\u5173\u952e<\/strong>\uff1a\u7b2c\u4e00\u4e2a\u65b9\u62ec\u53f7\u540e<strong>\u5fc5\u987b<\/strong>\u6709\u7a7a\u683c\uff0c\u7b2c\u4e8c\u4e2a\u65b9\u62ec\u53f7\u524d<strong>\u5fc5\u987b<\/strong>\u6709\u7a7a\u683c\uff0c\u5426\u5219\u62a5\u9519\u3002<\/li>\n<li>\u7b49\u4ef7\u547d\u4ee4\uff1a<code>test \u6761\u4ef6<\/code> \u4e0e <code>[ \u6761\u4ef6 ]<\/code> \u529f\u80fd\u76f8\u540c\u3002<\/li>\n<\/ul>\n<h4 id=\"\u6d4b\u8bd5\u6761\u4ef6\u4e09\u5927\u7c7b\">\u6d4b\u8bd5\u6761\u4ef6\u4e09\u5927\u7c7b<\/h4>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u522b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u6570\u503c\u6bd4\u8f83<\/td>\n<td>\u6bd4\u8f83\u4e24\u4e2a\u6574\u6570\u7684\u5927\u5c0f\u6216\u76f8\u7b49\u5173\u7cfb<\/td>\n<\/tr>\n<tr>\n<td>\u5b57\u7b26\u4e32\u6bd4\u8f83<\/td>\n<td>\u6bd4\u8f83\u5b57\u7b26\u4e32\u5185\u5bb9\u3001\u5927\u5c0f\u987a\u5e8f\uff0c\u6216\u5224\u65ad\u957f\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>\u6587\u4ef6\u6bd4\u8f83<\/td>\n<td>\u68c0\u67e5\u6587\u4ef6\u5c5e\u6027\uff08\u7c7b\u578b\u3001\u6743\u9650\u3001\u6240\u6709\u8005\u3001\u65b0\u65e7\u7b49\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr>\n<h3 id=\"1-\u6587\u4ef6\u6bd4\u8f83\uff08\u5e38\u7528\u9009\u9879\uff09\">1. \u6587\u4ef6\u6bd4\u8f83\uff08\u5e38\u7528\u9009\u9879\uff09<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u9009\u9879<\/th>\n<th>\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>-d file<\/code><\/td>\n<td>\u5b58\u5728\u4e14\u4e3a<strong>\u76ee\u5f55<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>-e file<\/code><\/td>\n<td>\u5b58\u5728\uff08\u4e0d\u8bba\u7c7b\u578b\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>-f file<\/code><\/td>\n<td>\u5b58\u5728\u4e14\u4e3a<strong>\u666e\u901a\u6587\u4ef6<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>-r file<\/code><\/td>\n<td>\u5b58\u5728\u4e14<strong>\u53ef\u8bfb<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>-s file<\/code><\/td>\n<td>\u5b58\u5728\u4e14<strong>\u975e\u7a7a<\/strong>\uff08\u5927\u5c0f &gt; 0\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>-w file<\/code><\/td>\n<td>\u5b58\u5728\u4e14<strong>\u53ef\u5199<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>-x file<\/code><\/td>\n<td>\u5b58\u5728\u4e14<strong>\u53ef\u6267\u884c<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>-O file<\/code><\/td>\n<td>\u5b58\u5728\u4e14\u5c5e<strong>\u5f53\u524d\u7528\u6237<\/strong>\u6240\u6709<\/td>\n<\/tr>\n<tr>\n<td><code>-G file<\/code><\/td>\n<td>\u5b58\u5728\u4e14<strong>\u9ed8\u8ba4\u7ec4<\/strong>\u4e0e\u5f53\u524d\u7528\u6237\u76f8\u540c<\/td>\n<\/tr>\n<tr>\n<td><code>file1 -nt file2<\/code><\/td>\n<td>file1 <strong>\u6bd4<\/strong> file2 <strong>\u65b0<\/strong>\uff08\u4fee\u6539\u65f6\u95f4\u8f83\u65b0\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>file1 -ot file2<\/code><\/td>\n<td>file1 <strong>\u6bd4<\/strong> file2 <strong>\u65e7<\/strong>\uff08\u4fee\u6539\u65f6\u95f4\u8f83\u65e7\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr>\n<h3 id=\"2-\u6570\u503c\u6bd4\u8f83\uff08\u4ec5\u9002\u7528\u4e8e\u6574\u6570\uff09\">2. \u6570\u503c\u6bd4\u8f83\uff08\u4ec5\u9002\u7528\u4e8e\u6574\u6570\uff09<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u9009\u9879<\/th>\n<th>\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>n1 -eq n2<\/code><\/td>\n<td>\u7b49\u4e8e\uff08equal\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>n1 -ge n2<\/code><\/td>\n<td>\u5927\u4e8e\u6216\u7b49\u4e8e\uff08greater or equal\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>n1 -gt n2<\/code><\/td>\n<td>\u5927\u4e8e\uff08greater than\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>n1 -le n2<\/code><\/td>\n<td>\u5c0f\u4e8e\u6216\u7b49\u4e8e\uff08less or equal\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>n1 -lt n2<\/code><\/td>\n<td>\u5c0f\u4e8e\uff08less than\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>n1 -ne n2<\/code><\/td>\n<td>\u4e0d\u7b49\u4e8e\uff08not equal\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u4e0d\u652f\u6301\u6d6e\u70b9\u6570\u6bd4\u8f83\uff0c\u82e5\u9700\u6bd4\u8f83\u6d6e\u70b9\u8bf7\u4f7f\u7528 <code>bc<\/code> \u6216 <code>awk<\/code>\u3002<\/p>\n<\/blockquote>\n<hr>\n<h3 id=\"3-\u5b57\u7b26\u4e32\u6bd4\u8f83\">3. \u5b57\u7b26\u4e32\u6bd4\u8f83<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u9009\u9879 \/ \u5199\u6cd5<\/th>\n<th>\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>str1 = str2<\/code><\/td>\n<td>\u5b57\u7b26\u4e32\u5185\u5bb9<strong>\u76f8\u540c<\/strong>\uff08\u6ce8\u610f\u7b49\u53f7\u4e24\u8fb9\u6709\u7a7a\u683c\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>str1 != str2<\/code><\/td>\n<td>\u5b57\u7b26\u4e32\u5185\u5bb9<strong>\u4e0d\u540c<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>str1 \\&lt; str2<\/code><\/td>\n<td>str1 <strong>\u5c0f\u4e8e<\/strong> str2\uff08\u6309\u5b57\u5178\u5e8f\uff0c\u9700\u8f6c\u4e49 <code>&lt;<\/code> \u4ee5\u9632\u91cd\u5b9a\u5411\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>str1 \\&gt; str2<\/code><\/td>\n<td>str1 <strong>\u5927\u4e8e<\/strong> str2\uff08\u9700\u8f6c\u4e49 <code>&gt;<\/code>\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>-n str1<\/code><\/td>\n<td>\u5b57\u7b26\u4e32<strong>\u957f\u5ea6\u4e0d\u4e3a 0<\/strong>\uff08\u975e\u7a7a\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>-z str1<\/code><\/td>\n<td>\u5b57\u7b26\u4e32<strong>\u957f\u5ea6\u4e3a 0<\/strong>\uff08\u7a7a\u4e32\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote>\n<p><strong>\u63d0\u793a<\/strong>\uff1a\u5728 <code>[ ]<\/code> \u5185\u4f7f\u7528 <code>&lt;<\/code> \u6216 <code>&gt;<\/code> \u65f6\u5fc5\u987b\u52a0\u53cd\u659c\u6760\u8f6c\u4e49\uff0c\u5982 <code>[ &quot;a&quot; \\&lt; &quot;b&quot; ]<\/code>\uff1b\u82e5\u5728 <code>[[ ]]<\/code>\uff08bash \u589e\u5f3a\u6d4b\u8bd5\uff09\u4e2d\u5219\u65e0\u9700\u8f6c\u4e49\u3002<\/p>\n<\/blockquote>\n<hr>\n<h4 id=\"\u4f7f\u7528\u793a\u4f8b\">\u4f7f\u7528\u793a\u4f8b<\/h4>\n<pre><code class=\"language-bash\"># \u6587\u4ef6\u5b58\u5728\u6027\u68c0\u67e5\r\nif [ -e \/etc\/passwd ]; then echo &quot;\u5b58\u5728&quot;; fi\r\n\r\n# \u6570\u503c\u6bd4\u8f83\r\nif [ $num -gt 10 ]; then echo &quot;\u5927\u4e8e10&quot;; fi\r\n\r\n# \u5b57\u7b26\u4e32\u975e\u7a7a\r\nif [ -n &quot;$var&quot; ]; then echo &quot;\u53d8\u91cf\u975e\u7a7a&quot;; fi\r\n\r\n# \u5b57\u7b26\u4e32\u76f8\u7b49\uff08\u6ce8\u610f\u5f15\u53f7\u9632\u7a7a\u503c\uff09\r\nif [ &quot;$str1&quot; = &quot;$str2&quot; ]; then echo &quot;\u76f8\u7b49&quot;; fi\r\n<\/code><\/pre>\n<h4 id=\"\u5e38\u89c1\u9677\u9631\">\u5e38\u89c1\u9677\u9631<\/h4>\n<ul>\n<li><strong>\u7a7a\u683c<\/strong>\uff1a\u65b9\u62ec\u53f7\u5185\u5916\u3001\u64cd\u4f5c\u7b26\u4e24\u4fa7\u90fd\u5fc5\u987b\u6709\u7a7a\u683c\u3002<\/li>\n<li><strong>\u53d8\u91cf\u5f15\u7528<\/strong>\uff1a\u5efa\u8bae\u7528\u53cc\u5f15\u53f7\u5305\u88f9\u53d8\u91cf\uff0c\u907f\u514d\u7a7a\u503c\u6216\u542b\u7a7a\u683c\u65f6\u8bed\u6cd5\u9519\u8bef\u3002<\/li>\n<li><strong>\u8f6c\u4e49<\/strong>\uff1a<code>&lt;<\/code> \u548c <code>&gt;<\/code> \u5728 <code>[ ]<\/code> \u4e2d\u9700\u8f6c\u4e49\uff0c\u6216\u5728 <code>[[ ]]<\/code> \u4e2d\u76f4\u63a5\u4f7f\u7528\uff08bash \u4e13\u5c5e\uff09\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Shell \u6d4b\u8bd5\u6761\u4ef6\uff08test \u547d\u4ee4\u4e0e\u65b9\u62ec\u53f7 []\uff09 \u57fa\u672c\u8bed\u6cd5 \u4f7f\u7528\u65b9\u62ec\u53f7\u5b9a\u4e49\u6d4b\u8bd5\u6761\u4ef6\uff1a[ \u6761\u4ef6 ] \u5173\u952e\uff1a\u7b2c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[33],"tags":[3,9],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-linux","tag-linux","tag-shell"],"_links":{"self":[{"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=96"}],"version-history":[{"count":3,"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":99,"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=\/wp\/v2\/posts\/96\/revisions\/99"}],"wp:attachment":[{"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wangguihui.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}