{"id":1225,"date":"2014-09-11T16:08:35","date_gmt":"2014-09-11T16:08:35","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1225"},"modified":"2014-09-11T16:08:35","modified_gmt":"2014-09-11T16:08:35","slug":"shell%e6%9d%a1%e4%bb%b6%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1225.html","title":{"rendered":"Shell\u6761\u4ef6\u6d4b\u8bd5"},"content":{"rendered":"<p>\u6700\u8fd1\u548cshell\u811a\u672c\u6253\u4ea4\u9053\u6bd4\u8f83\u591a\uff0c\u867d\u7136\u5f88\u591a\u5185\u5bb9\u4e4b\u524d\u6216\u591a\u6216\u5c11\u90fd\u6709\u6240\u63a5\u89e6\uff0c\u4f46\u65f6\u95f4\u4e00\u4e45\u4e86\u96be\u514d\u6709\u4e9b\u5fd8\u8bb0\u7684\uff0c\u6240\u4ee5\uff0c\u5728\u6b64\u8bb0\u5f55\u4e00\u4e0b\u505a\u4e2a\u5b66\u4e60\/\u5907\u4efd\uff0c\u540c\u65f6\u611f\u8c22\u522b\u4eba\u7684\u70ed\u5fc3\u5206\u4eab\/\u6574\u7406\uff1a<\/p>\n<h5>\u6587\u4ef6\u72b6\u6001\u6d4b\u8bd5<\/h5>\n<table class=\"muse-table\" border=\"2\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>-b filename<\/td>\n<td>\u5f53filename \u5b58\u5728\u5e76\u4e14\u662f\u5757\u6587\u4ef6\u65f6\u8fd4\u56de\u771f(\u8fd4\u56de0)<\/td>\n<\/tr>\n<tr>\n<td>-c filename<\/td>\n<td>\u5f53filename \u5b58\u5728\u5e76\u4e14\u662f\u5b57\u7b26\u6587\u4ef6\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>-d pathname<\/td>\n<td>\u5f53pathname \u5b58\u5728\u5e76\u4e14\u662f\u4e00\u4e2a\u76ee\u5f55\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>-e pathname<\/td>\n<td>\u5f53\u7531pathname \u6307\u5b9a\u7684<strong>\u6587\u4ef6\u6216\u76ee\u5f55\u5b58\u5728\u65f6\u8fd4\u56de\u771f<\/strong><\/td>\n<\/tr>\n<tr>\n<td>-f filename<\/td>\n<td>\u5f53filename <strong>\u5b58\u5728\u5e76\u4e14\u662f\u6b63\u89c4\u6587\u4ef6\u65f6\u8fd4\u56de\u771f<\/strong><\/td>\n<\/tr>\n<tr>\n<td>-g pathname<\/td>\n<td>\u5f53\u7531pathname \u6307\u5b9a\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u5b58\u5728\u5e76\u4e14\u8bbe\u7f6e\u4e86SGID \u4f4d\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>-h filename<\/td>\n<td>\u5f53filename \u5b58\u5728\u5e76\u4e14\u662f\u7b26\u53f7\u94fe\u63a5\u6587\u4ef6\u65f6\u8fd4\u56de\u771f (\u6216 -L filename)<\/td>\n<\/tr>\n<tr>\n<td>-k pathname<\/td>\n<td>\u5f53\u7531pathname \u6307\u5b9a\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u5b58\u5728\u5e76\u4e14\u8bbe\u7f6e\u4e86&#8221;\u7c98\u6ede&#8221;\u4f4d\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>-p filename<\/td>\n<td>\u5f53filename \u5b58\u5728\u5e76\u4e14\u662f\u547d\u540d\u7ba1\u9053\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>-r pathname<\/td>\n<td>\u5f53\u7531pathname \u6307\u5b9a\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u5b58\u5728\u5e76\u4e14\u53ef\u8bfb\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>-s filename<\/td>\n<td><span style=\"color: #ff0000;\"><strong>\u5f53filename \u5b58\u5728\u5e76\u4e14\u6587\u4ef6\u5927\u5c0f\u5927\u4e8e0 \u65f6\u8fd4\u56de\u771f<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td>-S filename<\/td>\n<td>\u5f53filename \u5b58\u5728\u5e76\u4e14\u662fsocket \u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>-t fd<\/td>\n<td>\u5f53fd \u662f\u4e0e\u7ec8\u7aef\u8bbe\u5907\u76f8\u5173\u8054\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>-u pathname<\/td>\n<td>\u5f53\u7531pathname \u6307\u5b9a\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u5b58\u5728\u5e76\u4e14\u8bbe\u7f6e\u4e86SUID \u4f4d\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>-w pathname<\/td>\n<td>\u5f53\u7531pathname \u6307\u5b9a\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u5b58\u5728\u5e76\u4e14\u53ef\u5199\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>-x pathname<\/td>\n<td>\u5f53\u7531pathname \u6307\u5b9a\u7684<span style=\"color: #ff0000;\"><strong>\u6587\u4ef6\u6216\u76ee\u5f55\u5b58\u5728\u5e76\u4e14\u53ef\u6267\u884c\u65f6\u8fd4\u56de\u771f<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td>-O pathname<\/td>\n<td>\u5f53\u7531pathname \u5b58\u5728\u5e76\u4e14\u88ab\u5f53\u524d\u8fdb\u7a0b\u7684\u6709\u6548\u7528\u6237id \u7684\u7528\u6237\u62e5\u6709\u65f6\u8fd4\u56de\u771f(\u5b57\u6bcdO \u5927\u5199)<\/td>\n<\/tr>\n<tr>\n<td>-G pathname<\/td>\n<td>\u5f53\u7531pathname \u5b58\u5728\u5e76\u4e14\u5c5e\u4e8e\u5f53\u524d\u8fdb\u7a0b\u7684\u6709\u6548\u7528\u6237id \u7684\u7528\u6237\u7684\u7528\u6237\u7ec4\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>file1 -nt file2<\/td>\n<td>file1 \u6bd4file2 \u65b0\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>file1 -ot file2<\/td>\n<td>file1 \u6bd4file2 \u65e7\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>f1 -ef f2<\/td>\n<td>files f1 and f2 are hard links to the same file<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"lang:default decode:true\">if [ -b \/dev\/hda ] ;then echo \"yes\" ;else echo \"no\";fi  \/\/ \u5c06\u6253\u5370 yes\n\ntest -c \/dev\/hda ; echo $?  \/\/ \u5c06\u6253\u5370 1 \u8868\u793atest \u547d\u4ee4\u7684\u8fd4\u56de\u503c\u4e3a1\uff0c\/dev\/hda \u4e0d\u662f\u5b57\u7b26\u8bbe\u5907\n\n[ -w \/etc\/passwd ]; echo $?  \/\/ \u67e5\u770b\u5bf9\u5f53\u524d\u7528\u6237\u800c\u8a00\uff0cpasswd \u6587\u4ef6\u662f\u5426\u53ef\u5199<\/pre>\n<h5><a id=\"sec4\" name=\"sec4\"><\/a>\u6d4b\u8bd5\u65f6\u903b\u8f91\u64cd\u4f5c\u7b26<\/h5>\n<table class=\"muse-table\" border=\"2\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>-a<\/td>\n<td>\u903b\u8f91\u4e0e\uff0c\u64cd\u4f5c\u7b26\u4e24\u8fb9\u5747\u4e3a\u771f\uff0c\u7ed3\u679c\u4e3a\u771f\uff0c\u5426\u5219\u4e3a\u5047\u3002<\/td>\n<\/tr>\n<tr>\n<td>-o<\/td>\n<td>\u903b\u8f91\u6216\uff0c\u64cd\u4f5c\u7b26\u4e24\u8fb9\u4e00\u8fb9\u4e3a\u771f\uff0c\u7ed3\u679c\u4e3a\u771f\uff0c\u5426\u5219\u4e3a\u5047\u3002<\/td>\n<\/tr>\n<tr>\n<td>!<\/td>\n<td>\u903b\u8f91\u5426\uff0c\u6761\u4ef6\u4e3a\u5047\uff0c\u7ed3\u679c\u4e3a\u771f\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"lang:default decode:true\">[ -w result.txt -a -w score.txt ] ;echo $?  \/\/ \u6d4b\u8bd5\u4e24\u4e2a\u6587\u4ef6\u662f\u5426\u5747\u53ef\u5199<\/pre>\n<h5><a id=\"sec5\" name=\"sec5\"><\/a>\u5e38\u89c1\u5b57\u7b26\u4e32\u6d4b\u8bd5<\/h5>\n<table class=\"muse-table\" border=\"2\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>-z string<\/td>\n<td><span style=\"color: #ff0000;\"><strong>\u5b57\u7b26\u4e32string \u4e3a\u7a7a\u4e32(\u957f\u5ea6\u4e3a0)\u65f6\u8fd4\u56de\u771f<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td>-n string<\/td>\n<td><span style=\"color: #ff0000;\"><strong>\u5b57\u7b26\u4e32string \u4e3a<span style=\"color: #000000;\">\u975e\u7a7a\u4e32<\/span>\u65f6\u8fd4\u56de\u771f<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td>str1 = str2<\/td>\n<td>\u5b57\u7b26\u4e32str1 \u548c\u5b57\u7b26\u4e32str2 \u76f8\u7b49\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>str1 == str2<\/td>\n<td>\u540c =<\/td>\n<\/tr>\n<tr>\n<td>str1 != str2<\/td>\n<td>\u5b57\u7b26\u4e32str1 \u548c\u5b57\u7b26\u4e32str2 \u4e0d\u76f8\u7b49\u65f6\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>str1 &lt; str2<\/td>\n<td>\u6309\u5b57\u5178\u987a\u5e8f\u6392\u5e8f\uff0c\u5b57\u7b26\u4e32str1 \u5728\u5b57\u7b26\u4e32str2 \u4e4b\u524d<\/td>\n<\/tr>\n<tr>\n<td>str1 &gt; str2<\/td>\n<td>\u6309\u5b57\u5178\u987a\u5e8f\u6392\u5e8f\uff0c\u5b57\u7b26\u4e32str1 \u5728\u5b57\u7b26\u4e32str2 \u4e4b\u540e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"lang:default decode:true\">name=\"zqf\"; [ $name = \"zqf\" ];echo $?  \/\/ \u6253\u5370 0 \u8868\u793a\u53d8\u91cfname \u7684\u503c\u548c\u5b57\u7b26\u4e32\"zqf\"\u76f8\u7b49<\/pre>\n<h5><a id=\"sec6\" name=\"sec6\"><\/a>\u5e38\u89c1\u6570\u503c\u6d4b\u8bd5<\/h5>\n<table class=\"muse-table\" border=\"2\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>int1 -eq int2<\/td>\n<td>\u5982\u679cint1 \u7b49\u4e8eint2\uff0c\u5219\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>int1 -ne int2<\/td>\n<td>\u5982\u679cint1 \u4e0d\u7b49\u4e8eint2\uff0c\u5219\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>int1 -lt int2<\/td>\n<td>\u5982\u679cint1 \u5c0f\u4e8eint2\uff0c\u5219\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>int1 -le int2<\/td>\n<td>\u5982\u679cint1 \u5c0f\u4e8e\u7b49\u4e8eint2\uff0c\u5219\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>int1 -gt int2<\/td>\n<td>\u5982\u679cint1 \u5927\u4e8eint2\uff0c\u5219\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<tr>\n<td>int1 -ge int2<\/td>\n<td>\u5982\u679cint1 \u5927\u4e8e\u7b49\u4e8eint2\uff0c\u5219\u8fd4\u56de\u771f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728 (()) \u4e2d\u7684\u6d4b\u8bd5\uff1a<\/p>\n<table class=\"muse-table\" border=\"2\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td>&lt;<\/td>\n<td>\u5c0f\u4e8e(\u5728\u53cc\u62ec\u53f7\u91cc\u4f7f\u7528)<\/td>\n<td>((&#8220;$a&#8221; &lt; &#8220;$b&#8221;))<\/td>\n<\/tr>\n<tr>\n<td>&lt;=<\/td>\n<td>\u5c0f\u4e8e\u7b49\u4e8e (\u5728\u53cc\u62ec\u53f7\u91cc\u4f7f\u7528)<\/td>\n<td>((&#8220;$a&#8221; &lt;= &#8220;$b&#8221;))<\/td>\n<\/tr>\n<tr>\n<td>&gt;<\/td>\n<td>\u5927\u4e8e (\u5728\u53cc\u62ec\u53f7\u91cc\u4f7f\u7528)<\/td>\n<td>((&#8220;$a&#8221; &gt; &#8220;$b&#8221;))<\/td>\n<\/tr>\n<tr>\n<td>&gt;=<\/td>\n<td>\u5927\u4e8e\u7b49\u4e8e(\u5728\u53cc\u62ec\u53f7\u91cc\u4f7f\u7528)<\/td>\n<td>((&#8220;$a&#8221; &gt;= &#8220;$b&#8221;))<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"lang:default decode:true  \">x=1 ; [ $x -eq 1 ] ; echo $? \/\/ \u5c06\u6253\u5370 0 \u8868\u793a\u53d8\u91cfx \u7684\u503c\u7b49\u4e8e\u6570\u5b571\nx=a ; [ $x -eq \"1\" ] \/\/ shell \u6253\u5370\u9519\u8bef\u4fe1\u606f [: a: integer expression expected<\/pre>\n<h5><a id=\"sec7\" name=\"sec7\"><\/a>test \uff0c [] , [[]]<\/h5>\n<p class=\"first\">\u56e0\u4e3a shell \u548c\u6211\u4eec\u901a\u5e38\u7684\u7f16\u7a0b\u8bed\u8a00\u4e0d\u540c\uff0c\u6240\u4ee5\u6709\u4e9b\u672c\u5c5e\u4e8e\u7a0b\u5e8f\u8bed\u8a00\u672c\u8eab\u7684\u6982\u5ff5\u5728 shell \u4e2d\u4f1a\u96be\u4ee5\u7406\u89e3\u3002&#8221;\u57fa\u672c\u529f&#8221; \u4e0d\u597d\uff0c\u5c31\u66f4\u5bb9\u6613 &#8220;\u72af\u56f0&#8221; \u4e86\u3002<\/p>\n<p>\u4ee5 bash \u4e3a\u4f8b (\u5176\u4ed6\u517c\u5bb9 shell \u5dee\u4e0d\u591a)\uff1a<\/p>\n<ol>\n<li>test \u548c [ \u662f bash \u7684\u5185\u90e8\u547d\u4ee4\uff0cGNU\/linux \u7cfb\u7edf\u7684 coreutils \u8f6f\u4ef6\u5305\u901a \u5e38\u4e5f\u5e26 \/usr\/bin\/test \u548c \/usr\/bin\/[ \u547d\u4ee4\u3002\u5982\u679c\u6211\u4eec\u4e0d\u7528\u7edd\u5bf9\u8def\u5f84\u6307 \u660e\uff0c\u901a\u5e38\u6211\u4eec\u7528\u7684\u90fd\u662f bash \u81ea\u5e26\u7684\u547d\u4ee4\u3002<\/li>\n<li>[[ \u662f bash \u7a0b\u5e8f\u8bed\u8a00\u7684\u5173\u952e\u5b57\uff01<\/li>\n<\/ol>\n<pre class=\"lang:default decode:true example\">$ ls -l \/usr\/bin\/[ \/usr\/bin\/test\n-rwxr-xr-x 1 root root 37400  9\u6708 18 15:25 \/usr\/bin\/[\n-rwxr-xr-x 1 root root 33920  9\u6708 18 15:25 \/usr\/bin\/test\n\n$ type [ [[ test\n[ is a shell builtin\n[[ is a shell keyword\ntest is a shell builtin<\/pre>\n<p>\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e2a\u4e09\u4e2a\u529f\u80fd\u901a\u7528\u3002\u4f46\u662f\u547d\u4ee4\u548c\u5173\u952e\u5b57\u603b\u662f\u6709\u533a\u522b\u7684\u3002\u547d\u4ee4\u548c \u5173\u952e\u5b57\u7684\u5dee\u522b\u6709\u591a\u5927\u5462\uff1f<\/p>\n<p>\u5982\u679c\u662f\u547d\u4ee4\uff0c\u5b83\u5c31\u548c\u53c2\u6570\u7ec4\u5408\u4e3a\u4e00\u4f53\u88ab shell \u89e3\u91ca\uff0c\u90a3\u6837\u6bd4\u5982 &#8220;&gt;&#8221; &#8220;&lt;&#8221; \u5c31\u88ab shell \u89e3\u91ca\u4e3a\u91cd\u5b9a\u5411\u7b26\u53f7\u4e86\u3002\u5173\u952e\u5b57\u5374\u4e0d\u8fd9\u6837\u3002<\/p>\n<h5><a id=\"sec8\" name=\"sec8\"><\/a>\u5728 [[ \u4e2d\u4f7f\u7528 &amp;&amp; \u548c ||<\/h5>\n<p class=\"first\">[ \u4e2d\u4f7f\u7528 -a \u548c -o \u8868\u793a\u903b\u8f91\u4e0e\u548c\u903b\u8f91\u6216\u3002<\/p>\n<h5><a id=\"sec9\" name=\"sec9\"><\/a>[[ \u4e2d\u53ef\u4ee5\u4f7f\u7528\u901a\u914d\u7b26<\/h5>\n<pre class=\"lang:default decode:true example\">arch=i486\n[[ $arch = i*86 ]] &amp;&amp; echo \"arch is x86!\"<\/pre>\n<h5><a id=\"sec10\" name=\"sec10\"><\/a>[[ \u4e2d\u5339\u914d\u5b57\u7b26\u4e32\u6216\u901a\u914d\u7b26\uff0c\u4e0d\u9700\u8981\u5f15\u53f7<\/h5>\n<pre class=\"lang:default decode:true example\">[[ $arch_com = i386 || $ARCH = i*86 ]] &amp;&amp; cat &gt;&gt; $TFS_REPO &lt;&lt;EOF\n[tfs-i386]\nname=GTES11.3 prelim1\nbaseurl=${BASEURL}i386\/\nenabled=1\nEOF<\/pre>\n<p>\u4ee5\u4e0a\u5185\u5bb9\u6765\u81ea\uff1a<a href=\"http:\/\/jianlee.ylinux.org\/Computer\/Shell\/shell_test.html\" target=\"_blank\">Shell \u6761\u4ef6\u6d4b\u8bd5<\/a><\/p>\n<hr \/>\n<h6>1. \u5173\u4e8e\u67d0\u4e2a\u6587\u4ef6\u540d\u7684\u300e\u7c7b\u578b\u300f\u4fa6\u6d4b(\u5b58\u5728\u4e0e\u5426)\uff0c\u5982 test -e filename<\/h6>\n<p>-e \u8be5\u300e\u6587\u4ef6\u540d\u300f\u662f\u5426\u5b58\u5728\uff1f(\u5e38\u7528)<br \/>\n-f \u8be5\u300e\u6587\u4ef6\u540d\u300f\u662f\u5426\u4e3a\u6587\u4ef6(file)\uff1f(\u5e38\u7528)<br \/>\n-d \u8be5\u300e\u6587\u4ef6\u540d\u300f\u662f\u5426\u4e3a\u76ee\u5f55(directory)\uff1f(\u5e38\u7528)<br \/>\n-b \u8be5\u300e\u6587\u4ef6\u540d\u300f\u662f\u5426\u4e3a\u4e00\u4e2a block device \u88c5\u7f6e\uff1f<br \/>\n-c \u8be5\u300e\u6587\u4ef6\u540d\u300f\u662f\u5426\u4e3a\u4e00\u4e2a character device \u88c5\u7f6e\uff1f<br \/>\n-S \u8be5\u300e\u6587\u4ef6\u540d\u300f\u662f\u5426\u4e3a\u4e00\u4e2a Socket \u6587\u4ef6\uff1f<br \/>\n-p \u8be5\u300e\u6587\u4ef6\u540d\u300f\u662f\u5426\u4e3a\u4e00\u4e2a FIFO (pipe) \u6587\u4ef6\uff1f<br \/>\n-L \u8be5\u300e\u6587\u4ef6\u540d\u300f\u662f\u5426\u4e3a\u4e00\u4e2a\u8fde\u7ed3\u6863\uff1f<\/p>\n<h6>2. \u5173\u4e8e\u6587\u4ef6\u7684\u6743\u9650\u4fa6\u6d4b\uff0c\u5982 test -r filename<\/h6>\n<p>-r \u4fa6\u6d4b\u8be5\u6587\u4ef6\u540d\u662f\u5426\u5177\u6709\u300e\u53ef\u8bfb\u300f\u7684\u5c5e\u6027\uff1f<br \/>\n-w \u4fa6\u6d4b\u8be5\u6587\u4ef6\u540d\u662f\u5426\u5177\u6709\u300e\u53ef\u5199\u300f\u7684\u5c5e\u6027\uff1f<br \/>\n-x \u4fa6\u6d4b\u8be5\u6587\u4ef6\u540d\u662f\u5426\u5177\u6709\u300e\u53ef\u6267\u884c\u300f\u7684\u5c5e\u6027\uff1f<br \/>\n-u \u4fa6\u6d4b\u8be5\u6587\u4ef6\u540d\u662f\u5426\u5177\u6709\u300eSUID\u300f\u7684\u5c5e\u6027\uff1f<br \/>\n-g \u4fa6\u6d4b\u8be5\u6587\u4ef6\u540d\u662f\u5426\u5177\u6709\u300eSGID\u300f\u7684\u5c5e\u6027\uff1f<br \/>\n-k \u4fa6\u6d4b\u8be5\u6587\u4ef6\u540d\u662f\u5426\u5177\u6709\u300eSticky bit\u300f\u7684\u5c5e\u6027\uff1f<br \/>\n-s \u4fa6\u6d4b\u8be5\u6587\u4ef6\u540d\u662f\u5426\u4e3a\u300e\u975e\u7a7a\u767d\u6587\u4ef6\u300f\uff1f<\/p>\n<h6>3. \u4e24\u4e2a\u6587\u4ef6\u4e4b\u95f4\u7684\u6bd4\u8f83\uff0c\u5982\uff1a test file1 -nt file2<\/h6>\n<p>-nt (newer than)\u5224\u65ad file1 \u662f\u5426\u6bd4 file2 \u65b0<br \/>\n-ot (older than)\u5224\u65ad file1 \u662f\u5426\u6bd4 file2 \u65e7<br \/>\n-ef \u5224\u65ad file2 \u4e0e file2 \u662f\u5426\u4e3a\u540c\u4e00\u6587\u4ef6\uff0c\u53ef\u7528\u5728\u5224\u65ad hard link \u7684\u5224\u5b9a\u4e0a\u3002 \u4e3b\u8981\u610f\u4e49\u5728\u5224\u5b9a\uff0c\u4e24\u4e2a\u6587\u4ef6\u662f\u5426\u5747\u6307\u5411\u540c\u4e00\u4e2a inode \u54e9\uff01<\/p>\n<h6>4. \u5173\u4e8e\u4e24\u4e2a\u6574\u6570\u4e4b\u95f4\u7684\u5224\u5b9a\uff0c\u4f8b\u5982 test n1 -eq n2<\/h6>\n<p>-eq \u4e24\u6570\u503c\u76f8\u7b49 (equal)<br \/>\n-ne \u4e24\u6570\u503c\u4e0d\u7b49 (not equal)<br \/>\n-gt n1 \u5927\u4e8e n2 (greater than)<br \/>\n-lt n1 \u5c0f\u4e8e n2 (less than)<br \/>\n-ge n1 \u5927\u4e8e\u7b49\u4e8e n2 (greater than or equal)<br \/>\n-le n1 \u5c0f\u4e8e\u7b49\u4e8e n2 (less than or equal)<\/p>\n<h6>5. \u5224\u5b9a\u5b57\u7b26\u4e32\u7684\u6570\u636e<\/h6>\n<p>test -z string \u5224\u5b9a\u5b57\u7b26\u4e32\u662f\u5426\u4e3a 0 \uff1f\u82e5 string \u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u5219\u4e3a true<br \/>\ntest -n string \u5224\u5b9a\u5b57\u7b26\u4e32\u662f\u5426\u975e\u4e3a 0 \uff1f\u82e5 string \u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u5219\u4e3a false\u3002<br \/>\n\u6ce8\uff1a -n \u4ea6\u53ef\u7701\u7565<br \/>\ntest str1 = str2 \u5224\u5b9a str1 \u662f\u5426\u7b49\u4e8e str2 \uff0c\u82e5\u76f8\u7b49\uff0c\u5219\u56de\u4f20 true<br \/>\ntest str1 != str2 \u5224\u5b9a str1 \u662f\u5426\u4e0d\u7b49\u4e8e str2 \uff0c\u82e5\u76f8\u7b49\uff0c\u5219\u56de\u4f20 false<\/p>\n<h6>6. \u591a\u91cd\u6761\u4ef6\u5224\u5b9a\uff0c\u4f8b\u5982\uff1a test -r filename -a -x filename<\/h6>\n<p>-a (and)\u4e24\u72b6\u51b5\u540c\u65f6\u6210\u7acb\uff01\u4f8b\u5982 test -r file -a -x file\uff0c\u5219 file \u540c\u65f6\u5177\u6709 r \u4e0e x \u6743\u9650\u65f6\uff0c\u624d\u56de\u4f20 true\u3002<br \/>\n-o (or)\u4e24\u72b6\u51b5\u4efb\u4f55\u4e00\u4e2a\u6210\u7acb\uff01\u4f8b\u5982 test -r file -o -x file\uff0c\u5219 file \u5177\u6709 r \u6216 x \u6743\u9650\u65f6\uff0c\u5c31\u53ef\u56de\u4f20 true\u3002<br \/>\n! \u53cd\u76f8\u72b6\u6001\uff0c\u5982 test ! -x file \uff0c\u5f53 file \u4e0d\u5177\u6709 x \u65f6\uff0c\u56de\u4f20 true<\/p>\n<h6>\u603b\u7ed3\u4e0b\u6765\uff1a<\/h6>\n<p>\u6761\u4ef6\u6d4b\u8bd5\/test\u547d\u4ee4\u6d4b\u8bd5\u7684\u8868\u8fbe\u5f0f\u7c7b\u578b\u53ef\u5206\u4e3a\u56db\u7c7b\uff1a<\/p>\n<ol>\n<li>\u8868\u8fbe\u5f0f\u5224\u65ad<\/li>\n<li>\u5b57\u7b26\u4e32\u6bd4\u8f83<\/li>\n<li>\u6570\u5b57\u6bd4\u8f83<\/li>\n<li>\u6587\u4ef6\u6bd4\u8f83<\/li>\n<\/ol>\n<h6>\u62d3\u5c55\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/www.ibm.com\/developerworks\/cn\/linux\/l-bash-test.html\" target=\"_blank\">Linux \u6280\u5de7: Bash \u6d4b\u8bd5\u548c\u6bd4\u8f83\u51fd\u6570<\/a><\/li>\n<li><a href=\"http:\/\/vbird.dic.ksu.edu.tw\/linux_basic\/0340bashshell-scripts_3.php\" target=\"_blank\">\u9e1f\u54e5\u7684 Linux \u79c1\u623f\u83dc &#8212; \u5b66\u4e60 Shell Scripts<\/a><\/li>\n<li><a href=\"http:\/\/man.linuxde.net\/test\" target=\"_blank\">test\u547d\u4ee4_Linux test \u547d\u4ee4\u7528\u6cd5\u8be6\u89e3<\/a><\/li>\n<li><a href=\"http:\/\/jianlee.ylinux.org\/Computer\/Shell\/shell_test.html\" target=\"_blank\">Shell \u6761\u4ef6\u6d4b\u8bd5<\/a><\/li>\n<li><a href=\"http:\/\/www.linuxidc.com\/Linux\/2013-05\/84975.htm\" target=\"_blank\">Shell\u6761\u4ef6\u6d4b\u8bd5<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u548cshell\u811a\u672c\u6253\u4ea4\u9053\u6bd4\u8f83\u591a\uff0c\u867d\u7136\u5f88\u591a\u5185\u5bb9\u4e4b\u524d\u6216\u591a\u6216\u5c11\u90fd\u6709\u6240\u63a5\u89e6\uff0c\u4f46\u65f6\u95f4\u4e00\u4e45\u4e86\u96be\u514d\u6709\u4e9b\u5fd8\u8bb0\u7684\uff0c\u6240\u4ee5\uff0c\u5728\u6b64\u8bb0 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11],"tags":[65,329],"class_list":["post-1225","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","tag-shell","tag-test"],"views":4653,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1225"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1225\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}