{"id":520,"date":"2025-05-09T23:53:18","date_gmt":"2025-05-09T15:53:18","guid":{"rendered":"https:\/\/www.zackzzp.top\/?p=520"},"modified":"2025-05-09T23:53:37","modified_gmt":"2025-05-09T15:53:37","slug":"linux-%e4%ba%9a%e4%bf%a1","status":"publish","type":"post","link":"http:\/\/www.zackzzp.top\/index.php\/2025\/05\/09\/linux-%e4%ba%9a%e4%bf%a1\/","title":{"rendered":"Linux-\u4e9a\u4fe1"},"content":{"rendered":"<h1 id=\"linux\">Linux<\/h1>\n<h2 id=\"\u6587\u4ef6\u6743\u9650\">\u6587\u4ef6\u6743\u9650<\/h2>\n<p>setuid &#8212; \u4e3a\u6587\u4ef6\u6388\u4e88\u7279\u6b8a\u6743\u9650\uff0c\u5f53\u7528\u6237\u8fd0\u884c\u8be5\u6587\u4ef6\u65f6\u53ef\u4ee5\u4f7f\u7528\u6240\u6709\u8005\u6743\u9650<\/p>\n<p>chown &#8211;\u6388\u4e88\u6587\u4ef6\u6216\u76ee\u5f55\u6743\u9650<\/p>\n<p>4 \u8bfb 2\u5199 1\u6267\u884c<\/p>\n<p>chgrp \u66f4\u6539\u76ee\u5f55\u7ec4<\/p>\n<p>umask \u9884\u5148\u8bbe\u7f6e\u5c4f\u853d\u4f4d\uff0c\u8bbe\u7f6e\u6743\u9650\u65f6\u4f1a\u51cf\u53bbumask\u7684\u503c<\/p>\n<p>sgid \u5c06\u6743\u9650\u63d0\u5347\u81f3\u76ee\u5f55\u6240\u5c5e\u7ec4\uff0c\u65b0\u6587\u4ef6\u4f1a\u5f3a\u5236\u7ee7\u627f\u7ec4 chown filename 2775 2\u4e3asgid\u7684\u6807\u8bc6\u7b26<\/p>\n<h2 id=\"find\u548cxargs\">find\u548cxargs<\/h2>\n<h3 id=\"find\">find<\/h3>\n<p>find \u8def\u5f84 \u5339\u914d\u6761\u4ef6 \u6267\u884c\u52a8\u4f5c<\/p>\n<p>-name \u6309\u540e\u7f00\u540d\u79f0\u5339\u914d<\/p>\n<p>-perm \u6309\u6743\u9650\u5339\u914d<\/p>\n<p>-group \u6309\u7ec4\u540d\u641c\u7d22<\/p>\n<p>-nouser\uff0c-nogroup \u67e5\u627e\u65e0\u6709\u6548\u5c5e\u4e3b\/\u7ec4\u6587\u4ef6<\/p>\n<p>-mtime \u6309\u4fee\u6539\u65f6\u95f4<\/p>\n<p>-newer \u67e5\u627e\u6bd4\u6307\u5b9a\u6587\u4ef6\u66f4\u65b0\u7684\u6587\u4ef6<\/p>\n<p>-type \u6309\u6587\u4ef6\u7c7b\u578b\u641c\u7d22<\/p>\n<p>-size \u6309\u6587\u4ef6\u5927\u5c0f\u641c\u7d22<\/p>\n<h3 id=\"xargs\">xargs<\/h3>\n<p>xargs\u4e3b\u8981\u662f\u63a5\u53d7\u4e0a\u4e00\u6bb5\u6307\u4ee4\u62ff\u5230\u7684\u7ed3\u679c\u4f5c\u4e3a\u53c2\u6570<\/p>\n<p>find \u3002-name &#8220;.tmp&#8221; | xargs rm -f<\/p>\n<h2 id=\"\u540e\u53f0\u6267\u884c\u547d\u4ee4\">\u540e\u53f0\u6267\u884c\u547d\u4ee4<\/h2>\n<h3 id=\"cron\">cron<\/h3>\n<p>cron \u5468\u671f\u6027\u8fd0\u884c\u4efb\u52a1<\/p>\n<p>crontab \u7528\u6765\u81ea\u5b9a\u4e49\u5468\u671f\u4efb\u52a1<\/p>\n<h3 id=\"at\">at<\/h3>\n<p>\u5728\u6307\u5b9a\u65f6\u95f4\u8fd0\u884c<strong>\u4e00\u6b21\u6027<\/strong>\u4efb\u52a1<\/p>\n<h3>&amp;<\/h3>\n<p>&amp;\u5c06\u547d\u4ee4\u653e\u5165\u540e\u53f0\u6267\u884c\uff0c\u4e0d\u5360\u7528\u7ec8\u7aef<\/p>\n<h3 id=\"nohup\">nohup<\/h3>\n<p>\u7528\u6237\u9000\u51fa\u540e\u4ecd\u4fdd\u6301\u8fdb\u7a0b\u8fd0\u884c\uff0c\u9002\u7528\u4e8e\u6301\u4e45\u5316\u4efb\u52a1<\/p>\n<h2 id=\"\u6587\u4ef6\u540d\u7f6e\u6362\u603b\u7ed3\">\u6587\u4ef6\u540d\u7f6e\u6362\u603b\u7ed3<\/h2>\n<p>*\u5339\u914d\u4efb\u610f\u5b57\u7b26\u4e32<\/p>\n<p>\uff1f\u5339\u914d\u5355\u4e2a\u5b57\u7b26<\/p>\n<p>[&#8230;]\u5339\u914d\u62ec\u53f7\u5185\u4efb\u4e00\u5b57\u7b26<\/p>\n<p>[!&#8230;]\u5339\u914d\u4e0d\u5728\u62ec\u53f7\u5185\u7684\u5b57\u7b26<\/p>\n<h2 id=\"shell\u8f93\u51fa\u4e0e\u8f93\u5165\">shell\u8f93\u51fa\u4e0e\u8f93\u5165<\/h2>\n<h3 id=\"\u4e00\u6838\u5fc3\u6982\u5ff5\u4e0e\u547d\u4ee4\">\u4e00\u3001\u6838\u5fc3\u6982\u5ff5\u4e0e\u547d\u4ee4<\/h3>\n<ol start=\"\">\n<li>\n<p>\u6807\u51c6\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef \u2022 \u6807\u51c6\u8f93\u5165\uff08stdin\uff09\uff1a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a\u00a0<code>0<\/code>\uff0c\u9ed8\u8ba4\u6765\u6e90\u662f\u952e\u76d8\u3002<\/p>\n<p>\u2022 \u6807\u51c6\u8f93\u51fa\uff08stdout\uff09\uff1a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a\u00a0<code>1<\/code>\uff0c\u9ed8\u8ba4\u8f93\u51fa\u5230\u5c4f\u5e55\u3002<\/p>\n<p>\u2022 \u6807\u51c6\u9519\u8bef\uff08stderr\uff09\uff1a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e3a\u00a0<code>2<\/code>\uff0c\u9ed8\u8ba4\u8f93\u51fa\u5230\u5c4f\u5e55\uff0c\u7528\u4e8e\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p>\u91cd\u5b9a\u5411 \u901a\u8fc7\u7b26\u53f7\u6539\u53d8\u8f93\u5165\/\u8f93\u51fa\u7684\u6765\u6e90\u6216\u76ee\u6807\uff1a \u2022\u00a0<code>&gt;<\/code>\uff1a\u8986\u76d6\u8f93\u51fa\u5230\u6587\u4ef6\uff08\u5982\u00a0<code>command &gt; file<\/code>\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>&gt;&gt;<\/code>\uff1a\u8ffd\u52a0\u8f93\u51fa\u5230\u6587\u4ef6\uff08\u5982\u00a0<code>command &gt;&gt; file<\/code>\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>2&gt;<\/code>\uff1a\u91cd\u5b9a\u5411\u6807\u51c6\u9519\u8bef\uff08\u5982\u00a0<code>command 2&gt; error.log<\/code>\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>&amp;&gt;<\/code>\u00a0\u6216\u00a0<code>&gt;&amp;<\/code>\uff1a\u5408\u5e76\u6807\u51c6\u8f93\u51fa\u548c\u9519\u8bef\uff08\u5982\u00a0<code>command &amp;&gt; output.log<\/code>\uff09\u3002<\/p>\n<\/li>\n<\/ol>\n<h3 id=\"\u4e8c\u5173\u952e\u547d\u4ee4\u8be6\u89e3\">\u4e8c\u3001\u5173\u952e\u547d\u4ee4\u8be6\u89e3<\/h3>\n<ol start=\"\">\n<li>\n<p><code>echo<\/code>\u00a0\u2022 \u529f\u80fd\uff1a\u663e\u793a\u6587\u672c\u6216\u53d8\u91cf\uff0c\u652f\u6301\u8f6c\u4e49\u5b57\u7b26\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<p>\u25e6\u00a0<code>-e<\/code>\uff1a\u542f\u7528\u8f6c\u4e49\uff08\u5982\u00a0<code>\\n<\/code>\u00a0\u6362\u884c\uff0c<code>\\t<\/code>\u00a0\u5236\u8868\u7b26\uff09\u3002<\/p>\n<p>\u25e6\u00a0<code>-n<\/code>\uff1a\u7981\u6b62\u672b\u5c3e\u6362\u884c\uff08\u5982\u00a0<code>echo -n \"Enter name: \"<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">echo \"Hello\\nWorld\"          # \u8f93\u51fa\u4e24\u884c\uff08\u9700 -e\uff09\necho -e \"Path: \\$PATH\"       # \u663e\u793a\u53d8\u91cf\uff0c\u8f6c\u4e49 $\necho \"Message\" &gt; file.txt    # \u8f93\u51fa\u5230\u6587\u4ef6\n<\/code><\/pre>\n<\/li>\n<li>\n<p><code>read<\/code>\u00a0\u2022 \u529f\u80fd\uff1a\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u6570\u636e\uff0c\u8d4b\u503c\u7ed9\u53d8\u91cf\u3002<\/p>\n<p>\u2022 \u7528\u6cd5\uff1a<\/p>\n<p>\u25e6 \u5355\u53d8\u91cf\u8bfb\u53d6\u5168\u90e8\u8f93\u5165\uff1a<code>read var<\/code>\u3002<\/p>\n<p>\u25e6 \u591a\u53d8\u91cf\u6309\u7a7a\u683c\u5206\u9694\uff1a<code>read var1 var2<\/code>\uff0c\u591a\u4f59\u5185\u5bb9\u8d4b\u7ed9\u6700\u540e\u4e00\u4e2a\u53d8\u91cf\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">read name                    # \u8f93\u5165 \"Alice Bob\"\uff0cname=\"Alice Bob\"\nread first last              # \u8f93\u5165 \"Alice Bob\"\uff0cfirst=\"Alice\", last=\"Bob\"\n<\/code><\/pre>\n<\/li>\n<li>\n<p><code>cat<\/code>\u00a0\u2022 \u529f\u80fd\uff1a\u663e\u793a\u6587\u4ef6\u5185\u5bb9\u3001\u521b\u5efa\u6587\u4ef6\u6216\u5408\u5e76\u6587\u4ef6\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<p>\u25e6\u00a0<code>-v<\/code>\uff1a\u663e\u793a\u63a7\u5236\u5b57\u7b26\uff08\u5982\u00a0<code>^M<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">cat file.txt                 # \u663e\u793a\u6587\u4ef6\u5185\u5bb9\ncat file1 file2 &gt; merged     # \u5408\u5e76\u6587\u4ef6\ncat &gt; newfile &lt;&lt;EOF          # \u4ea4\u4e92\u5f0f\u521b\u5efa\u6587\u4ef6\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u7ba1\u9053\u00a0<code>|<\/code>\u00a0\u2022 \u529f\u80fd\uff1a\u5c06\u524d\u4e00\u4e2a\u547d\u4ee4\u7684\u8f93\u51fa\u4f5c\u4e3a\u540e\u4e00\u4e2a\u547d\u4ee4\u7684\u8f93\u5165\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">ls | grep \".txt\"             # \u5217\u51fa\u6240\u6709.txt\u6587\u4ef6\nwho | awk '{print $1}'       # \u63d0\u53d6\u767b\u5f55\u7528\u6237\u540d\n<\/code><\/pre>\n<\/li>\n<li>\n<p><code>tee<\/code>\u00a0\u2022 \u529f\u80fd\uff1a\u5c06\u8f93\u5165\u540c\u65f6\u8f93\u51fa\u5230\u5c4f\u5e55\u548c\u6587\u4ef6\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<p>\u25e6\u00a0<code>-a<\/code>\uff1a\u8ffd\u52a0\u800c\u975e\u8986\u76d6\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">ls | tee files.log           # \u663e\u793a\u5e76\u4fdd\u5b58\u7ed3\u679c\ncommand | tee -a log.txt     # \u8ffd\u52a0\u5230\u65e5\u5fd7\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3 id=\"\u4e09\u9ad8\u7ea7\u91cd\u5b9a\u5411\u6280\u5de7\">\u4e09\u3001\u9ad8\u7ea7\u91cd\u5b9a\u5411\u6280\u5de7<\/h3>\n<ol start=\"\">\n<li>\n<p>\u5408\u5e76\u6807\u51c6\u8f93\u51fa\u4e0e\u9519\u8bef<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">command &gt; output.log 2&gt;&amp;1      # \u8986\u76d6\u5199\u5165\ncommand &amp;&gt;&gt; output.log         # \u8ffd\u52a0\u5199\u5165\uff08\u7b49\u6548\u4e8e &gt;&gt; output.log 2&gt;&amp;1\uff09\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u8f93\u5165\u91cd\u5b9a\u5411\u4e0e Here Document<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">sort &lt; input.txt               # \u4ece\u6587\u4ef6\u8bfb\u53d6\u8f93\u5165\nmail user &lt;&lt;END                # \u591a\u884c\u8f93\u5165\u76f4\u5230END\nHello, this is a message.\nEND\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u6587\u4ef6\u63cf\u8ff0\u7b26\u64cd\u4f5c \u2022 \u81ea\u5b9a\u4e49\u63cf\u8ff0\u7b26\uff083-9\uff09\uff1a<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">exec 3&lt; input.txt            # \u6253\u5f00\u6587\u4ef6\u63cf\u8ff0\u7b263\u8bfb\u53d6\nread -u 3 line               # \u4ece\u63cf\u8ff0\u7b263\u8bfb\u53d6\nexec 3&lt;&amp;-                    # \u5173\u95ed\u63cf\u8ff0\u7b263\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3 id=\"\u56db\u7279\u6b8a\u573a\u666f\u5904\u7406\">\u56db\u3001\u7279\u6b8a\u573a\u666f\u5904\u7406<\/h3>\n<ol start=\"\">\n<li>\n<p>\u9519\u8bef\u5904\u7406 \u2022 \u5ffd\u7565\u9519\u8bef\uff1a<code>command 2&gt; \/dev\/null<\/code>\u3002<\/p>\n<p>\u2022 \u5206\u79bb\u8f93\u51fa\u4e0e\u9519\u8bef\uff1a<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">command &gt; output.log 2&gt; error.log\n<\/code><\/pre>\n<\/li>\n<li>\n<p><code>exec<\/code>\u00a0\u547d\u4ee4 \u2022 \u66ff\u6362\u5f53\u524dShell\uff1a<code>exec new_command<\/code>\u3002<\/p>\n<p>\u2022 \u64cd\u4f5c\u6587\u4ef6\u63cf\u8ff0\u7b26\uff1a<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">exec 4&gt;&amp;1                   # \u4fdd\u5b58\u6807\u51c6\u8f93\u51fa\u5230\u63cf\u8ff0\u7b264\nexec 1&gt; log.txt             # \u91cd\u5b9a\u5411\u6807\u51c6\u8f93\u51fa\u5230\u6587\u4ef6\necho \"Logging...\"           # \u8f93\u51fa\u5230log.txt\nexec 1&gt;&amp;4                   # \u6062\u590d\u6807\u51c6\u8f93\u51fa\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3 id=\"\u4e94\u6ce8\u610f\u4e8b\u9879\u4e0e\u6280\u5de7\">\u4e94\u3001\u6ce8\u610f\u4e8b\u9879\u4e0e\u6280\u5de7<\/h3>\n<p>\u2022 \u8f6c\u4e49\u5b57\u7b26\uff1a\u5728<code>echo<\/code>\u4e2d\u9700\u7528\u00a0<code>-e<\/code>\u00a0\u542f\u7528\uff08Linux\uff09\uff0c\u53cc\u5f15\u53f7\u9700\u8f6c\u4e49\uff1a<code>echo \"\\\"Hello\\\"\"<\/code>\u3002<\/p>\n<p>\u2022 \u8f93\u5165\u5206\u9694\uff1a<code>read<\/code>\u00a0\u6309\u7a7a\u683c\u5206\u5272\u53d8\u91cf\uff0c\u591a\u4f59\u5185\u5bb9\u5408\u5e76\u5230\u672b\u53d8\u91cf\u3002<\/p>\n<p>\u2022 \u7ba1\u9053\u94fe\uff1a\u53ef\u4e32\u8054\u591a\u4e2a\u547d\u4ee4\uff0c\u5982\u00a0<code>df | awk '{print $1}' | grep -v \"Filesystem\"<\/code>\u3002<\/p>\n<h3 id=\"\u516d\u603b\u7ed3\u56fe\u8868\">\u516d\u3001\u603b\u7ed3\u56fe\u8868<\/h3>\n<figure class=\"table-figure\">\n<table>\n<thead>\n<tr>\n<th>\u64cd\u4f5c<\/th>\n<th>\u547d\u4ee4\u793a\u4f8b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u8f93\u51fa\u91cd\u5b9a\u5411<\/td>\n<td><code>echo \"text\" &gt; file<\/code><\/td>\n<td>\u8986\u76d6\u5199\u5165\u6587\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>\u8ffd\u52a0\u8f93\u51fa<\/td>\n<td><code>echo \"text\" &gt;&gt; file<\/code><\/td>\n<td>\u8ffd\u52a0\u5230\u6587\u4ef6\u672b\u5c3e<\/td>\n<\/tr>\n<tr>\n<td>\u9519\u8bef\u91cd\u5b9a\u5411<\/td>\n<td><code>command 2&gt; error.log<\/code><\/td>\n<td>\u4fdd\u5b58\u9519\u8bef\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td>\u5408\u5e76\u8f93\u51fa\u4e0e\u9519\u8bef<\/td>\n<td><code>command &amp;&gt; output.log<\/code><\/td>\n<td>\u8f93\u51fa\u548c\u9519\u8bef\u5199\u5165\u540c\u4e00\u6587\u4ef6<\/td>\n<\/tr>\n<tr>\n<td>\u591a\u884c\u8f93\u5165\uff08Here Doc\uff09<\/td>\n<td><code>cat &lt;&lt;EOF &gt; file<\/code><\/td>\n<td>\u4ea4\u4e92\u5f0f\u8f93\u5165\u76f4\u5230EOF<\/td>\n<\/tr>\n<tr>\n<td>\u7ba1\u9053<\/td>\n<td><code>ls | grep \".txt\"<\/code><\/td>\n<td>\u00a0<\/td>\n<\/tr>\n<tr>\n<td>\u540c\u65f6\u8f93\u51fa\u4e0e\u4fdd\u5b58<\/td>\n<td><code>ls | tee files.log<\/code><\/td>\n<td>\u00a0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h2 id=\"\u7b2c6\u7ae0\u547d\u4ee4\u6267\u884c\u987a\u5e8f\u603b\u7ed3\">\u7b2c6\u7ae0\u547d\u4ee4\u6267\u884c\u987a\u5e8f\u603b\u7ed3<\/h2>\n<h3 id=\"\u6838\u5fc3\u5185\u5bb9\">\u6838\u5fc3\u5185\u5bb9<\/h3>\n<p>\u672c\u7ae0\u91cd\u70b9\u8bb2\u89e3\u5982\u4f55\u6839\u636e\u524d\u5e8f\u547d\u4ee4\u7684\u6267\u884c\u7ed3\u679c\uff08\u6210\u529f\u6216\u5931\u8d25\uff09\u63a7\u5236\u540e\u7eed\u547d\u4ee4\u7684\u6267\u884c\u903b\u8f91\uff0c\u901a\u8fc7\u64cd\u4f5c\u7b26\u548c\u547d\u4ee4\u7ec4\u5408\u5b9e\u73b0\u6761\u4ef6\u5316\u811a\u672c\u6d41\u7a0b\u3002<\/p>\n<hr \/>\n<h3 id=\"\u4e00\u903b\u8f91\u63a7\u5236\u64cd\u4f5c\u7b26-\u548c-||\"><strong>\u4e00\u3001\u903b\u8f91\u63a7\u5236\u64cd\u4f5c\u7b26\uff1a<code>&amp;&amp;<\/code>\u00a0\u548c\u00a0<code>||<\/code><\/strong><\/h3>\n<ol start=\"\">\n<li>\n<p><strong><code>&amp;&amp;<\/code>\uff08\u903b\u8f91\u4e0e\uff09<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u529f\u80fd<\/strong>\uff1a\u524d\u5e8f\u547d\u4ee4\u6210\u529f\uff08\u9000\u51fa\u72b6\u6001\u7801\u4e3a0\uff09\uff0c\u624d\u6267\u884c\u540e\u7eed\u547d\u4ee4\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8bed\u6cd5<\/strong>\uff1a<code>\u547d\u4ee41 &amp;&amp; \u547d\u4ee42<\/code><\/p>\n<\/li>\n<li>\n<p>\u00a0<\/p>\n<p>\u5e94\u7528\u573a\u666f<\/p>\n<p>\uff1a<\/p>\n<ul>\n<li>\n<p>\u786e\u4fdd\u5173\u952e\u6b65\u9aa4\u6210\u529f\u540e\u518d\u6267\u884c\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<li>\n<p>\u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">cp file.txt backup\/ &amp;&amp; echo \"\u5907\u4efd\u6210\u529f\"  # \u62f7\u8d1d\u6210\u529f\u624d\u8f93\u51fa\u63d0\u793a\nsort data.txt &gt; sorted.txt &amp;&amp; lp sorted.txt  # \u6392\u5e8f\u6210\u529f\u518d\u6253\u5370\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong><code>||<\/code>\uff08\u903b\u8f91\u6216\uff09<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u529f\u80fd<\/strong>\uff1a\u524d\u5e8f\u547d\u4ee4\u5931\u8d25\uff08\u9000\u51fa\u72b6\u6001\u7801\u975e0\uff09\uff0c\u624d\u6267\u884c\u540e\u7eed\u547d\u4ee4\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8bed\u6cd5<\/strong>\uff1a<code>\u547d\u4ee41 || \u547d\u4ee42<\/code><\/p>\n<\/li>\n<li>\n<p>\u00a0<\/p>\n<p>\u5e94\u7528\u573a\u666f<\/p>\n<p>\uff1a<\/p>\n<ul>\n<li>\n<p>\u9519\u8bef\u5904\u7406\u6216\u5907\u7528\u65b9\u6848\u89e6\u53d1\u3002<\/p>\n<\/li>\n<li>\n<p>\u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">comet script.sh || (echo \"\u811a\u672c\u6267\u884c\u5931\u8d25\" | mail admin; exit 1)  # \u5931\u8d25\u5219\u53d1\u90ae\u4ef6\u5e76\u9000\u51fa\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e8c\u547d\u4ee4\u7ec4\u5408-\u4e0e-\"><strong>\u4e8c\u3001\u547d\u4ee4\u7ec4\u5408\uff1a<code>()<\/code>\u00a0\u4e0e\u00a0<code>{}<\/code><\/strong><\/h3>\n<ol start=\"\">\n<li>\n<p><strong>\u5706\u62ec\u53f7\u00a0<code>()<\/code>\uff1a\u5b50Shell\u6267\u884c<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u529f\u80fd<\/strong>\uff1a\u5728\u5b50Shell\u4e2d\u6267\u884c\u7ec4\u5408\u547d\u4ee4\uff0c\u73af\u5883\u53d8\u91cf\u4fee\u6539\u4e0d\u5f71\u54cd\u5f53\u524dShell\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8bed\u6cd5<\/strong>\uff1a<code>(\u547d\u4ee41; \u547d\u4ee42; ...)<\/code><\/p>\n<\/li>\n<li>\n<p>\u00a0<\/p>\n<p>\u793a\u4f8b<\/p>\n<p>\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">(cd \/tmp; ls)  # \u5b50Shell\u4e2d\u5207\u6362\u76ee\u5f55\u5e76\u5217\u51fa\u6587\u4ef6\uff0c\u5f53\u524d\u76ee\u5f55\u4e0d\u53d8\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u82b1\u62ec\u53f7\u00a0<code>{}<\/code>\uff1a\u5f53\u524dShell\u6267\u884c<\/strong><\/p>\n<ul>\n<li>\n<p><strong>\u529f\u80fd<\/strong>\uff1a\u5728\u5f53\u524dShell\u4e2d\u6267\u884c\u7ec4\u5408\u547d\u4ee4\uff0c\u9700\u6ce8\u610f\u683c\u5f0f\uff08\u7a7a\u683c\u548c\u5206\u53f7\uff09\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8bed\u6cd5<\/strong>\uff1a<code>{ \u547d\u4ee41; \u547d\u4ee42; ...; }<\/code><\/p>\n<\/li>\n<li>\n<p>\u00a0<\/p>\n<p>\u793a\u4f8b<\/p>\n<p>\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">{ echo \"\u5f00\u59cb\u65f6\u95f4: $(date)\"; sort data.txt; echo \"\u7ed3\u675f\u65f6\u95f4: $(date)\"; } &gt; log.txt  # \u8bb0\u5f55\u547d\u4ee4\u6267\u884c\u65f6\u95f4\u5230\u65e5\u5fd7\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e09\u7ec4\u5408\u547d\u4ee4\u4e0e\u903b\u8f91\u63a7\u5236\u7ed3\u5408\"><strong>\u4e09\u3001\u7ec4\u5408\u547d\u4ee4\u4e0e\u903b\u8f91\u63a7\u5236\u7ed3\u5408<\/strong><\/h3>\n<ol start=\"\">\n<li>\n<p><strong>\u590d\u6742\u6761\u4ef6\u6267\u884c<\/strong><\/p>\n<ul>\n<li>\n<p>\u901a\u8fc7\u7ec4\u5408\u547d\u4ee4\u548c\u903b\u8f91\u64cd\u4f5c\u7b26\uff0c\u5b9e\u73b0\u591a\u6b65\u9aa4\u6761\u4ef6\u63a7\u5236\u3002<\/p>\n<\/li>\n<li>\n<p>\u00a0<\/p>\n<p>\u793a\u4f8b<\/p>\n<p>\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">sort file.txt &gt; sorted.txt &amp;&amp; { \n    cp sorted.txt \/backup\/ \n    lp sorted.txt \n} || echo \"\u6392\u5e8f\u5931\u8d25\uff0c\u65e0\u6cd5\u6253\u5370\" | mail admin\n<\/code><\/pre>\n<ul>\n<li>\u82e5\u6392\u5e8f\u6210\u529f\uff0c\u5219\u5907\u4efd\u5e76\u6253\u5370\uff1b\u5931\u8d25\u5219\u53d1\u90ae\u4ef6\u901a\u77e5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u5904\u7406\u94fe<\/strong><\/p>\n<ul>\n<li>\n<p>\u7ed3\u5408\u591a\u4e2a<code>&amp;&amp;<\/code>\u548c<code>||<\/code>\u6784\u5efa\u5065\u58ee\u7684\u6d41\u7a0b\u3002<\/p>\n<\/li>\n<li>\n<p>\u00a0<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">make build &amp;&amp; make test || { \n    echo \"\u7f16\u8bd1\u6216\u6d4b\u8bd5\u5931\u8d25\"; \n    exit 1 \n}\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u56db\u5173\u952e\u6ce8\u610f\u4e8b\u9879\"><strong>\u56db\u3001\u5173\u952e\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n<ol start=\"\">\n<li>\n<p><strong>\u9000\u51fa\u72b6\u6001\u7801<\/strong><\/p>\n<ul>\n<li>\u6bcf\u4e2a\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\u7801\u51b3\u5b9a\u903b\u8f91\u64cd\u4f5c\u7b26\u7684\u884c\u4e3a\uff0c\u7ec4\u5408\u547d\u4ee4\u7684\u9000\u51fa\u7801\u4e3a\u6700\u540e\u4e00\u4e2a\u547d\u4ee4\u7684\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5b50Shell\u4e0e\u5f53\u524dShell\u7684\u5f71\u54cd<\/strong><\/p>\n<ul>\n<li>\n<p><code>()<\/code>\u4e2d\u7684\u53d8\u91cf\u4fee\u6539\u4e0d\u5f71\u54cd\u5f53\u524dShell\uff0c<code>{}<\/code>\u4e2d\u7684\u4fee\u6539\u4f1a\u4fdd\u7559\u3002<\/p>\n<\/li>\n<li>\n<p>\u00a0<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">(a=1; echo $a)  # \u8f93\u51fa1\uff0c\u4f46\u5f53\u524dShell\u4e2da\u672a\u5b9a\u4e49\n{ a=1; echo $a; }  # \u8f93\u51fa1\uff0c\u4e14\u5f53\u524dShell\u4e2da=1\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u683c\u5f0f\u89c4\u8303<\/strong><\/p>\n<ul>\n<li><code>{}<\/code>\u9700\u5185\u90e8\u547d\u4ee4\u4ee5\u5206\u53f7\u7ed3\u5c3e\uff0c\u4e14\u62ec\u53f7\u4e0e\u547d\u4ee4\u95f4\u6709\u7a7a\u683c\u3002<\/li>\n<li><strong>\u9519\u8bef\u793a\u4f8b<\/strong>\uff1a<code>{ cmd1 cmd2 }<\/code>\u00a0\u2192 \u6b63\u786e\u5199\u6cd5\uff1a<code>{ cmd1; cmd2; }<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e94\u5e94\u7528\u603b\u7ed3\"><strong>\u4e94\u3001\u5e94\u7528\u603b\u7ed3<\/strong><\/h3>\n<ul>\n<li><strong>\u811a\u672c\u5065\u58ee\u6027<\/strong>\uff1a\u901a\u8fc7\u6761\u4ef6\u6267\u884c\u907f\u514d\u6570\u636e\u4e22\u5931\u6216\u65e0\u6548\u64cd\u4f5c\uff08\u5982\u5220\u9664\u524d\u786e\u8ba4\u5907\u4efd\u6210\u529f\uff09\u3002<\/li>\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u53ca\u65f6\u901a\u77e5\u5931\u8d25\u5e76\u7ec8\u6b62\u6d41\u7a0b\uff08\u5982<code>||<\/code>\u7ed3\u5408<code>exit<\/code>\uff09\u3002<\/li>\n<li><strong>\u590d\u6742\u6d41\u7a0b<\/strong>\uff1a\u7ec4\u5408\u547d\u4ee4\u5b9e\u73b0\u591a\u4efb\u52a1\u539f\u5b50\u64cd\u4f5c\uff08\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u8d44\u6e90\u6e05\u7406\uff09\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u7075\u6d3b\u4f7f\u7528<code>&amp;&amp;<\/code>\u3001<code>||<\/code>\u548c\u547d\u4ee4\u7ec4\u5408\uff0c\u53ef\u7f16\u5199\u51fa\u9ad8\u6548\u3001\u5b89\u5168\u7684Shell\u811a\u672c\uff0c\u7cbe\u786e\u63a7\u5236\u547d\u4ee4\u6267\u884c\u987a\u5e8f\u548c\u9519\u8bef\u5904\u7406\u903b\u8f91\u3002<\/p>\n<h2 id=\"\u6b63\u5219\u8868\u8fbe\u5f0f\u6838\u5fc3\u603b\u7ed3\">\u6b63\u5219\u8868\u8fbe\u5f0f\u6838\u5fc3\u603b\u7ed3<\/h2>\n<h3 id=\"\u4e00\u6b63\u5219\u8868\u8fbe\u5f0f\u7b80\u4ecb\">\u4e00\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b80\u4ecb<\/h3>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08RE\uff09\u662f\u7528\u4e8e\u6587\u672c\u5339\u914d\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u901a\u8fc7\u5143\u5b57\u7b26\u7ec4\u5408\u63cf\u8ff0\u5b57\u7b26\u4e32\u6a21\u5f0f\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8eUNIX\/Linux\u73af\u5883\u4e2d\u7684\u6587\u672c\u8fc7\u6ee4\uff08\u5982<code>grep<\/code>\u3001<code>sed<\/code>\u3001<code>awk<\/code>\uff09\u3002<\/p>\n<hr \/>\n<h3 id=\"\u4e8c\u57fa\u672c\u5143\u5b57\u7b26\u53ca\u7528\u6cd5\">\u4e8c\u3001\u57fa\u672c\u5143\u5b57\u7b26\u53ca\u7528\u6cd5<\/h3>\n<ol start=\"\">\n<li>\n<p>\u884c\u9996\u4e0e\u884c\u5c3e\u5339\u914d \u2022\u00a0<code>^<\/code>\uff1a\u5339\u914d\u884c\u9996\u3002<\/p>\n<p>\u25e6 \u4f8b\uff1a<code>^d<\/code>\u00a0\u5339\u914d\u4ee5<code>d<\/code>\u5f00\u5934\u7684\u884c\uff08\u5982<code>drwxrwxrw-<\/code>\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>$<\/code>\uff1a\u5339\u914d\u884c\u5c3e\u3002<\/p>\n<p>\u25e6 \u4f8b\uff1a<code>trouble$<\/code>\u00a0\u5339\u914d\u4ee5<code>trouble<\/code>\u7ed3\u5c3e\u7684\u884c\uff1b<code>^$<\/code>\u00a0\u5339\u914d\u7a7a\u884c\u3002<\/p>\n<\/li>\n<li>\n<p>\u901a\u914d\u7b26 \u2022\u00a0<code>.<\/code>\uff1a\u5339\u914d\u4efb\u610f\u5355\u4e2a\u5b57\u7b26\u3002<\/p>\n<p>\u25e6 \u4f8b\uff1a<code>beg.n<\/code>\u00a0\u53ef\u5339\u914d<code>begin<\/code>\u3001<code>beg3n<\/code>\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p>\u91cd\u590d\u5339\u914d \u2022\u00a0<code>*<\/code>\uff1a\u5339\u914d\u524d\u4e00\u4e2a\u5b57\u7b26\u7684\u96f6\u6b21\u6216\u591a\u6b21\u51fa\u73b0\u3002<\/p>\n<p>\u25e6 \u4f8b\uff1a<code>compu*t<\/code>\u00a0\u5339\u914d<code>computer<\/code>\u3001<code>compuuuut<\/code>\u7b49\u3002<\/p>\n<p>\u2022\u00a0<code>{}<\/code>\uff1a\u7cbe\u786e\u63a7\u5236\u91cd\u590d\u6b21\u6570\uff08\u9700\u8f6c\u4e49<code>\\<\/code>\uff09\u3002<\/p>\n<p>\u25e6\u00a0<code>pattern\\{n\\}<\/code>\uff1a\u6070\u597d\u51fa\u73b0<code>n<\/code>\u6b21\uff08\u4f8b\uff1a<code>A\\{2\\}<\/code>\u00a0\u5339\u914d<code>AA<\/code>\uff09\u3002<\/p>\n<p>\u25e6\u00a0<code>pattern\\{n,\\}<\/code>\uff1a\u81f3\u5c11\u51fa\u73b0<code>n<\/code>\u6b21\uff08\u4f8b\uff1a<code>A\\{4,\\}<\/code>\u00a0\u5339\u914d<code>AAAA<\/code>\u6216<code>AAAAA<\/code>\uff09\u3002<\/p>\n<p>\u25e6\u00a0<code>pattern\\{n,m\\}<\/code>\uff1a\u51fa\u73b0<code>n<\/code>\u5230<code>m<\/code>\u6b21\uff08\u4f8b\uff1a<code>A\\{2,4\\}<\/code>\u00a0\u5339\u914d<code>AA<\/code>\u3001<code>AAA<\/code>\u3001<code>AAAA<\/code>\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b57\u7b26\u96c6 \u2022\u00a0<code>[]<\/code>\uff1a\u5339\u914d\u62ec\u53f7\u5185\u4efb\u610f\u5355\u4e2a\u5b57\u7b26\uff0c\u652f\u6301\u8303\u56f4\u8868\u793a\uff08\u5982<code>[0-9]<\/code>\uff09\u3002<\/p>\n<p>\u25e6 \u4f8b\uff1a<code>[Cc]omputer<\/code>\u00a0\u5339\u914d<code>Computer<\/code>\u6216<code>computer<\/code>\uff1b<code>[^0-9]<\/code>\u00a0\u5339\u914d\u975e\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<\/li>\n<li>\n<p>\u8f6c\u4e49\u7b26 \u2022\u00a0<code>\\<\/code>\uff1a\u5c4f\u853d\u5143\u5b57\u7b26\u7684\u7279\u6b8a\u542b\u4e49\u3002<\/p>\n<p>\u25e6 \u4f8b\uff1a<code>\\.<\/code>\u00a0\u5339\u914d\u5b57\u9762\u53e5\u70b9\uff1b<code>\\*<\/code>\u00a0\u5339\u914d\u5b57\u9762<code>*<\/code>\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e09\u5e38\u89c1\u5339\u914d\u6a21\u5f0f\u793a\u4f8b\">\u4e09\u3001\u5e38\u89c1\u5339\u914d\u6a21\u5f0f\u793a\u4f8b<\/h3>\n<figure class=\"table-figure\">\n<table>\n<thead>\n<tr>\n<th>\u6a21\u5f0f<\/th>\n<th>\u542b\u4e49<\/th>\n<th>\u793a\u4f8b\u5339\u914d<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>^[the]<\/code><\/td>\n<td>\u4ee5<code>t<\/code>\u3001<code>h<\/code>\u6216<code>e<\/code>\u5f00\u5934\u7684\u884c<\/td>\n<td><code>the start<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>[Ss]ystem\\.<\/code><\/td>\n<td>\u5339\u914d<code>System.<\/code>\u6216<code>system.<\/code><\/td>\n<td><code>System.<\/code>\u3001<code>system.<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>^[A-Za-z]*$<\/code><\/td>\n<td>\u4ec5\u5305\u542b\u5b57\u6bcd\u7684\u884c<\/td>\n<td><code>Hello<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}<\/code><\/td>\n<td>\u65e5\u671f\u683c\u5f0f<code>YYYY-MM-DD<\/code><\/td>\n<td><code>2023-10-05<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>[0-9]\\{3\\}\\.[0-9]\\{3\\}\\.[0-9]\\{3\\}\\.[0-9]\\{3\\}<\/code><\/td>\n<td>IP\u5730\u5740\u683c\u5f0f\uff08<code>nnn.nnn.nnn.nnn<\/code>\uff09<\/td>\n<td><code>192.168.1.1<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>^d..x..x..x<\/code><\/td>\n<td>\u5177\u6709\u53ef\u6267\u884c\u6743\u9650\u7684\u76ee\u5f55\uff08<code>drwxr-xr-x<\/code>\uff09<\/td>\n<td><code>drwxr-xr-x<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>[ou].*t<\/code><\/td>\n<td>\u4ee5<code>o<\/code>\u6216<code>u<\/code>\u5f00\u5934\u3001<code>t<\/code>\u7ed3\u5c3e\u7684\u5355\u8bcd\uff08\u5982<code>shout<\/code>\uff09<\/td>\n<td><code>shout<\/code>\u3001<code>bought<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr \/>\n<h3 id=\"\u56db\u9ad8\u7ea7\u6280\u5de7\u4e0e\u6ce8\u610f\u4e8b\u9879\">\u56db\u3001\u9ad8\u7ea7\u6280\u5de7\u4e0e\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ol start=\"\">\n<li>\u7ec4\u5408\u4f7f\u7528\u5143\u5b57\u7b26 \u2022 \u4f8b\uff1a<code>^...1<\/code>\u00a0\u5339\u914d\u884c\u9996\u7b2c4\u4e2a\u5b57\u7b26\u4e3a<code>1<\/code>\u7684\u5b57\u7b26\u4e32\uff08\u5982<code>0011XA9912<\/code>\uff09\u3002<\/li>\n<li>\u6392\u9664\u5339\u914d \u2022 \u4f7f\u7528<code>[^]<\/code>\u6392\u9664\u5b57\u7b26\u3002\u4f8b\uff1a<code>[^a-z]<\/code>\u00a0\u5339\u914d\u975e\u5c0f\u5199\u5b57\u6bcd\u3002<\/li>\n<li>\u7279\u6b8a\u5b57\u7b26\u8f6c\u4e49 \u2022 \u9700\u8f6c\u4e49\u7684\u5b57\u7b26\u5305\u62ec\uff1a<code>$<\/code>\u3001<code>.<\/code>\u3001<code>*<\/code>\u3001<code>[<\/code>\u3001<code>]<\/code>\u3001<code>^<\/code>\u3001<code>\\<\/code>\u7b49\u3002\u4f8b\uff1a<code>\\$\\$<\/code>\u00a0\u5339\u914d\u5b57\u9762<code>$$<\/code>\u3002<\/li>\n<li>\u5339\u914d\u4efb\u610f\u5b57\u7b26\u4e32 \u2022\u00a0<code>.*<\/code>\u00a0\u8868\u793a\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u4efb\u610f\u5b57\u7b26\uff08\u8d2a\u5a6a\u5339\u914d\uff09\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e94\u5e94\u7528\u573a\u666f\">\u4e94\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u4ee5\u4e0b\u573a\u666f\u4e2d\u5c24\u4e3a\u5173\u952e\uff1a<\/p>\n<ol start=\"\">\n<li>\u8fc7\u6ee4\u65e5\u5fd7\uff1a\u63d0\u53d6\u7279\u5b9a\u683c\u5f0f\u7684\u65e5\u5fd7\u884c\uff08\u5982\u65f6\u95f4\u6233\u3001\u9519\u8bef\u4ee3\u7801\uff09\u3002<\/li>\n<li>\u9a8c\u8bc1\u8f93\u5165\u683c\u5f0f\uff1a\u68c0\u67e5\u65e5\u671f\u3001\u90ae\u7bb1\u3001IP\u5730\u5740\u7b49\u5408\u6cd5\u6027\u3002<\/li>\n<li>\u6279\u91cf\u6587\u672c\u5904\u7406\uff1a\u4f7f\u7528<code>sed<\/code>\u66ff\u6362\u6587\u672c\uff0c<code>awk<\/code>\u63d0\u53d6\u5b57\u6bb5\uff0c<code>grep<\/code>\u641c\u7d22\u6a21\u5f0f\u3002<\/li>\n<\/ol>\n<hr \/>\n<p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u5143\u5b57\u7b26\u548c\u6a21\u5f0f\uff0c\u53ef\u4ee5\u9ad8\u6548\u5904\u7406\u590d\u6742\u7684\u6587\u672c\u5339\u914d\u4efb\u52a1\uff0c\u4e3a\u540e\u7eed\u5b66\u4e60<code>grep<\/code>\u3001<code>sed<\/code>\u548c<code>awk<\/code>\u7b49\u5de5\u5177\u6253\u4e0b\u57fa\u7840\u3002<\/p>\n<h2 id=\"\u7b2c8\u7ae0-grep\u5bb6\u65cf\u8be6\u7ec6\u603b\u7ed3\">\u7b2c8\u7ae0 grep\u5bb6\u65cf\u8be6\u7ec6\u603b\u7ed3<\/h2>\n<h3 id=\"\u4e00grep\u5bb6\u65cf\u6982\u8ff0\">\u4e00\u3001grep\u5bb6\u65cf\u6982\u8ff0<\/h3>\n<p>grep\u662fUNIX\/Linux\u4e2d\u7528\u4e8e\u6587\u672c\u6a21\u5f0f\u5339\u914d\u7684\u6838\u5fc3\u5de5\u5177\uff0c\u5305\u542b\u4e09\u79cd\u53d8\u5f62\uff1a<\/p>\n<ol start=\"\">\n<li>grep\uff1a\u652f\u6301\u57fa\u672c\u6b63\u5219\u8868\u8fbe\u5f0f\uff08BRE\uff09\uff0c\u7528\u4e8e\u5feb\u901f\u6a21\u5f0f\u67e5\u627e\u3002<\/li>\n<li>egrep\uff08\u6269\u5c55grep\uff09\uff1a\u652f\u6301\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\uff08ERE\uff09\uff0c\u63d0\u4f9b\u66f4\u7075\u6d3b\u7684\u6a21\u5f0f\u5339\u914d\uff08\u5982\u00a0<code>|<\/code>\u00a0\u591a\u9009\u5206\u652f\uff09\u3002<\/li>\n<li>fgrep\uff08\u5feb\u901fgrep\uff09\uff1a\u4ec5\u5339\u914d\u56fa\u5b9a\u5b57\u7b26\u4e32\uff0c\u9002\u5408\u65e0\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5feb\u901f\u641c\u7d22\u3002<\/li>\n<\/ol>\n<p>\u6ce8\u610f\uff1aGNU grep\u6574\u5408\u4e86\u4e09\u8005\u529f\u80fd\uff0c\u4f46\u9700\u901a\u8fc7\u9009\u9879\uff08\u5982\u00a0<code>-E<\/code>\u3001<code>-F<\/code>\uff09\u5207\u6362\u6a21\u5f0f\u3002<\/p>\n<hr \/>\n<h3 id=\"\u4e8cgrep\u57fa\u7840\u7528\u6cd5\">\u4e8c\u3001grep\u57fa\u7840\u7528\u6cd5<\/h3>\n<ol start=\"\">\n<li>\u547d\u4ee4\u683c\u5f0f<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">grep [\u9009\u9879] \"\u6a21\u5f0f\" [\u6587\u4ef6]\n<\/code><\/pre>\n<ol start=\"\">\n<li>\u5173\u952e\u9009\u9879 \u2022 -c\uff1a\u7edf\u8ba1\u5339\u914d\u884c\u6570\u3002<\/li>\n<\/ol>\n<p>\u2022 -i\uff1a\u5ffd\u7565\u5927\u5c0f\u5199\u3002<\/p>\n<p>\u2022 -n\uff1a\u663e\u793a\u5339\u914d\u884c\u53ca\u5176\u884c\u53f7\u3002<\/p>\n<p>\u2022 -v\uff1a\u53cd\u5411\u5339\u914d\uff08\u663e\u793a\u4e0d\u5305\u542b\u6a21\u5f0f\u7684\u884c\uff09\u3002<\/p>\n<p>\u2022 -l\/-L\uff1a\u4ec5\u8f93\u51fa\u5305\u542b\/\u4e0d\u5305\u542b\u6a21\u5f0f\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<p>\u2022 -h\uff1a\u591a\u6587\u4ef6\u641c\u7d22\u65f6\u4e0d\u663e\u793a\u6587\u4ef6\u540d\u3002<\/p>\n<p>\u2022 -s\uff1a\u9759\u9ed8\u6a21\u5f0f\uff08\u6291\u5236\u9519\u8bef\u4fe1\u606f\uff09\u3002<\/p>\n<ol start=\"\">\n<li>\u5f15\u53f7\u4f7f\u7528\u89c4\u5219 \u2022 \u53cc\u5f15\u53f7\uff1a\u5305\u88f9\u542b\u7a7a\u683c\u6216\u53d8\u91cf\u7684\u6a21\u5f0f\uff08\u5982\u00a0<code>grep \"$VAR\" file<\/code>\uff09\u3002<\/li>\n<\/ol>\n<p>\u2022 \u5355\u5f15\u53f7\uff1a\u5305\u88f9\u542b\u6b63\u5219\u5143\u5b57\u7b26\u7684\u6a21\u5f0f\uff08\u5982\u00a0<code>grep '^start' file<\/code>\uff09\u3002<\/p>\n<ol start=\"\">\n<li>\u591a\u6587\u4ef6\u641c\u7d22<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">grep \"pattern\" *.txt       # \u5f53\u524d\u76ee\u5f55\u6240\u6709.txt\u6587\u4ef6\ngrep \"sort it\" *           # \u6240\u6709\u6587\u4ef6\u4e2d\u5339\u914d\"sort it\"\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u4e09\u6b63\u5219\u8868\u8fbe\u5f0f\u6280\u5de7\">\u4e09\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u6280\u5de7<\/h3>\n<ol start=\"\">\n<li>\u7cbe\u786e\u5339\u914d \u2022 \u5355\u8bcd\u8fb9\u754c\uff1a\u4f7f\u7528\u00a0<code>\\&lt;<\/code>\u00a0\u548c\u00a0<code>\\&gt;<\/code>\uff08\u6216\u00a0<code>\\b<\/code>\uff09\u3002<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">grep '\\&lt;48\\&gt;' data.f     # \u7cbe\u786e\u5339\u914d\"48\"\u800c\u975e\"483\"\n<\/code><\/pre>\n<ol start=\"\">\n<li>\u884c\u9996\/\u884c\u5c3e\u5339\u914d \u2022\u00a0<code>^<\/code>\u00a0\u5339\u914d\u884c\u9996\uff0c<code>$<\/code>\u00a0\u5339\u914d\u884c\u5c3e\u3002<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">grep '^48' data.f        # \u884c\u9996\u4e3a48\u7684\u884c\ngrep 'end$' file         # \u884c\u5c3e\u4e3a\"end\"\u7684\u884c\n<\/code><\/pre>\n<ol start=\"\">\n<li>\u5b57\u7b26\u8303\u56f4\u4e0e\u7c7b \u2022 \u8303\u56f4\uff1a<code>[0-9]<\/code>\u00a0\u5339\u914d\u6570\u5b57\uff0c<code>[A-Za-z]<\/code>\u00a0\u5339\u914d\u5b57\u6bcd\u3002<\/li>\n<\/ol>\n<p>\u2022 \u9884\u5b9a\u4e49\u7c7b\u540d\uff08\u9700\u53cc\u62ec\u53f7\uff09\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">grep '[[:upper:]]' file  # \u5339\u914d\u5927\u5199\u5b57\u6bcd\uff08\u7b49\u4ef7[A-Z]\uff09\ngrep '5[[:upper:]]\\{2\\}' data.f  # 5\u540e\u8ddf\u4e24\u4e2a\u5927\u5199\u5b57\u6bcd\uff08\u59825AP\uff09\n<\/code><\/pre>\n<ol start=\"\">\n<li>\u91cd\u590d\u4e0e\u6b21\u6570 \u2022\u00a0<code>\\{n\\}<\/code>\u00a0\u7cbe\u786e\u91cd\u590dn\u6b21\uff0c<code>\\{n,\\}<\/code>\u00a0\u81f3\u5c11n\u6b21\uff0c<code>\\{n,m\\}<\/code>\u00a0\u91cd\u590dn\u5230m\u6b21\u3002<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">grep '9\\{3\\}' data.f     # \u5339\u914d\"999\"\ngrep '8\\{2,6\\}3' file    # 8\u91cd\u590d2-6\u6b21\u540e\u63a53\uff08\u59828883\uff09\n<\/code><\/pre>\n<ol start=\"\">\n<li>\u7279\u6b8a\u5b57\u7b26\u8f6c\u4e49 \u2022 \u4f7f\u7528\u00a0<code>\\<\/code>\u00a0\u8f6c\u4e49\u00a0<code>.<\/code>\u3001<code>*<\/code>\u3001<code>$<\/code>\u00a0\u7b49\u5143\u5b57\u7b26\u3002<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">grep '\\.' file           # \u5339\u914d\u542b\u53e5\u70b9\u7684\u884c\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u56db\u9ad8\u7ea7\u5e94\u7528\u6848\u4f8b\">\u56db\u3001\u9ad8\u7ea7\u5e94\u7528\u6848\u4f8b<\/h3>\n<ol start=\"\">\n<li>\u65e5\u671f\u4e0eIP\u5339\u914d \u2022 \u65e5\u671f\u683c\u5f0f\uff08\u5982YYYY\u6216\u5e74\u4efd\u672b\u4e24\u4f4d\uff09\uff1a<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">grep '199[68]' data.f    # \u5339\u914d1996\u62161998\n<\/code><\/pre>\n<p>\u2022 IP\u5730\u5740\u5339\u914d\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">grep '[0-9]\\{3\\}\\.[0-9]\\{3\\}\\.' ipfile  # \u5339\u914dnnn.nnn.\u683c\u5f0f\u7684IP\n<\/code><\/pre>\n<ol start=\"\">\n<li>\u7ed3\u5408\u7cfb\u7edf\u547d\u4ee4 \u2022 \u67e5\u627e\u8fdb\u7a0b\uff1a<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">ps ax | grep 'named' | grep -v \"grep\"   # \u6392\u9664grep\u81ea\u8eab\u8fdb\u7a0b\n<\/code><\/pre>\n<p>\u2022 \u7528\u6237\u67e5\u8be2\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">who | egrep 'louise|pauline'            # \u5339\u914d\u591a\u4e2a\u7528\u6237\n<\/code><\/pre>\n<ol start=\"\">\n<li>\u591a\u6a21\u5f0f\u6587\u4ef6\uff08egrep -f\uff09 \u2022 \u521b\u5efa\u6a21\u5f0f\u6587\u4ef6\u00a0<code>grep_patterns.txt<\/code>\uff1a<\/li>\n<\/ol>\n<p>plaintext<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-plaintext\" lang=\"plaintext\">484\n47\n<\/code><\/pre>\n<p>\u2022 \u6267\u884c\u591a\u6a21\u5f0f\u641c\u7d22\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">egrep -f grep_patterns.txt data.f\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u4e94egrep\u6269\u5c55\u529f\u80fd\">\u4e94\u3001egrep\u6269\u5c55\u529f\u80fd<\/h3>\n<p>\u2022 \u591a\u9009\u5206\u652f\u00a0<code>|<\/code>\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">egrep '(3ZL|2CC)' data.f  # \u5339\u914d3ZL\u62162CC\n<\/code><\/pre>\n<p>\u2022 \u53ef\u9009\u5b57\u7b26\u00a0<code>?<\/code>\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">egrep 'reboot(s)?' *      # \u5339\u914d\"reboot\"\u6216\"reboots\"\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u516d\u5b9e\u7528\u573a\u666f\u603b\u7ed3\">\u516d\u3001\u5b9e\u7528\u573a\u666f\u603b\u7ed3<\/h3>\n<ol start=\"\">\n<li>\u65e5\u5fd7\u5206\u6790\uff1a\u5feb\u901f\u5b9a\u4f4d\u9519\u8bef\u4fe1\u606f\uff08\u5982\u00a0<code>grep \"ERROR\" app.log<\/code>\uff09\u3002<\/li>\n<li>\u914d\u7f6e\u68c0\u67e5\uff1a\u641c\u7d22\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u53c2\u6570\uff08\u5982\u00a0<code>grep \"Port\" \/etc\/ssh\/sshd_config<\/code>\uff09\u3002<\/li>\n<li>\u6570\u636e\u6e05\u6d17\uff1a\u63d0\u53d6\u7279\u5b9a\u683c\u5f0f\u6570\u636e\uff08\u5982IP\u3001\u65e5\u671f\uff09\u3002<\/li>\n<li>\u8fdb\u7a0b\u7ba1\u7406\uff1a\u7ed3\u5408\u00a0<code>ps<\/code>\u00a0\u76d1\u63a7\u670d\u52a1\u72b6\u6001\u3002<\/li>\n<li>\u7528\u6237\u7ba1\u7406\uff1a\u5728\u00a0<code>\/etc\/passwd<\/code>\u00a0\u4e2d\u67e5\u627e\u7528\u6237\u4fe1\u606f\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e03\u6ce8\u610f\u4e8b\u9879\">\u4e03\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<p>\u2022 \u6027\u80fd\uff1a\u907f\u514d\u8fc7\u4e8e\u5bbd\u6cdb\u7684\u6a21\u5f0f\uff08\u5982\u00a0<code>.*<\/code>\uff09\uff0c\u4f18\u5148\u7cbe\u786e\u5339\u914d\u3002<\/p>\n<p>\u2022 \u8de8\u5e73\u53f0\u5dee\u5f02\uff1a\u4e0d\u540c\u7cfb\u7edf\uff08\u5982BSD\/macOS\u4e0eLinux\uff09\u7684grep\u9009\u9879\u53ef\u80fd\u4e0d\u540c\u3002<\/p>\n<p>\u2022 \u6b63\u5219\u8868\u8fbe\u5f0f\u7c7b\u578b\uff1a\u660e\u786e\u4f7f\u7528BRE\uff08\u9ed8\u8ba4\uff09\u6216ERE\uff08<code>-E<\/code>\uff09\uff0c\u907f\u514d\u8bed\u6cd5\u6df7\u6dc6\u3002<\/p>\n<p>\u901a\u8fc7\u7075\u6d3b\u7ed3\u5408grep\u5bb6\u65cf\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u9ad8\u6548\u5904\u7406\u6587\u672c\u5904\u7406\u4efb\u52a1\uff0c\u6210\u4e3aShell\u811a\u672c\u548c\u65e5\u5e38\u7cfb\u7edf\u7ba1\u7406\u7684\u5229\u5668\u3002<\/p>\n<h2 id=\"awk-\u7b2c9\u7ae0\u5185\u5bb9\u603b\u7ed3\">AWK \u7b2c9\u7ae0\u5185\u5bb9\u603b\u7ed3<\/h2>\n<h3 id=\"1-awk\u7b80\u4ecb\"><strong>1. AWK\u7b80\u4ecb<\/strong><\/h3>\n<p>\u2022 \u7528\u9014\uff1aAWK\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u683c\u5f0f\u5316\u62a5\u6587\u3001\u62bd\u53d6\u6570\u636e\uff0c\u64c5\u957f\u6587\u672c\u6d4f\u89c8\u548c\u6570\u636e\u64cd\u4f5c\u3002<\/p>\n<p>\u2022 \u7279\u70b9\uff1a\u8bed\u6cd5\u8f83\u590d\u6742\uff0c\u4f46\u7ed3\u5408grep\u3001sed\u7b49\u5de5\u5177\u53ef\u63d0\u5347Shell\u7f16\u7a0b\u6548\u7387\u3002\u652f\u6301\u81ea\u89e3\u91ca\u7684\u7f16\u7a0b\u8bed\u8a00\u7279\u6027\u3002<\/p>\n<hr \/>\n<h3 id=\"2-\u8c03\u7528awk\u7684\u65b9\u5f0f\"><strong>2. \u8c03\u7528AWK\u7684\u65b9\u5f0f<\/strong><\/h3>\n<p>\u2022 \u547d\u4ee4\u884c\u8c03\u7528\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">awk [-F \u5206\u9694\u7b26] '\u547d\u4ee4' \u8f93\u5165\u6587\u4ef6\n<\/code><\/pre>\n<p>\u2022\u00a0<code>-F<\/code>\u6307\u5b9a\u57df\u5206\u9694\u7b26\uff08\u9ed8\u8ba4\u7a7a\u683c\uff09\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a\u5904\u7406<code>passwd<\/code>\u6587\u4ef6\uff08\u5192\u53f7\u5206\u9694\uff09\uff1a<\/p>\n<pre><code>```bash\nawk -F: 'commands' \/etc\/passwd\n```\n<\/code><\/pre>\n<p>\u2022 \u811a\u672c\u6587\u4ef6\u8c03\u7528\uff1a<\/p>\n<p>\u2022 \u5c06AWK\u547d\u4ee4\u5199\u5165\u6587\u4ef6\uff0c\u9996\u884c\u6dfb\u52a0<code>#!\/bin\/awk -f<\/code>\uff0c\u8d4b\u4e88\u6267\u884c\u6743\u9650\u540e\u76f4\u63a5\u8fd0\u884c\u3002<\/p>\n<p>\u2022 \u6216\u901a\u8fc7<code>-f<\/code>\u9009\u9879\u6307\u5b9a\u811a\u672c\uff1a<\/p>\n<pre><code>```bash\nawk -f \u811a\u672c\u6587\u4ef6 \u8f93\u5165\u6587\u4ef6\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"3-awk\u811a\u672c\u7ed3\u6784\"><strong>3. AWK\u811a\u672c\u7ed3\u6784<\/strong><\/h3>\n<p>\u2022 \u6a21\u5f0f\u4e0e\u52a8\u4f5c\uff1a<\/p>\n<p>\u2022 \u6a21\u5f0f\uff1a\u6761\u4ef6\u8bed\u53e5\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u51b3\u5b9a\u4f55\u65f6\u89e6\u53d1\u52a8\u4f5c\u3002\u7279\u6b8a\u6a21\u5f0f<code>BEGIN<\/code>\uff08\u5904\u7406\u524d\u6267\u884c\uff09\u548c<code>END<\/code>\uff08\u5904\u7406\u540e\u6267\u884c\uff09\u3002<\/p>\n<p>\u2022 \u52a8\u4f5c\uff1a\u7528<code>{}<\/code>\u5305\u88f9\u7684\u64cd\u4f5c\uff0c\u5982\u6253\u5370\u3001\u8ba1\u7b97\u7b49\u3002\u82e5\u7701\u7565\u6a21\u5f0f\uff0c\u9ed8\u8ba4\u5904\u7406\u6240\u6709\u884c\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a\u6253\u5370\u8868\u5934\u548c\u8868\u5c3e\uff1a<\/p>\n<pre><code>```bash\nawk 'BEGIN{print \"Header\"} {print $0} END{print \"Footer\"}' file.txt\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"4-\u57df\u4e0e\u8bb0\u5f55\u5904\u7406\"><strong>4. \u57df\u4e0e\u8bb0\u5f55\u5904\u7406<\/strong><\/h3>\n<p>\u2022 \u57df\uff08Field\uff09\uff1a\u9ed8\u8ba4\u4ee5\u7a7a\u683c\u5206\u9694\uff0c\u901a\u8fc7<code>$1, $2, ..., $n<\/code>\u8bbf\u95ee\u3002<code>$0<\/code>\u8868\u793a\u6574\u6761\u8bb0\u5f55\u3002<\/p>\n<p>\u2022 \u8bb0\u5f55\uff08Record\uff09\uff1a\u9ed8\u8ba4\u4ee5\u6362\u884c\u7b26\u5206\u9694\uff0c\u53ef\u901a\u8fc7\u5185\u7f6e\u53d8\u91cf<code>RS<\/code>\u4fee\u6539\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>\u2022 \u63d0\u53d6\u540d\u5b57\u548c\u7ea7\u522b\uff08\u7b2c1\u30014\u57df\uff09\uff1a<\/p>\n<pre><code>```bash\nawk '{print $1, $4}' grade.txt\n```\n<\/code><\/pre>\n<p>\u2022 \u6dfb\u52a0\u8868\u5934\u548cTab\u5206\u9694\uff1a<\/p>\n<pre><code>```bash\nawk 'BEGIN{print \"Name\\tBelt\"} {print $1 \"\\t\" $4}' grade.txt\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"5-\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e\u6761\u4ef6\u64cd\u4f5c\"><strong>5. \u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e\u6761\u4ef6\u64cd\u4f5c<\/strong><\/h3>\n<p>\u2022 \u6b63\u5219\u5339\u914d\uff1a<\/p>\n<p>\u2022 \u4f7f\u7528<code>~<\/code>\u5339\u914d\u6b63\u5219\uff0c<code>!~<\/code>\u4e0d\u5339\u914d\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a\u5339\u914d\u7ea7\u522b\u5305\u542b<code>Brown<\/code>\u7684\u8bb0\u5f55\uff1a<\/p>\n<pre><code>```bash\nawk '$4 ~ \/Brown\/ {print $0}' grade.txt\n```\n<\/code><\/pre>\n<p>\u2022 \u6761\u4ef6\u64cd\u4f5c\u7b26\uff1a<\/p>\n<p>\u2022 \u6bd4\u8f83\uff1a<code>&lt;<\/code>,\u00a0<code>&lt;=<\/code>,\u00a0<code>==<\/code>,\u00a0<code>!=<\/code>,\u00a0<code>&gt;=<\/code>,\u00a0<code>&gt;<\/code>\u3002<\/p>\n<p>\u2022 \u903b\u8f91\uff1a<code>&amp;&amp;<\/code>\uff08\u4e0e\uff09\u3001<code>||<\/code>\uff08\u6216\uff09\u3001<code>!<\/code>\uff08\u975e\uff09\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a\u67e5\u627e\u5e74\u9f84\u5c0f\u4e8e10\u5c81\u7684\u5b66\u751f\uff1a<\/p>\n<pre><code>```bash\nawk '$5 &lt; 10' grade.txt\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"6-\u5185\u7f6e\u53d8\u91cf\"><strong>6. \u5185\u7f6e\u53d8\u91cf<\/strong><\/h3>\n<figure class=\"table-figure\">\n<table>\n<thead>\n<tr>\n<th>\u53d8\u91cf<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>NR<\/code><\/td>\n<td>\u5df2\u8bfb\u8bb0\u5f55\u6570\uff08\u884c\u53f7\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>NF<\/code><\/td>\n<td>\u5f53\u524d\u8bb0\u5f55\u7684\u57df\u4e2a\u6570<\/td>\n<\/tr>\n<tr>\n<td><code>FS<\/code><\/td>\n<td>\u8f93\u5165\u57df\u5206\u9694\u7b26\uff08\u9ed8\u8ba4\u7a7a\u683c\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>OFS<\/code><\/td>\n<td>\u8f93\u51fa\u57df\u5206\u9694\u7b26\uff08\u9ed8\u8ba4\u7a7a\u683c\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>FILENAME<\/code><\/td>\n<td>\u5f53\u524d\u5904\u7406\u7684\u6587\u4ef6\u540d<\/td>\n<\/tr>\n<tr>\n<td><code>RS<\/code><\/td>\n<td>\u8bb0\u5f55\u5206\u9694\u7b26\uff08\u9ed8\u8ba4\u6362\u884c\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>\u2022 \u7edf\u8ba1\u6587\u4ef6\u884c\u6570\uff1a<\/p>\n<pre><code>```bash\nawk 'END{print NR}' grade.txt\n```\n<\/code><\/pre>\n<p>\u2022 \u663e\u793a\u6587\u4ef6\u540d\u548c\u6bcf\u884c\u57df\u6570\uff1a<\/p>\n<pre><code>```bash\nawk '{print FILENAME, NR, NF}' grade.txt\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"7-\u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570\"><strong>7. \u5b57\u7b26\u4e32\u5904\u7406\u51fd\u6570<\/strong><\/h3>\n<p>\u2022 \u5e38\u7528\u51fd\u6570\uff1a<\/p>\n<p>\u2022\u00a0<code>gsub(r,s)<\/code>\uff1a\u5168\u5c40\u66ff\u6362\u6b63\u5219<code>r<\/code>\u4e3a<code>s<\/code>\u3002<\/p>\n<p>\u2022\u00a0<code>index(s,t)<\/code>\uff1a\u8fd4\u56de\u5b50\u4e32<code>t<\/code>\u5728<code>s<\/code>\u4e2d\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u2022\u00a0<code>length(s)<\/code>\uff1a\u8fd4\u56de\u5b57\u7b26\u4e32\u957f\u5ea6\u3002<\/p>\n<p>\u2022\u00a0<code>split(s,a,sep)<\/code>\uff1a\u6309<code>sep<\/code>\u5206\u5272<code>s<\/code>\u5230\u6570\u7ec4<code>a<\/code>\u3002<\/p>\n<p>\u2022\u00a0<code>substr(s,p,n)<\/code>\uff1a\u4ece\u4f4d\u7f6e<code>p<\/code>\u63d0\u53d6\u957f\u5ea6\u4e3a<code>n<\/code>\u7684\u5b50\u4e32\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>\u2022 \u66ff\u6362\u6587\u672c\u4e2d\u7684\u6570\u5b57\uff1a<\/p>\n<pre><code>```bash\nawk '{gsub(\/48\/, \"49\"); print $0}' grade.txt\n```\n<\/code><\/pre>\n<p>\u2022 \u63d0\u53d6\u5b50\u4e32\uff1a<\/p>\n<pre><code>```bash\nawk '{print substr($1, 3, 5)}' grade.txt  # \u63d0\u53d6\u7b2c1\u57df\u7b2c3\u5b57\u7b26\u540e\u76845\u4e2a\u5b57\u7b26\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"8-\u683c\u5f0f\u5316\u8f93\u51faprintf\"><strong>8. \u683c\u5f0f\u5316\u8f93\u51fa\uff08printf\uff09<\/strong><\/h3>\n<p>\u2022 \u683c\u5f0f\u63a7\u5236\u7b26\uff1a<\/p>\n<p>\u2022\u00a0<code>%s<\/code>\uff08\u5b57\u7b26\u4e32\uff09\u3001<code>%d<\/code>\uff08\u6574\u6570\uff09\u3001<code>%f<\/code>\uff08\u6d6e\u70b9\u6570\uff09\u3001<code>%c<\/code>\uff08\u5b57\u7b26\uff09\u7b49\u3002<\/p>\n<p>\u2022 \u4fee\u9970\u7b26\uff1a<code>-<\/code>\uff08\u5de6\u5bf9\u9f50\uff09\u3001<code>\u5bbd\u5ea6<\/code>\u3001<code>.\u7cbe\u5ea6<\/code>\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">awk '{printf \"%-15s %10d\\n\", $1, $6}' grade.txt  # \u5de6\u5bf9\u9f50\u540d\u5b57\uff0c\u53f3\u5bf9\u9f50\u5206\u6570\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"9-\u6570\u7ec4\u5e94\u7528\"><strong>9. \u6570\u7ec4\u5e94\u7528<\/strong><\/h3>\n<p>\u2022 \u6570\u7ec4\u64cd\u4f5c\uff1a<\/p>\n<p>\u2022 \u65e0\u9700\u9884\u5148\u58f0\u660e\uff0c\u81ea\u52a8\u521b\u5efa\u3002<\/p>\n<p>\u2022 \u904d\u5386\u6570\u7ec4\uff1a<code>for (key in array) print array[key]<\/code>\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>\u2022 \u7edf\u8ba1\u4e0d\u540c\u7ea7\u522b\u7684\u6570\u91cf\uff1a<\/p>\n<pre><code>```bash\nawk -F# '{color[$1]++} END{for (c in color) print c, color[c]}' data.txt\n```\n<\/code><\/pre>\n<p>\u2022 \u5206\u5272\u5b57\u7b26\u4e32\u5230\u6570\u7ec4\uff1a<\/p>\n<pre><code>```bash\nawk '{split($0, arr, \"#\"); print arr[1]}' input.txt\n```\n<\/code><\/pre>\n<hr \/>\n<p><strong>10. \u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/strong>\u00a0\u2022 \u7edf\u8ba1\u5b66\u751f\u603b\u5206\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">awk '{sum += $6} END{print \"\u603b\u5206:\", sum}' grade.txt\n<\/code><\/pre>\n<p>\u2022 \u5206\u6790\u7cfb\u7edf\u78c1\u76d8\u7a7a\u95f4\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">df -k | awk '$4 &lt; 50000 {print $6 \" \u7a7a\u95f4\u4e0d\u8db3: \" $4}'\n<\/code><\/pre>\n<p>\u2022 \u5904\u7406passwd\u6587\u4ef6\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">awk -F: '{print \"\u7528\u6237:\" $1, \"UID:\" $3}' \/etc\/passwd\n<\/code><\/pre>\n<hr \/>\n<p><strong>11. \u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5<\/strong>\u00a0\u2022 \u5e38\u89c1\u9519\u8bef\uff1a<\/p>\n<p>\u2022 \u5f15\u53f7\u4e0d\u5339\u914d\u3001\u62ec\u53f7\u7f3a\u5931\u3001\u57df\u7f16\u53f7\u9519\u8bef\u3002<\/p>\n<p>\u2022 \u9519\u8bef\u793a\u4f8b\uff1a<code>awk: cmd. line:1: unterminated string<\/code>\uff08\u672a\u95ed\u5408\u5b57\u7b26\u4e32\uff09\u3002<\/p>\n<p>\u2022 \u8c03\u8bd5\u6280\u5de7\uff1a<\/p>\n<p>\u2022 \u4f7f\u7528<code>print<\/code>\u8bed\u53e5\u8f93\u51fa\u4e2d\u95f4\u53d8\u91cf\u3002<\/p>\n<p>\u2022 \u901a\u8fc7<code>tee<\/code>\u547d\u4ee4\u540c\u65f6\u8f93\u51fa\u5230\u5c4f\u5e55\u548c\u6587\u4ef6\uff1a<\/p>\n<pre><code>```bash\nawk '{print $0}' grade.txt | tee output.txt\n```\n<\/code><\/pre>\n<hr \/>\n<p><strong>\u603b\u7ed3<\/strong>\u00a0AWK\u7684\u6838\u5fc3\u529f\u80fd\u5728\u4e8e\u7075\u6d3b\u5904\u7406\u7ed3\u6784\u5316\u6587\u672c\uff0c\u901a\u8fc7\u6a21\u5f0f\u5339\u914d\u3001\u57df\u64cd\u4f5c\u3001\u5185\u7f6e\u51fd\u6570\u548c\u6570\u7ec4\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002\u638c\u63e1\u5176\u57fa\u672c\u8bed\u6cd5\uff08\u5982\u6a21\u5f0f-\u52a8\u4f5c\u7ed3\u6784\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5185\u7f6e\u53d8\u91cf\uff09\u540e\uff0c\u53ef\u9ad8\u6548\u5b8c\u6210\u65e5\u5fd7\u5206\u6790\u3001\u6570\u636e\u7edf\u8ba1\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u7b49\u9700\u6c42\u3002\u7ed3\u5408Shell\u811a\u672c\u4e0e\u5176\u4ed6\u5de5\u5177\uff08\u5982sed\u3001grep\uff09\uff0c\u80fd\u8fdb\u4e00\u6b65\u63d0\u5347\u6587\u672c\u5904\u7406\u80fd\u529b\u3002<\/p>\n<h2 id=\"\u7b2c10\u7ae0-sed\u7528\u6cd5\u8be6\u89e3\u603b\u7ed3\">\u7b2c10\u7ae0 sed\u7528\u6cd5\u8be6\u89e3\u603b\u7ed3<\/h2>\n<hr \/>\n<h3 id=\"\u4e00sed\u57fa\u7840\u6982\u5ff5\"><strong>\u4e00\u3001sed\u57fa\u7840\u6982\u5ff5<\/strong><\/h3>\n<ol start=\"\">\n<li>\n<p>\u975e\u4ea4\u4e92\u5f0f\u6587\u672c\u7f16\u8f91\u5668 \u2022 \u64cd\u4f5c\u6587\u4ef6\u6216\u6807\u51c6\u8f93\u5165\uff08\u952e\u76d8\u3001\u91cd\u5b9a\u5411\u3001\u7ba1\u9053\u7b49\uff09\u7684\u6587\u672c\u62f7\u8d1d\uff0c\u4e0d\u76f4\u63a5\u4fee\u6539\u539f\u6587\u4ef6\u3002<\/p>\n<p>\u2022 \u9002\u5408\u81ea\u52a8\u5316\u5904\u7406\uff0c\u4e00\u6b21\u6027\u5904\u7406\u6240\u6709\u4fee\u6539\uff0c\u6548\u7387\u9ad8\u3002<\/p>\n<p>\u2022 \u4e0eVi\u4e0d\u540c\uff0c\u65e0\u9700\u7528\u6237\u4ea4\u4e92\uff0c\u9002\u7528\u4e8e\u811a\u672c\u5316\u4efb\u52a1\u3002<\/p>\n<\/li>\n<li>\n<p>\u6838\u5fc3\u529f\u80fd \u2022 \u6587\u672c\u62bd\u53d6\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3001\u57df\u6bd4\u8f83\u3001\u589e\u5220\u6539\u64cd\u4f5c\u3002<\/p>\n<p>\u2022 \u652f\u6301\u884c\u53f7\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9a\u4f4d\u6587\u672c\uff0c\u652f\u6301\u591a\u547d\u4ee4\u7ec4\u5408\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e8c\u8c03\u7528sed\u7684\u4e09\u79cd\u65b9\u5f0f\"><strong>\u4e8c\u3001\u8c03\u7528sed\u7684\u4e09\u79cd\u65b9\u5f0f<\/strong><\/h3>\n<ol start=\"\">\n<li>\n<p>\u547d\u4ee4\u884c\u76f4\u63a5\u8c03\u7528<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">sed [\u9009\u9879] '\u547d\u4ee4' \u8f93\u5165\u6587\u4ef6\n<\/code><\/pre>\n<p>\u2022 \u547d\u4ee4\u9700\u7528\u5355\u5f15\u53f7\u5305\u88f9\uff0c\u5982\u00a0<code>sed 's\/old\/new\/' file.txt<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>\u811a\u672c\u6587\u4ef6\u8c03\u7528<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">sed -f \u811a\u672c\u6587\u4ef6 \u8f93\u5165\u6587\u4ef6\n<\/code><\/pre>\n<p>\u2022 \u811a\u672c\u6587\u4ef6\u4e2d\u5199\u5165sed\u547d\u4ee4\uff0c\u5982\u00a0<code>#!\/bin\/sed -f \/pattern\/d<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>\u53ef\u6267\u884c\u811a\u672c \u2022 \u811a\u672c\u9996\u884c\u6307\u5b9a\u89e3\u91ca\u5668\uff1a<code>#!\/bin\/sed -f<\/code>\uff0c\u8d4b\u4e88\u6267\u884c\u6743\u9650\u540e\u76f4\u63a5\u8fd0\u884c\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e09\u5e38\u7528\u9009\u9879\"><strong>\u4e09\u3001\u5e38\u7528\u9009\u9879<\/strong><\/h3>\n<p>\u2022\u00a0<code>-n<\/code>\uff1a\u6291\u5236\u9ed8\u8ba4\u8f93\u51fa\uff08\u4ec5\u6253\u5370\u660e\u786e\u6307\u5b9a\u7684\u884c\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-e<\/code>\uff1a\u5141\u8bb8\u591a\u4e2a\u7f16\u8f91\u547d\u4ee4\uff0c\u5982\u00a0<code>sed -e 's\/a\/A\/' -e 's\/b\/B\/'<\/code>\u3002<\/p>\n<p>\u2022\u00a0<code>-f<\/code>\uff1a\u6307\u5b9a\u5305\u542bsed\u547d\u4ee4\u7684\u811a\u672c\u6587\u4ef6\u3002<\/p>\n<p>\u2022\u00a0<code>-i<\/code>\uff08\u672a\u63d0\u53ca\u4f46\u5e38\u7528\uff09\uff1a\u76f4\u63a5\u4fee\u6539\u6587\u4ef6\uff08\u6ce8\u610f\u5907\u4efd\uff09\u3002<\/p>\n<hr \/>\n<h3 id=\"\u56db\u6587\u672c\u5b9a\u4f4d\u4e0e\u57fa\u672c\u547d\u4ee4\"><strong>\u56db\u3001\u6587\u672c\u5b9a\u4f4d\u4e0e\u57fa\u672c\u547d\u4ee4<\/strong><\/h3>\n<ol start=\"\">\n<li>\n<p>\u5b9a\u4f4d\u65b9\u5f0f \u2022 \u884c\u53f7\uff1a<code>x<\/code>\uff08\u5355\u884c\uff09\u3001<code>x,y<\/code>\uff08\u8303\u56f4\uff09\u3001<code>$<\/code>\uff08\u6700\u540e\u4e00\u884c\uff09\u3002<\/p>\n<p>\u2022 \u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<code>\/pattern\/<\/code>\u00a0\u6216\u6df7\u5408\u6a21\u5f0f\u5982\u00a0<code>3,\/pattern\/<\/code>\uff08\u7b2c3\u884c\u5230\u5339\u914d\u884c\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>\u57fa\u672c\u547d\u4ee4<\/p>\n<figure class=\"table-figure\">\n<table>\n<thead>\n<tr>\n<th>\u547d\u4ee4<\/th>\n<th>\u529f\u80fd<\/th>\n<th>\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>p<\/code><\/td>\n<td>\u6253\u5370\u884c<\/td>\n<td><code>sed -n '2p' file<\/code>\uff08\u6253\u5370\u7b2c2\u884c\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>d<\/code><\/td>\n<td>\u5220\u9664\u884c<\/td>\n<td><code>sed '1,3d' file<\/code>\uff08\u5220\u96641-3\u884c\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>a\\<\/code><\/td>\n<td>\u884c\u540e\u8ffd\u52a0<\/td>\n<td><code>sed '\/pattern\/a\\\u65b0\u6587\u672c' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>i\\<\/code><\/td>\n<td>\u884c\u524d\u63d2\u5165<\/td>\n<td><code>sed '3i\\\u65b0\u6587\u672c' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>c\\<\/code><\/td>\n<td>\u66ff\u6362\u884c<\/td>\n<td><code>sed '\/pattern\/c\\\u65b0\u884c' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>s<\/code><\/td>\n<td>\u66ff\u6362\u6587\u672c<\/td>\n<td><code>sed 's\/old\/new\/g' file<\/code>\uff08\u5168\u5c40\u66ff\u6362\uff09<\/td>\n<\/tr>\n<tr>\n<td><code>r<\/code><\/td>\n<td>\u8bfb\u53d6\u6587\u4ef6\u63d2\u5165<\/td>\n<td><code>sed '\/pattern\/r other.txt' file<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>w<\/code><\/td>\n<td>\u5199\u5165\u6587\u4ef6<\/td>\n<td><code>sed '\/pattern\/w output.txt' file<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e94\u6b63\u5219\u8868\u8fbe\u5f0f\u5e94\u7528\"><strong>\u4e94\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u5e94\u7528<\/strong><\/h3>\n<ol start=\"\">\n<li>\n<p>\u5143\u5b57\u7b26\u5904\u7406 \u2022 \u9700\u8f6c\u4e49\u7279\u6b8a\u5b57\u7b26\uff0c\u5982\u00a0<code>\\$<\/code>\u00a0\u8868\u793a\u00a0<code>$<\/code>\uff0c<code>\\.<\/code>\u00a0\u5339\u914d\u53e5\u70b9\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<code>sed -n '\/\\$\/p' file<\/code>\uff08\u5339\u914d\u542b<code>$<\/code>\u7684\u884c\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>\u6a21\u5f0f\u5339\u914d \u2022 \u4efb\u610f\u5b57\u7b26\uff1a<code>.*<\/code>\uff08\u5982\u00a0<code>.*ing<\/code>\u00a0\u5339\u914d\u4ee5\u00a0<code>ing<\/code>\u00a0\u7ed3\u5c3e\u7684\u5355\u8bcd\uff09\u3002<\/p>\n<p>\u2022 \u884c\u9996\/\u884c\u5c3e\uff1a<code>^<\/code>\uff08\u884c\u9996\uff09\u3001<code>$<\/code>\uff08\u884c\u5c3e\uff09\uff0c\u5982\u00a0<code>sed 's\/^#\/\/g'<\/code>\u00a0\u5220\u9664\u884c\u9996\u7684\u00a0<code>#<\/code>\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u516d\u9ad8\u7ea7\u64cd\u4f5c\u4e0e\u811a\u672c\u793a\u4f8b\"><strong>\u516d\u3001\u9ad8\u7ea7\u64cd\u4f5c\u4e0e\u811a\u672c\u793a\u4f8b<\/strong><\/h3>\n<ol start=\"\">\n<li>\n<p>\u591a\u547d\u4ee4\u7ec4\u5408<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">sed -e 's\/old\/new\/' -e '\/pattern\/d' file\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u63a7\u5236\u5b57\u7b26\u5904\u7406 \u2022 \u663e\u793a\u63a7\u5236\u5b57\u7b26\uff1a<code>sed -n 'l' file<\/code>\uff08\u663e\u793a\u5982\u00a0<code>\\033<\/code>\u00a0\u7684\u516b\u8fdb\u5236\u7801\uff09\u3002<\/p>\n<p>\u2022 \u5220\u9664\u884c\u5c3e\u00a0<code>^M<\/code>\uff1a<code>sed 's\/^M\/\/g' file<\/code>\uff08\u9700\u8f93\u5165\u5b9e\u9645\u63a7\u5236\u7b26\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>\u6587\u672c\u8f6c\u6362\u793a\u4f8b \u2022 \u53bb\u9664\u884c\u9996\u6570\u5b57\uff1a<code>sed 's\/^[0-9]*\/\/' file<\/code>\u3002<\/p>\n<p>\u2022 \u9644\u52a0\u6587\u672c\u5230\u884c\u5c3e\uff1a<code>sed 's\/$\/ Passed\/' file<\/code>\u3002<\/p>\n<p>\u2022 \u66ff\u6362\u8def\u5f84\u5206\u9694\u7b26\uff1a<code>echo \"\/usr\/local\" | sed 's\/^\\\/\/\/'<\/code>\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e03\u5b9e\u7528\u6280\u5de7\u4e0e\u4e00\u884c\u547d\u4ee4\"><strong>\u4e03\u3001\u5b9e\u7528\u6280\u5de7\u4e0e\u4e00\u884c\u547d\u4ee4<\/strong><\/h3>\n<ol start=\"\">\n<li>\n<p>\u5feb\u901f\u5904\u7406 \u2022 \u5220\u9664\u7a7a\u884c\uff1a<code>sed '\/^$\/d' file<\/code>\u3002<\/p>\n<p>\u2022 \u538b\u7f29\u591a\u4e2a\u7a7a\u683c\uff1a<code>sed 's\/ \\+\/ \/g' file<\/code>\u3002<\/p>\n<p>\u2022 \u5220\u9664\u884c\u9996\u7a7a\u683c\uff1a<code>sed 's\/^ *\/\/' file<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>\u53d8\u91cf\u4f20\u9012 \u2022 Shell\u53d8\u91cf\u4f20\u9012\u5230sed\uff08\u53cc\u5f15\u53f7\u5141\u8bb8\u53d8\u91cf\u6269\u5c55\uff09\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">REPLACE=\"NEW\"; sed \"s\/OLD\/$REPLACE\/g\" file\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u7ed3\u679c\u4fdd\u5b58\u4e0e\u8bbe\u7f6e\u53d8\u91cf \u2022 \u4fdd\u5b58\u8f93\u51fa\uff1a<code>sed 's\/old\/new\/' file &gt; output<\/code>\u3002<\/p>\n<p>\u2022 \u8bbe\u7f6eShell\u53d8\u91cf\uff1a<code>NEW=$(sed 's\/old\/new\/' file)<\/code>\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u516b\u5178\u578b\u5e94\u7528\u573a\u666f\"><strong>\u516b\u3001\u5178\u578b\u5e94\u7528\u573a\u666f<\/strong><\/h3>\n<ol start=\"\">\n<li>\n<p>\u65e5\u5fd7\u5904\u7406 \u2022 \u63d0\u53d6\u7279\u5b9a\u65f6\u95f4\u6bb5\u65e5\u5fd7\uff1a<code>sed -n '\/2023-10-01 12:00\/,\/2023-10-01 13:00\/p' log.txt<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\u6e05\u6d17 \u2022 \u5220\u9664\u63a7\u5236\u5b57\u7b26\u3001\u683c\u5f0f\u5316CSV\u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u6279\u91cf\u91cd\u547d\u540d \u2022 \u7ed3\u5408\u7ba1\u9053\u4e0e\u5faa\u73af\uff0c\u5982\u6279\u91cf\u4fee\u6539\u6587\u4ef6\u540d\u540e\u7f00\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">ls *.txt | sed 's\/\\.txt$\/\/' | xargs -I {} mv {}.txt {}.bak\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e5d\u6ce8\u610f\u4e8b\u9879\"><strong>\u4e5d\u3001\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n<ol start=\"\">\n<li>\u5907\u4efd\u539f\u6587\u4ef6\uff1a\u811a\u672c\u5904\u7406\u524d\u5efa\u8bae\u5907\u4efd\uff0c\u907f\u514d\u8bef\u64cd\u4f5c\u3002<\/li>\n<li>\u5206\u6b65\u6d4b\u8bd5\uff1a\u590d\u6742\u4efb\u52a1\u5206\u89e3\u4e3a\u591a\u4e2a\u6b65\u9aa4\uff0c\u9010\u6b65\u9a8c\u8bc1\u7ed3\u679c\u3002<\/li>\n<li>\u6027\u80fd\u4f18\u5316\uff1a\u907f\u514d\u5728\u8d85\u5927\u6587\u4ef6\u4e2d\u4f7f\u7528\u5168\u5c40\u66ff\u6362\uff08<code>g<\/code>\uff09\uff0c\u4f18\u5148\u6307\u5b9a\u8303\u56f4\u3002<\/li>\n<\/ol>\n<hr \/>\n<p>\u901a\u8fc7\u672c\u7ae0\u5b66\u4e60\uff0c\u8bfb\u8005\u53ef\u638c\u63e1sed\u7684\u6838\u5fc3\u529f\u80fd\uff0c\u7075\u6d3b\u8fd0\u7528\u5176\u975e\u4ea4\u4e92\u5f0f\u7279\u6027\u5904\u7406\u6587\u672c\uff0c\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u5b9e\u73b0\u9ad8\u6548\u81ea\u52a8\u5316\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"\u7b2c11\u7ae0-\u5408\u5e76\u4e0e\u5206\u5272\u5de5\u5177\u603b\u7ed3\">\u7b2c11\u7ae0 \u5408\u5e76\u4e0e\u5206\u5272\u5de5\u5177\u603b\u7ed3<\/h2>\n<hr \/>\n<h3 id=\"1-sort\u6587\u672c\u6392\u5e8f\"><strong>1. sort\uff1a\u6587\u672c\u6392\u5e8f<\/strong><\/h3>\n<p>\u7528\u9014\uff1a\u6309\u6307\u5b9a\u89c4\u5219\u5bf9\u6587\u4ef6\u884c\u6392\u5e8f\uff0c\u652f\u6301\u591a\u57df\u6392\u5e8f\u3001\u6570\u503c\u6392\u5e8f\u3001\u9006\u5e8f\u7b49\u3002 \u6838\u5fc3\u9009\u9879\uff1a \u2022\u00a0<code>-c<\/code>\uff1a\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5df2\u6392\u5e8f\u3002<\/p>\n<p>\u2022\u00a0<code>-u<\/code>\uff1a\u53bb\u9664\u91cd\u590d\u884c\uff08\u4ec5\u4fdd\u7559\u552f\u4e00\u884c\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-n<\/code>\uff1a\u6309\u6570\u503c\u6392\u5e8f\uff08\u907f\u514d\u6309\u5b57\u7b26\u6392\u5e8f\u5bfc\u81f4\u9519\u8bef\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-r<\/code>\uff1a\u9006\u5e8f\u6392\u5e8f\u3002<\/p>\n<p>\u2022\u00a0<code>-t<\/code>\uff1a\u6307\u5b9a\u5206\u9694\u7b26\uff08\u5982<code>-t:<\/code>\u8868\u793a\u5192\u53f7\u5206\u9694\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-k<\/code>\uff1a\u6307\u5b9a\u6392\u5e8f\u952e\uff08\u5982<code>-k3<\/code>\u6309\u7b2c3\u57df\u6392\u5e8f\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-o<\/code>\uff1a\u6307\u5b9a\u8f93\u51fa\u6587\u4ef6\u3002<\/p>\n<p>\u793a\u4f8b\uff1a \u2022 \u6309\u7b2c3\u57df\u6570\u503c\u6392\u5e8f\uff08\u5192\u53f7\u5206\u9694\uff09\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">sort -t: -k3n video.txt\n<\/code><\/pre>\n<p>\u2022 \u5408\u5e76\u5df2\u6392\u5e8f\u6587\u4ef6\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">sort -m sorted1.txt sorted2.txt\n<\/code><\/pre>\n<p>\u2022 \u68c0\u67e5\u6587\u4ef6\u662f\u5426\u6392\u5e8f\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">sort -c video.txt &amp;&amp; echo \"\u5df2\u6392\u5e8f\" || echo \"\u672a\u6392\u5e8f\"\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"2-uniq\u5904\u7406\u91cd\u590d\u884c\"><strong>2. uniq\uff1a\u5904\u7406\u91cd\u590d\u884c<\/strong><\/h3>\n<p>\u7528\u9014\uff1a\u8fc7\u6ee4\u6216\u7edf\u8ba1\u8fde\u7eed\u91cd\u590d\u884c\u3002 \u6838\u5fc3\u9009\u9879\uff1a \u2022\u00a0<code>-u<\/code>\uff1a\u4ec5\u663e\u793a\u4e0d\u91cd\u590d\u884c\u3002<\/p>\n<p>\u2022\u00a0<code>-d<\/code>\uff1a\u4ec5\u663e\u793a\u91cd\u590d\u884c\uff08\u6bcf\u7ec4\u663e\u793a\u4e00\u6b21\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-c<\/code>\uff1a\u7edf\u8ba1\u6bcf\u884c\u91cd\u590d\u6b21\u6570\u3002<\/p>\n<p>\u2022\u00a0<code>-f N<\/code>\uff1a\u5ffd\u7565\u524dN\u4e2a\u57df\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<p>\u793a\u4f8b\uff1a \u2022 \u7edf\u8ba1\u91cd\u590d\u6b21\u6570\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">uniq -c myfile.txt\n<\/code><\/pre>\n<p>\u2022 \u663e\u793a\u91cd\u590d\u884c\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">uniq -d myfile.txt\n<\/code><\/pre>\n<p>\u2022 \u5ffd\u7565\u524d2\u57df\u6bd4\u8f83\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">uniq -f2 data.txt\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"3-join\u8fde\u63a5\u4e24\u4e2a\u6587\u4ef6\"><strong>3. join\uff1a\u8fde\u63a5\u4e24\u4e2a\u6587\u4ef6<\/strong><\/h3>\n<p>\u7528\u9014\uff1a\u57fa\u4e8e\u516c\u5171\u57df\u5408\u5e76\u4e24\u4e2a\u5df2\u6392\u5e8f\u6587\u4ef6\uff0c\u7c7b\u4f3cSQL\u7684JOIN\u64cd\u4f5c\u3002 \u6838\u5fc3\u9009\u9879\uff1a \u2022\u00a0<code>-a N<\/code>\uff1a\u663e\u793a\u7b2cN\u4e2a\u6587\u4ef6\u7684\u4e0d\u5339\u914d\u884c\uff08\u5982<code>-a1<\/code>\u663e\u793a\u6587\u4ef61\u672a\u5339\u914d\u884c\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-o<\/code>\uff1a\u6307\u5b9a\u8f93\u51fa\u683c\u5f0f\uff08\u5982<code>1.1,2.2<\/code>\u8f93\u51fa\u6587\u4ef61\u7b2c1\u57df\u548c\u6587\u4ef62\u7b2c2\u57df\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-t<\/code>\uff1a\u6307\u5b9a\u5206\u9694\u7b26\uff08\u9ed8\u8ba4\u7a7a\u683c\/Tab\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-j<\/code>\uff1a\u6307\u5b9a\u8fde\u63a5\u57df\uff08\u5982<code>-j1 3 -j2 2<\/code>\u8fde\u63a5\u6587\u4ef61\u7b2c3\u57df\u548c\u6587\u4ef62\u7b2c2\u57df\uff09\u3002<\/p>\n<p>\u793a\u4f8b\uff1a \u2022 \u5408\u5e76\u59d3\u540d\u548c\u5730\u5740\u6587\u4ef6\uff08\u5192\u53f7\u5206\u9694\uff09\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">join -t: names.txt town.txt\n<\/code><\/pre>\n<p>\u2022 \u663e\u793a\u6587\u4ef61\u672a\u5339\u914d\u884c\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">join -a1 names.txt town.txt\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"4-cut\u526a\u5207\u5217\u6216\u57df\"><strong>4. cut\uff1a\u526a\u5207\u5217\u6216\u57df<\/strong><\/h3>\n<p>\u7528\u9014\uff1a\u4ece\u6587\u4ef6\u6216\u8f93\u5165\u4e2d\u63d0\u53d6\u6307\u5b9a\u5217\u6216\u57df\u3002 \u6838\u5fc3\u9009\u9879\uff1a \u2022\u00a0<code>-d<\/code>\uff1a\u6307\u5b9a\u5206\u9694\u7b26\uff08\u5982<code>-d:<\/code>\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-f<\/code>\uff1a\u9009\u62e9\u57df\uff08\u5982<code>-f1,3<\/code>\u6216<code>-f1-3<\/code>\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-c<\/code>\uff1a\u6309\u5b57\u7b26\u4f4d\u7f6e\u526a\u5207\uff08\u5982<code>-c1-5<\/code>\u526a\u5207\u524d5\u5b57\u7b26\uff09\u3002<\/p>\n<p>\u793a\u4f8b\uff1a \u2022 \u63d0\u53d6\/etc\/passwd\u7684\u7528\u6237\u540d\u548c\u5bb6\u76ee\u5f55\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">cut -d: -f1,6 \/etc\/passwd\n<\/code><\/pre>\n<p>\u2022 \u526a\u5207\u6587\u4ef6\u540d\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">ls *.log | cut -c4-6\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"5-paste\u5408\u5e76\u884c\"><strong>5. paste\uff1a\u5408\u5e76\u884c<\/strong><\/h3>\n<p>\u7528\u9014\uff1a\u6c34\u5e73\u5408\u5e76\u591a\u4e2a\u6587\u4ef6\u7684\u884c\uff0c\u9ed8\u8ba4\u7528Tab\u5206\u9694\u3002 \u6838\u5fc3\u9009\u9879\uff1a \u2022\u00a0<code>-d<\/code>\uff1a\u6307\u5b9a\u5206\u9694\u7b26\uff08\u5982<code>-d@<\/code>\u7528@\u5206\u9694\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-s<\/code>\uff1a\u5c06\u6587\u4ef6\u5408\u5e76\u4e3a\u5355\u884c\uff08\u5782\u76f4\u8f6c\u6c34\u5e73\uff09\u3002<\/p>\n<p>\u793a\u4f8b\uff1a \u2022 \u5408\u5e76\u4e24\u5217\u6570\u636e\uff08\u5192\u53f7\u5206\u9694\uff09\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">paste -d: pas1.txt pas2.txt\n<\/code><\/pre>\n<p>\u2022 \u5c06\u6587\u4ef6\u5185\u5bb9\u8f6c\u4e3a\u5355\u884c\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">paste -s data.txt\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"6-split\u5206\u5272\u5927\u6587\u4ef6\"><strong>6. split\uff1a\u5206\u5272\u5927\u6587\u4ef6<\/strong><\/h3>\n<p>\u7528\u9014\uff1a\u5c06\u5927\u6587\u4ef6\u6309\u884c\u6570\u6216\u5927\u5c0f\u5206\u5272\u4e3a\u5c0f\u6587\u4ef6\u3002 \u8bed\u6cd5\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">split [-l \u884c\u6570] \u8f93\u5165\u6587\u4ef6 [\u8f93\u51fa\u524d\u7f00]\n<\/code><\/pre>\n<p>\u793a\u4f8b\uff1a \u2022 \u6bcf1000\u884c\u5206\u5272\u6587\u4ef6\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">split -l 1000 bigfile.txt part_\n<\/code><\/pre>\n<p>\u2022 \u751f\u6210\u7684\u6587\u4ef6\u540d\u683c\u5f0f\u4e3a<code>part_aa<\/code>,\u00a0<code>part_ab<\/code>\u7b49\u3002<\/p>\n<hr \/>\n<h3 id=\"\u5173\u952e\u6ce8\u610f\u4e8b\u9879\"><strong>\u5173\u952e\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n<ol start=\"\">\n<li>\u9884\u5904\u7406\uff1a\u4f7f\u7528<code>join<\/code>\u524d\u9700\u786e\u4fdd\u6587\u4ef6\u5df2\u6392\u5e8f\uff1b<code>uniq<\/code>\u4ec5\u5904\u7406\u8fde\u7eed\u91cd\u590d\u884c\u3002<\/li>\n<li>\u5206\u9694\u7b26\uff1a\u660e\u786e\u4f7f\u7528<code>-t<\/code>\u6216<code>-d<\/code>\u6307\u5b9a\u5206\u9694\u7b26\uff08\u5982<code>-t:<\/code>\u5904\u7406\u5192\u53f7\u5206\u9694\u6570\u636e\uff09\u3002<\/li>\n<li>\u6570\u503c\u6392\u5e8f\uff1a\u5bf9\u6570\u5b57\u5217\u6392\u5e8f\u65f6\u52a1\u5fc5\u7528<code>-n<\/code>\uff0c\u907f\u514d\u5b57\u7b26\u6392\u5e8f\u5bfc\u81f4\u9519\u8bef\u3002<\/li>\n<li>\u8f93\u51fa\u63a7\u5236\uff1a\u7075\u6d3b\u4f7f\u7528\u91cd\u5b9a\u5411\uff08<code>&gt;<\/code>\uff09\u548c\u7ba1\u9053\uff08<code>|<\/code>\uff09\u7ec4\u5408\u5de5\u5177\uff08\u5982<code>sort | uniq -c<\/code>\uff09\u3002<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u9ad8\u6548\u5904\u7406\u6587\u672c\u6570\u636e\u7684\u6392\u5e8f\u3001\u5408\u5e76\u3001\u53bb\u91cd\u3001\u5206\u5272\u7b49\u64cd\u4f5c\uff0c\u63d0\u5347\u6570\u636e\u5904\u7406\u6548\u7387\u3002<\/p>\n<h2 id=\"\u7b2c12\u7ae0-tr-\u547d\u4ee4\u603b\u7ed3\">\u7b2c12\u7ae0\u00a0<code>tr<\/code>\u00a0\u547d\u4ee4\u603b\u7ed3<\/h2>\n<h4 id=\"1-\u57fa\u672c\u529f\u80fd\">1.\u00a0<strong>\u57fa\u672c\u529f\u80fd<\/strong><\/h4>\n<p><code>tr<\/code>\uff08translate\uff09\u7528\u4e8e\u4ece\u6807\u51c6\u8f93\u5165\u4e2d\u901a\u8fc7\u66ff\u6362\u6216\u5220\u9664\u64cd\u4f5c\u8fdb\u884c\u5b57\u7b26\u8f6c\u6362\uff0c\u4e3b\u8981\u529f\u80fd\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u5b57\u7b26\u66ff\u6362<\/strong>\uff1a\u5c06\u5b57\u7b26\u4e321\u4e2d\u7684\u5b57\u7b26\u6620\u5c04\u5230\u5b57\u7b26\u4e322\u4e2d\u7684\u5b57\u7b26\u8fdb\u884c\u8f6c\u6362\u3002<\/li>\n<li><strong>\u5220\u9664\u5b57\u7b26<\/strong>\uff1a\u4f7f\u7528\u00a0<code>-d<\/code>\u00a0\u9009\u9879\u5220\u9664\u6307\u5b9a\u5b57\u7b26\u3002<\/li>\n<li><strong>\u538b\u7f29\u91cd\u590d\u5b57\u7b26<\/strong>\uff1a\u4f7f\u7528\u00a0<code>-s<\/code>\u00a0\u9009\u9879\u5c06\u91cd\u590d\u5b57\u7b26\u538b\u7f29\u4e3a\u5355\u4e2a\u5b57\u7b26\u3002<\/li>\n<\/ul>\n<h3 id=\"2-\u547d\u4ee4\u683c\u5f0f\">2.\u00a0<strong>\u547d\u4ee4\u683c\u5f0f<\/strong><\/h3>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">tr [\u9009\u9879] \"\u5b57\u7b26\u4e321\" \"\u5b57\u7b26\u4e322\" &lt; \u8f93\u5165\u6587\u4ef6 &gt; \u8f93\u51fa\u6587\u4ef6\n<\/code><\/pre>\n<p><strong>\u5e38\u7528\u9009\u9879<\/strong>\uff1a<\/p>\n<ul>\n<li><code>-c<\/code>\uff1a\u4f7f\u7528\u5b57\u7b26\u4e321\u7684\u8865\u96c6\uff08ASCII\u5b57\u7b26\u96c6\u7684\u8865\u96c6\uff09\u3002<\/li>\n<li><code>-d<\/code>\uff1a\u5220\u9664\u5b57\u7b26\u4e321\u4e2d\u7684\u5b57\u7b26\u3002<\/li>\n<li><code>-s<\/code>\uff1a\u538b\u7f29\u91cd\u590d\u5b57\u7b26\u3002<\/li>\n<\/ul>\n<h3 id=\"3-\u5b57\u7b26\u8303\u56f4\u4e0e\u8868\u793a\">3.\u00a0<strong>\u5b57\u7b26\u8303\u56f4\u4e0e\u8868\u793a<\/strong><\/h3>\n<ul>\n<li>\n<p>\u00a0<\/p>\n<p>\u5b57\u7b26\u8303\u56f4<\/p>\n<p>\uff1a<\/p>\n<ul>\n<li><code>[a-z]<\/code>\uff1a\u5c0f\u5199\u5b57\u6bcd\u3002<\/li>\n<li><code>[A-Z]<\/code>\uff1a\u5927\u5199\u5b57\u6bcd\u3002<\/li>\n<li><code>[0-9]<\/code>\uff1a\u6570\u5b57\u3002<\/li>\n<li>\u516b\u8fdb\u5236\u8868\u793a\uff1a\u5982\u00a0<code>\\012<\/code>\u00a0\u8868\u793a\u6362\u884c\u7b26\u3002<\/li>\n<li>\u91cd\u590d\u6a21\u5f0f\uff1a<code>[O*2]<\/code>\u00a0\u5339\u914d\u00a0<code>OO<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u00a0<\/p>\n<p>\u5b57\u7b26\u7c7b<\/p>\n<p>\uff1a<\/p>\n<ul>\n<li><code>[:lower:]<\/code>\uff1a\u5c0f\u5199\u5b57\u6bcd\u3002<\/li>\n<li><code>[:upper:]<\/code>\uff1a\u5927\u5199\u5b57\u6bcd\u3002<\/li>\n<li><code>[:digit:]<\/code>\uff1a\u6570\u5b57\u3002<\/li>\n<li><code>[:space:]<\/code>\uff1a\u7a7a\u767d\u5b57\u7b26\uff08\u5982\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u884c\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3 id=\"4-\u5e38\u7528\u64cd\u4f5c\u793a\u4f8b\">4.\u00a0<strong>\u5e38\u7528\u64cd\u4f5c\u793a\u4f8b<\/strong><\/h3>\n<h5 id=\"41-\u538b\u7f29\u91cd\u590d\u5b57\u7b26\"><strong>4.1 \u538b\u7f29\u91cd\u590d\u5b57\u7b26<\/strong><\/h5>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u5c06\u91cd\u590d\u5b57\u6bcd\u538b\u7f29\u4e3a\u5355\u4e2a\uff08\u5982 \"cowwwws\" \u2192 \"cows\"\uff09\ntr -s \"[a-z]\" &lt; oops.txt\n<\/code><\/pre>\n<h5 id=\"42-\u5220\u9664\u7a7a\u884c\"><strong>4.2 \u5220\u9664\u7a7a\u884c<\/strong><\/h5>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u4f7f\u7528\u6362\u884c\u7684\u516b\u8fdb\u5236\u8868\u793a\u6216\u901f\u8bb0\u7b26\ntr -s \"[\\012]\" &lt; plane.txt   # \u6216 tr -s \"\\n\"\n<\/code><\/pre>\n<h5 id=\"43-\u5927\u5c0f\u5199\u8f6c\u6362\"><strong>4.3 \u5927\u5c0f\u5199\u8f6c\u6362<\/strong><\/h5>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u5927\u5199\u8f6c\u5c0f\u5199\necho \"HELLO\" | tr \"[A-Z]\" \"[a-z]\"   # \u8f93\u51fa\uff1ahello\n# \u5c0f\u5199\u8f6c\u5927\u5199\uff08\u4f7f\u7528\u5b57\u7b26\u7c7b\uff09\necho \"world\" | tr \"[:lower:]\" \"[:upper:]\"   # \u8f93\u51fa\uff1aWORLD\n<\/code><\/pre>\n<h5 id=\"44-\u5220\u9664\u6307\u5b9a\u5b57\u7b26\"><strong>4.4 \u5220\u9664\u6307\u5b9a\u5b57\u7b26<\/strong><\/h5>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u5220\u9664\u6240\u6709\u6570\u5b57\uff08\u7ed3\u5408 -c \u548c -s\uff09\ntr -cs \"[a-zA-Z]\" \"[\\n*]\" &lt; diary.txt   # \u4ec5\u4fdd\u7559\u5b57\u6bcd\u5e76\u538b\u7f29\u6362\u884c\n<\/code><\/pre>\n<h5 id=\"45-\u63a7\u5236\u5b57\u7b26\u5904\u7406\"><strong>4.5 \u63a7\u5236\u5b57\u7b26\u5904\u7406<\/strong><\/h5>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u8f6c\u6362 DOS \u6362\u884c\u7b26\uff08^M\uff09\u4e3a UNIX \u6362\u884c\u7b26\ntr -s \"\\r\" \"\\n\" &lt; dos_file.txt\n# \u5220\u9664\u6240\u6709\u63a7\u5236\u5b57\u7b26\uff08\u5982 ^Z\uff09\ntr -d \"[\\032]\" &lt; input.txt\n<\/code><\/pre>\n<h5 id=\"46-\u5feb\u901f\u66ff\u6362\"><strong>4.6 \u5feb\u901f\u66ff\u6362<\/strong><\/h5>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u66ff\u6362\u5192\u53f7\u4e3a\u5236\u8868\u7b26\uff08\u7528\u4e8e\u589e\u5f3a\u53ef\u8bfb\u6027\uff09\ntr \":\" \"\\t\" &lt; passwd.txt\n# \u66ff\u6362\u591a\u4e2a\u8fde\u7eed0\u4e3a\u661f\u53f7\uff08\u5982 \"0000\" \u2192 \"****\"\uff09\ntr \"[0 * 4]\" \"*\" &lt; hdisk.txt\n<\/code><\/pre>\n<h3 id=\"5-\u9ad8\u7ea7\u6280\u5de7\">5.\u00a0<strong>\u9ad8\u7ea7\u6280\u5de7<\/strong><\/h3>\n<ul>\n<li>\n<p>\u00a0<\/p>\n<p>\u8865\u96c6\u64cd\u4f5c<\/p>\n<p>\uff1a\u4f7f\u7528<\/p>\n<p>\u00a0<\/p>\n<pre><code>-c\n<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>\u53cd\u5411\u5339\u914d\u5b57\u7b26\u3002<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u5220\u9664\u6240\u6709\u975e\u5b57\u6bcd\u5b57\u7b26\uff08\u4fdd\u7559\u5b57\u6bcd\u5e76\u538b\u7f29\u6362\u884c\uff09\ntr -cs \"[:alpha:]\" \"[\\n]\" &lt; file.txt\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u00a0<\/p>\n<p>\u6587\u4ef6\u5904\u7406<\/p>\n<p>\uff1a\u901a\u8fc7\u91cd\u5b9a\u5411\u4fdd\u5b58\u7ed3\u679c\u3002<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">tr \"[A-Z]\" \"[a-z]\" &lt; input.txt &gt; output.txt\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u00a0<\/p>\n<p>\u7ec4\u5408\u9009\u9879<\/p>\n<p>\uff1a\u540c\u65f6\u4f7f\u7528<\/p>\n<p>\u00a0<\/p>\n<pre><code>-d\n<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>\u548c<\/p>\n<p>\u00a0<\/p>\n<pre><code>-s\n<\/code><\/pre>\n<p>\u3002<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u5220\u9664\u6240\u6709\u6570\u5b57\u5e76\u538b\u7f29\u7a7a\u683c\ntr -ds \"[0-9]\" \" \" &lt; data.txt\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3 id=\"6-\u7279\u6b8a\u63a7\u5236\u5b57\u7b26\u901f\u67e5\u8868\">6.\u00a0<strong>\u7279\u6b8a\u63a7\u5236\u5b57\u7b26\u901f\u67e5\u8868<\/strong><\/h3>\n<figure class=\"table-figure\">\n<table>\n<thead>\n<tr>\n<th>\u901f\u8bb0\u7b26<\/th>\n<th>\u542b\u4e49<\/th>\n<th>\u516b\u8fdb\u5236\u8868\u793a<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>\\a<\/code><\/td>\n<td>\u54cd\u94c3<\/td>\n<td><code>\\007<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>\\b<\/code><\/td>\n<td>\u9000\u683c<\/td>\n<td><code>\\010<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>\\f<\/code><\/td>\n<td>\u6362\u9875<\/td>\n<td><code>\\014<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>\\n<\/code><\/td>\n<td>\u6362\u884c<\/td>\n<td><code>\\012<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>\\r<\/code><\/td>\n<td>\u56de\u8f66<\/td>\n<td><code>\\015<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>\\t<\/code><\/td>\n<td>\u5236\u8868\u7b26<\/td>\n<td><code>\\011<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>\\v<\/code><\/td>\n<td>\u5782\u76f4\u5236\u8868\u7b26<\/td>\n<td><code>\\013<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<h3 id=\"7-\u6ce8\u610f\u4e8b\u9879\">7.\u00a0<strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n<ul>\n<li><strong>\u53c2\u6570\u987a\u5e8f<\/strong>\uff1a\u5b57\u7b26\u4e321\u4e3a\u88ab\u66ff\u6362\u5b57\u7b26\uff0c\u5b57\u7b26\u4e322\u4e3a\u76ee\u6807\u5b57\u7b26\uff0c\u957f\u5ea6\u9700\u4e00\u81f4\uff08\u82e5\u5b57\u7b26\u4e322\u8f83\u77ed\uff0c\u4f1a\u91cd\u590d\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\uff09\u3002<\/li>\n<li><strong>\u7279\u6b8a\u5b57\u7b26\u8f6c\u4e49<\/strong>\uff1a\u4f7f\u7528\u53cd\u659c\u6760\u8f6c\u4e49\u7279\u6b8a\u5b57\u7b26\uff08\u5982\u00a0<code>\\*<\/code>\u00a0\u8868\u793a\u661f\u53f7\uff09\u3002<\/li>\n<li><strong>\u6548\u7387<\/strong>\uff1a<code>tr<\/code>\u00a0\u5904\u7406\u901f\u5ea6\u4f18\u4e8e\u00a0<code>sed<\/code>\uff0c\u9002\u5408\u7b80\u5355\u5b57\u7b26\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h3 id=\"8-\u5178\u578b\u5e94\u7528\u573a\u666f\">8.\u00a0<strong>\u5178\u578b\u5e94\u7528\u573a\u666f<\/strong><\/h3>\n<ul>\n<li><strong>\u6570\u636e\u6e05\u6d17<\/strong>\uff1a\u53bb\u9664\u6587\u4ef6\u4e2d\u7684\u63a7\u5236\u5b57\u7b26\u6216\u975e\u6cd5\u5b57\u7b26\u3002<\/li>\n<li><strong>\u683c\u5f0f\u8f6c\u6362<\/strong>\uff1aDOS\/UNIX \u6362\u884c\u7b26\u8f6c\u6362\u3001CSV \u5206\u9694\u7b26\u66ff\u6362\u3002<\/li>\n<li><strong>\u65e5\u5fd7\u5904\u7406<\/strong>\uff1a\u538b\u7f29\u91cd\u590d\u7a7a\u683c\u6216\u7a7a\u884c\uff0c\u63d0\u5347\u53ef\u8bfb\u6027\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u7075\u6d3b\u7ec4\u5408\u9009\u9879\u548c\u5b57\u7b26\u8303\u56f4\uff0c<code>tr<\/code>\u00a0\u53ef\u9ad8\u6548\u5b8c\u6210\u6587\u672c\u5904\u7406\u4efb\u52a1\uff0c\u662f Shell \u811a\u672c\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u4e4b\u4e00\u3002<\/p>\n<h2 id=\"\u603b\u7ed3unix\u767b\u5f55\u73af\u5883\u4e0e\u914d\u7f6e\u6587\u4ef6\">\u603b\u7ed3\uff1aUNIX\u767b\u5f55\u73af\u5883\u4e0e\u914d\u7f6e\u6587\u4ef6<\/h2>\n<h3 id=\"1\u767b\u5f55\u8fc7\u7a0b\u7684\u6838\u5fc3\u6b65\u9aa4\">1.\u767b\u5f55\u8fc7\u7a0b\u7684\u6838\u5fc3\u6b65\u9aa4<\/h3>\n<p>\u2022 \u9a8c\u8bc1\u7528\u6237\u8eab\u4efd\uff1a\u7cfb\u7edf\u901a\u8fc7\u68c0\u67e5<code>\/etc\/passwd<\/code>\u6587\u4ef6\u9a8c\u8bc1\u7528\u6237\u540d\u548c\u52a0\u5bc6\u5bc6\u7801\u3002<\/p>\n<p>\u2022 \u521d\u59cb\u5316\u73af\u5883\uff1a\u4f9d\u6b21\u6267\u884c\u5168\u5c40\u914d\u7f6e\u6587\u4ef6<code>\/etc\/profile<\/code>\u548c\u7528\u6237\u4e2a\u4eba\u914d\u7f6e\u6587\u4ef6<code>$HOME\/.profile<\/code>\u3002<\/p>\n<h3 id=\"2etcpasswd\u6587\u4ef6\u89e3\u6790\">2.<code>\/etc\/passwd<\/code>\u6587\u4ef6\u89e3\u6790<\/h3>\n<p>\u2022 \u6587\u4ef6\u7ed3\u6784\uff1a7\u4e2a\u5b57\u6bb5\uff0c\u4ee5\u5192\u53f7\u5206\u9694\uff1a<\/p>\n<ol start=\"\">\n<li>\u767b\u5f55\u540d\uff08\u5982<code>kvp<\/code>\uff09<\/li>\n<li>\u52a0\u5bc6\u5bc6\u7801\uff08\u5982<code>JFqMmk9.uRioA<\/code>\uff09<\/li>\n<li>\u7528\u6237ID\uff08\u5982<code>405<\/code>\uff09<\/li>\n<li>\u7ec4ID\uff08\u5982<code>413<\/code>\uff09<\/li>\n<li>\u7528\u6237\u5168\u540d\uff08\u5982<code>K.V.Pally<\/code>\uff09<\/li>\n<li>\u7528\u6237\u6839\u76ee\u5f55\uff08\u5982<code>\/home\/sysdev\/kvp<\/code>\uff09<\/li>\n<li>\u9ed8\u8ba4Shell\uff08\u5982<code>\/bin\/sh<\/code>\uff09 \u2022 \u6ce8\u610f\u4e8b\u9879\uff1a\u76f4\u63a5\u4fee\u6539\u9700\u8c28\u614e\uff0c\u67d0\u4e9b\u7cfb\u7edf\u53ef\u80fd\u5c06\u5bc6\u7801\u79fb\u81f3<code>\/etc\/shadow<\/code>\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"3\u5168\u5c40\u914d\u7f6e\u6587\u4ef6etcprofile\">3.\u5168\u5c40\u914d\u7f6e\u6587\u4ef6<code>\/etc\/profile<\/code><\/h3>\n<p>\u2022 \u4f5c\u7528\uff1a\u4e3a\u6240\u6709\u7528\u6237\u8bbe\u7f6e\u5168\u5c40\u73af\u5883\u3002<\/p>\n<p>\u2022 \u5173\u952e\u529f\u80fd\uff1a<\/p>\n<p>\u2022 \u73af\u5883\u53d8\u91cf\uff1a\u5982<code>LOGNAME<\/code>\u3001<code>MANPATH<\/code>\u3001<code>PATH<\/code>\u3002<\/p>\n<p>\u2022 \u7ec8\u7aef\u8bbe\u7f6e\uff1a\u901a\u8fc7<code>TERM=vt220<\/code>\u5b9a\u4e49\u9ed8\u8ba4\u7ec8\u7aef\u7c7b\u578b\u3002<\/p>\n<p>\u2022 \u5b89\u5168\u7b56\u7565\uff1a\u8bbe\u7f6e<code>umask 022<\/code>\uff08\u6587\u4ef6\u9ed8\u8ba4\u6743\u9650\u4e3a<code>755<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u7cfb\u7edf\u6d88\u606f\uff1a\u663e\u793a<code>\/etc\/motd<\/code>\uff08\u767b\u5f55\u5f53\u5929\u7684\u6d88\u606f\uff09\u3002<\/p>\n<p>\u2022 \u767b\u5f55\u9650\u5236\uff1a\u68c0\u67e5\u7528\u6237\u767b\u5f55\u6b21\u6570\uff08\u793a\u4f8b\u4e2d\u9650\u5236\u6700\u591a2\u6b21\uff09\u3002<\/p>\n<p>\u2022 \u65e5\u5fd7\u8bb0\u5f55\uff1a\u4f7f\u7528<code>logger<\/code>\u8bb0\u5f55\u767b\u5f55\/\u9000\u51fa\u4e8b\u4ef6\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\u529f\u80fd\uff1a\u8bbe\u7f6e<code>PS1<\/code>\u63d0\u793a\u7b26\u3001\u9650\u5236\u6838\u5fc3\u8f6c\u50a8\uff08<code>ulimit -c 0<\/code>\uff09\u3002<\/p>\n<h3 id=\"4\u7528\u6237\u4e2a\u4eba\u914d\u7f6e\u6587\u4ef6$homeprofile\">4.\u7528\u6237\u4e2a\u4eba\u914d\u7f6e\u6587\u4ef6<code>$HOME\/.profile<\/code><\/h3>\n<p>\u2022 \u4f5c\u7528\uff1a\u8986\u76d6\u6216\u8865\u5145\u5168\u5c40\u914d\u7f6e\uff0c\u5b9a\u5236\u7528\u6237\u4e13\u5c5e\u73af\u5883\u3002<\/p>\n<p>\u2022 \u5e38\u89c1\u5b9a\u5236\u5185\u5bb9\uff1a<\/p>\n<p>\u2022 \u8def\u5f84\u6269\u5c55\uff1a\u6dfb\u52a0\u4e2a\u4eba<code>bin<\/code>\u76ee\u5f55\u5230<code>PATH<\/code>\uff08\u5982<code>PATH=$PATH:$HOME\/bin<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u73af\u5883\u53d8\u91cf\uff1a\u8bbe\u7f6e\u7f16\u8f91\u5668\uff08<code>EDITOR=vi<\/code>\uff09\u3001\u7ec8\u7aef\u7c7b\u578b\uff08<code>TERM=vt100<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u63d0\u793a\u7b26\u5b9a\u5236\uff1a\u52a8\u6001\u663e\u793a\u4e3b\u673a\u540d\u6216\u5f53\u524d\u76ee\u5f55\uff08\u5982<code>PS1=\"$(hostname)&gt; \"<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u5b9e\u7528\u547d\u4ee4\uff1a\u767b\u5f55\u65f6\u663e\u793a\u5728\u7ebf\u7528\u6237\u6570\uff08<code>echo \"$(who | wc -l) users are on today\"<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u751f\u6548\u65b9\u5f0f\uff1a\u9000\u51fa\u91cd\u65b0\u767b\u5f55\uff0c\u6216\u6267\u884c<code>. .profile<\/code>\uff08\u6216<code>source .profile<\/code>\uff09\u3002<\/p>\n<h3 id=\"5stty\u7ec8\u7aef\u8bbe\u7f6e\">5.<code>stty<\/code>\u7ec8\u7aef\u8bbe\u7f6e<\/h3>\n<p>\u2022 \u6838\u5fc3\u529f\u80fd\uff1a\u914d\u7f6e\u7ec8\u7aef\u8f93\u5165\/\u8f93\u51fa\u884c\u4e3a\u3002<\/p>\n<p>\u2022 \u5e38\u7528\u64cd\u4f5c\uff1a<\/p>\n<p>\u2022 \u67e5\u770b\u8bbe\u7f6e\uff1a<code>stty -a<\/code>\u3002<\/p>\n<p>\u2022 \u9000\u683c\u952e\u4fee\u590d\uff1a<code>stty erase ^?<\/code>\uff08\u8bbe\u7f6e\u4e3a<code>^?<\/code>\uff09\u6216<code>stty erase ^H<\/code>\uff08\u9700\u5728\u8f93\u5165\u65f6\u4f7f\u7528<code>Ctrl-V<\/code>\u8f6c\u4e49\uff09\u3002<\/p>\n<p>\u2022 \u4fdd\u5b58\/\u6062\u590d\u8bbe\u7f6e\uff1a<\/p>\n<pre><code>```bash\nSAVEDSTTY=$(stty -g)  # \u4fdd\u5b58\u5f53\u524d\u8bbe\u7f6e\nstty -echo            # \u5173\u95ed\u56de\u663e\uff08\u5982\u8f93\u5165\u5bc6\u7801\uff09\n# ...\u6267\u884c\u64cd\u4f5c...\nstty $SAVEDSTTY       # \u6062\u590d\u539f\u59cb\u8bbe\u7f6e\n```\n<\/code><\/pre>\n<p>\u2022 \u4fe1\u53f7\u63a7\u5236\uff1a\u7981\u7528<code>Ctrl+C<\/code>\u7ec8\u6b62\uff08<code>stty intr undef<\/code>\uff09\u3002<\/p>\n<h3 id=\"6\u521b\u5efalogout\u6587\u4ef6bourne-shell\">6.\u521b\u5efa<code>.logout<\/code>\u6587\u4ef6\uff08Bourne Shell\uff09<\/h3>\n<p>\u2022 \u5b9e\u73b0\u539f\u7406\uff1a\u901a\u8fc7<code>trap<\/code>\u6355\u83b7\u9000\u51fa\u4fe1\u53f7<code>0<\/code>\u3002<\/p>\n<p>\u2022 \u914d\u7f6e\u6b65\u9aa4\uff1a<\/p>\n<ol start=\"\">\n<li>\n<p>\u5728<\/p>\n<pre><code>.profile\n<\/code><\/pre>\n<p>\u672b\u5c3e\u6dfb\u52a0\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">trap \"$HOME\/.logout\" 0\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u521b\u5efa<\/p>\n<pre><code>.logout\n<\/code><\/pre>\n<p>\u6587\u4ef6\uff0c\u5b9a\u4e49\u9000\u51fa\u65f6\u6267\u884c\u7684\u64cd\u4f5c\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u793a\u4f8b\uff1a\u6e05\u7406\u4e34\u65f6\u6587\u4ef6\u5e76\u663e\u793a\u9000\u51fa\u6d88\u606f\nrm -f $HOME\/*.log $HOME\/*.tmp\necho \"Bye...bye $LOGNAME\"\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u5c0f\u7ed3\">\u5c0f\u7ed3<\/h3>\n<ol start=\"\">\n<li>\u767b\u5f55\u6d41\u7a0b\uff1a\u8eab\u4efd\u9a8c\u8bc1\u2192\u52a0\u8f7d\u5168\u5c40\u914d\u7f6e\u2192\u52a0\u8f7d\u7528\u6237\u914d\u7f6e\u3002<\/li>\n<li>\u914d\u7f6e\u6587\u4ef6\u5c42\u7ea7\uff1a<code>\/etc\/profile<\/code>\uff08\u5168\u5c40\uff09\u2192<code>$HOME\/.profile<\/code>\uff08\u7528\u6237\u7ea7\uff09\u3002<\/li>\n<li>\u6838\u5fc3\u914d\u7f6e\u9879\uff1a\u73af\u5883\u53d8\u91cf\uff08<code>PATH<\/code>\u3001<code>TERM<\/code>\uff09\u3001\u7ec8\u7aef\u884c\u4e3a\uff08<code>stty<\/code>\uff09\u3001\u5b89\u5168\u7b56\u7565\uff08<code>umask<\/code>\uff09\u3002<\/li>\n<li>\u7ec8\u7aef\u8c03\u8bd5\uff1a\u4f7f\u7528<code>stty<\/code>\u89e3\u51b3\u9000\u683c\u952e\u7b49\u95ee\u9898\uff0c\u4fdd\u5b58\/\u6062\u590d\u7ec8\u7aef\u72b6\u6001\u3002<\/li>\n<li>\u6269\u5c55\u529f\u80fd\uff1a\u901a\u8fc7<code>.logout<\/code>\u5b9e\u73b0\u9000\u51fa\u65f6\u7684\u81ea\u5b9a\u4e49\u6e05\u7406\u6216\u901a\u77e5\u3002<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u5408\u7406\u914d\u7f6e\u8fd9\u4e9b\u6587\u4ef6\uff0c\u7528\u6237\u53ef\u9ad8\u6548\u5b9a\u5236UNIX\u73af\u5883\uff0c\u63d0\u5347\u5b89\u5168\u6027\u548c\u4f7f\u7528\u4f53\u9a8c\u3002<\/p>\n<h2 id=\"\u7b2c14\u7ae0-\u73af\u5883\u4e0eshell\u53d8\u91cf\u603b\u7ed3\">\u7b2c14\u7ae0 \u73af\u5883\u4e0eShell\u53d8\u91cf\u603b\u7ed3<\/h2>\n<h3 id=\"\u4e00shell\u53d8\u91cf\u6982\u8ff0\">\u4e00\u3001Shell\u53d8\u91cf\u6982\u8ff0<\/h3>\n<p>\u2022 \u4f5c\u7528\uff1a\u5b9a\u5236\u7528\u6237\u73af\u5883\uff0c\u4fdd\u5b58\u8def\u5f84\u3001\u6587\u4ef6\u540d\u3001\u6570\u503c\u7b49\u4fe1\u606f\uff0c\u5747\u4e3a\u6587\u672c\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u2022 \u5206\u7c7b\uff1a<\/p>\n<p>\u2022 \u672c\u5730\u53d8\u91cf\uff1a\u4ec5\u5f53\u524dShell\u8fdb\u7a0b\u6709\u6548\uff0c\u5b50\u8fdb\u7a0b\u65e0\u6cd5\u7ee7\u627f\u3002<\/p>\n<p>\u2022 \u73af\u5883\u53d8\u91cf\uff1a\u53ef\u4f20\u9012\u7ed9\u5b50\u8fdb\u7a0b\uff0c\u9700\u7528<code>export<\/code>\u5bfc\u51fa\u3002<\/p>\n<p>\u2022 \u7279\u6b8a\u53d8\u91cf\uff1a\u53ea\u8bfb\uff0c\u7528\u4e8e\u4f20\u9012\u53c2\u6570\uff08\u5982\u4f4d\u7f6e\u53c2\u6570\u548c\u7279\u5b9a\u53d8\u91cf\uff09\u3002<\/p>\n<hr \/>\n<h3 id=\"\u4e8c\u672c\u5730\u53d8\u91cf\">\u4e8c\u3001\u672c\u5730\u53d8\u91cf<\/h3>\n<ol start=\"\">\n<li>\n<p>\u8bbe\u7f6e\u4e0e\u4f7f\u7528\uff1a \u2022 \u8bed\u6cd5\uff1a<code>\u53d8\u91cf\u540d=\u503c<\/code>\uff08\u7b49\u53f7\u4e24\u4fa7\u65e0\u7a7a\u683c\uff0c\u503c\u542b\u7a7a\u683c\u9700\u5f15\u53f7\uff09\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<code>GREAT_PICTURE=\"die hard\"<\/code>\uff0c\u901a\u8fc7<code>${\u53d8\u91cf\u540d}<\/code>\u5f15\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>\u64cd\u4f5c\uff1a \u2022 \u663e\u793a\uff1a<code>echo ${\u53d8\u91cf\u540d}<\/code>\u3002<\/p>\n<p>\u2022 \u6e05\u9664\uff1a<code>unset \u53d8\u91cf\u540d<\/code>\u3002<\/p>\n<p>\u2022 \u62fc\u63a5\uff1a\u76f4\u63a5\u5e76\u5217\u53d8\u91cf\uff0c\u5982<code>${FIRST}${LAST}<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>\u6d4b\u8bd5\u4e0e\u9ed8\u8ba4\u503c\uff1a \u2022\u00a0<code>${\u53d8\u91cf:-\u9ed8\u8ba4\u503c}<\/code>\uff1a\u53d8\u91cf\u672a\u8bbe\u7f6e\u65f6\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002<\/p>\n<p>\u2022\u00a0<code>${\u53d8\u91cf:=\u9ed8\u8ba4\u503c}<\/code>\uff1a\u53d8\u91cf\u672a\u8bbe\u7f6e\u65f6\u8d4b\u503c\u9ed8\u8ba4\u503c\u3002<\/p>\n<p>\u2022\u00a0<code>${\u53d8\u91cf:?\u9519\u8bef\u4fe1\u606f}<\/code>\uff1a\u53d8\u91cf\u672a\u8bbe\u7f6e\u65f6\u62a5\u9519\u9000\u51fa\u3002<\/p>\n<\/li>\n<li>\n<p>\u53ea\u8bfb\u53d8\u91cf\uff1a \u2022 \u8bbe\u7f6e\uff1a<code>readonly \u53d8\u91cf\u540d<\/code>\uff0c\u4e0d\u53ef\u4fee\u6539\u6216\u6e05\u9664\u3002<\/p>\n<p>\u2022 \u67e5\u770b\uff1a<code>readonly<\/code>\u547d\u4ee4\u5217\u51fa\u6240\u6709\u53ea\u8bfb\u53d8\u91cf\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e09\u73af\u5883\u53d8\u91cf\">\u4e09\u3001\u73af\u5883\u53d8\u91cf<\/h3>\n<ol start=\"\">\n<li>\n<p>\u8bbe\u7f6e\u4e0e\u5bfc\u51fa\uff1a \u2022 \u65b9\u5f0f\u4e00\uff1a<code>\u53d8\u91cf\u540d=\u503c; export \u53d8\u91cf\u540d<\/code>\u3002<\/p>\n<p>\u2022 \u65b9\u5f0f\u4e8c\uff1a\u5206\u4e24\u6b65\uff08<code>\u53d8\u91cf\u540d=\u503c<\/code>\u540e<code>export<\/code>\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>\u5e38\u7528\u73af\u5883\u53d8\u91cf\uff1a \u2022 PATH\uff1a\u547d\u4ee4\u641c\u7d22\u8def\u5f84\uff0c\u683c\u5f0f\u4e3a\u5192\u53f7\u5206\u9694\u7684\u76ee\u5f55\u5217\u8868\u3002<\/p>\n<p>\u2022 HOME\uff1a\u7528\u6237\u4e3b\u76ee\u5f55\uff0c<code>cd<\/code>\u547d\u4ee4\u7684\u9ed8\u8ba4\u76ee\u6807\u3002<\/p>\n<p>\u2022 PS1\/PS2\uff1a\u4e3b\/\u6b21\u63d0\u793a\u7b26\uff08\u5982<code>PS1=\"\\u@\\h:\\w\\$ \"<\/code>\uff09\u3002<\/p>\n<p>\u2022 TERM\uff1a\u7ec8\u7aef\u7c7b\u578b\uff08\u5982<code>vt100<\/code>\uff09\u3002<\/p>\n<p>\u2022 LANG\/LC_*\uff1a\u8bed\u8a00\u548c\u533a\u57df\u8bbe\u7f6e\u3002<\/p>\n<p>\u2022 USER\/LOGNAME\uff1a\u5f53\u524d\u7528\u6237\u540d\u3002<\/p>\n<\/li>\n<li>\n<p>\u5176\u4ed6\u53d8\u91cf\uff1a \u2022 EDITOR\uff1a\u9ed8\u8ba4\u7f16\u8f91\u5668\uff08\u5982<code>vi<\/code>\uff09\u3002<\/p>\n<p>\u2022 PAGER\uff1a\u5206\u9875\u7a0b\u5e8f\uff08\u5982<code>more<\/code>\u6216<code>less<\/code>\uff09\u3002<\/p>\n<p>\u2022 MANPATH\uff1a\u624b\u518c\u9875\u641c\u7d22\u8def\u5f84\u3002<\/p>\n<\/li>\n<li>\n<p>\u67e5\u770b\u4e0e\u6e05\u9664\uff1a \u2022 \u663e\u793a\u6240\u6709\u73af\u5883\u53d8\u91cf\uff1a<code>env<\/code>\u6216<code>printenv<\/code>\u3002<\/p>\n<p>\u2022 \u6e05\u9664\uff1a<code>unset \u53d8\u91cf\u540d<\/code>\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u56db\u53d8\u91cf\u66ff\u6362\u4e0e\u6269\u5c55\">\u56db\u3001\u53d8\u91cf\u66ff\u6362\u4e0e\u6269\u5c55<\/h3>\n<p>\u2022 \u66ff\u6362\u6a21\u5f0f\uff1a<\/p>\n<p>\u2022\u00a0<code>${\u53d8\u91cf:-\u503c}<\/code>\uff1a\u53d8\u91cf\u672a\u5b9a\u4e49\u65f6\u8fd4\u56de\u9ed8\u8ba4\u503c\u3002<\/p>\n<p>\u2022\u00a0<code>${\u53d8\u91cf:=\u503c}<\/code>\uff1a\u53d8\u91cf\u672a\u5b9a\u4e49\u65f6\u8d4b\u503c\u9ed8\u8ba4\u503c\u5e76\u8fd4\u56de\u3002<\/p>\n<p>\u2022\u00a0<code>${\u53d8\u91cf:+\u503c}<\/code>\uff1a\u53d8\u91cf\u5df2\u5b9a\u4e49\u65f6\u8fd4\u56de\u6307\u5b9a\u503c\u3002<\/p>\n<p>\u2022\u00a0<code>${\u53d8\u91cf:?\u4fe1\u606f}<\/code>\uff1a\u53d8\u91cf\u672a\u5b9a\u4e49\u65f6\u62a5\u9519\u5e76\u663e\u793a\u4fe1\u606f\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">echo ${COLOR:-red}    # \u672a\u5b9a\u4e49COLOR\u65f6\u8f93\u51fared\necho ${COLOR:=blue}   # \u672a\u5b9a\u4e49\u5219\u8d4b\u503cblue\u5e76\u8f93\u51fa\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u4e94\u4f4d\u7f6e\u53c2\u6570\u4e0e\u7279\u5b9a\u53d8\u91cf\">\u4e94\u3001\u4f4d\u7f6e\u53c2\u6570\u4e0e\u7279\u5b9a\u53d8\u91cf<\/h3>\n<ol start=\"\">\n<li>\n<p>\u4f4d\u7f6e\u53c2\u6570\uff1a \u2022\u00a0<code>$0<\/code>\uff1a\u811a\u672c\u540d\u79f0\u3002<\/p>\n<p>\u2022\u00a0<code>$1-$9<\/code>\uff1a\u7b2c1\u5230\u7b2c9\u4e2a\u53c2\u6570\u3002<\/p>\n<p>\u2022\u00a0<code>$#<\/code>\uff1a\u4f20\u9012\u7684\u53c2\u6570\u4e2a\u6570\u3002<\/p>\n<p>\u2022\u00a0<code>$*<\/code>\u548c<code>$@<\/code>\uff1a\u6240\u6709\u53c2\u6570\uff08<code>$*<\/code>\u5408\u5e76\u4e3a\u5355\u5b57\u7b26\u4e32\uff0c<code>$@<\/code>\u4fdd\u7559\u5206\u9694\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>\u7279\u5b9a\u53d8\u91cf\uff1a \u2022\u00a0<code>$$<\/code>\uff1a\u5f53\u524d\u8fdb\u7a0bID\u3002<\/p>\n<p>\u2022\u00a0<code>$!<\/code>\uff1a\u540e\u53f0\u6700\u540e\u4e00\u4e2a\u8fdb\u7a0b\u7684ID\u3002<\/p>\n<p>\u2022\u00a0<code>$?<\/code>\uff1a\u4e0a\u4e00\u4e2a\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\uff080\u4e3a\u6210\u529f\uff0c\u975e0\u4e3a\u5931\u8d25\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>$-<\/code>\uff1a\u5f53\u524dShell\u9009\u9879\uff08\u5982<code>set -x<\/code>\u7684\u8ddf\u8e2a\u6a21\u5f0f\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>\u5e94\u7528\u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u811a\u672c\u4e2d\u68c0\u67e5\u53c2\u6570\u4e2a\u6570\nif [ $# -eq 0 ]; then\n  echo \"Usage: $0 &lt;file&gt;\"\n  exit 1\nfi\n# \u68c0\u67e5\u547d\u4ee4\u662f\u5426\u6210\u529f\ncp file.txt \/backup\/ &amp;&amp; echo \"\u5907\u4efd\u6210\u529f\" || echo \"\u5907\u4efd\u5931\u8d25: $?\"\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u516d\u5411\u811a\u672c\u4f20\u9012\u53c2\u6570\">\u516d\u3001\u5411\u811a\u672c\u4f20\u9012\u53c2\u6570<\/h3>\n<p>\u2022 \u811a\u672c\u5185\u4f7f\u7528\u53c2\u6570\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">echo \"\u811a\u672c\u540d: $0\"\necho \"\u7b2c\u4e00\u4e2a\u53c2\u6570: $1\"\necho \"\u6240\u6709\u53c2\u6570: $*\"\n<\/code><\/pre>\n<p>\u2022 \u7ed3\u5408\u7cfb\u7edf\u547d\u4ee4\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">grep $1 \/etc\/passwd   # \u5c06\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f5c\u4e3a\u641c\u7d22\u5173\u952e\u5b57\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u4e03\u6700\u4f73\u5b9e\u8df5\">\u4e03\u3001\u6700\u4f73\u5b9e\u8df5<\/h3>\n<ol start=\"\">\n<li>\u53d8\u91cf\u547d\u540d\uff1a\u73af\u5883\u53d8\u91cf\u4f7f\u7528\u5927\u5199\uff0c\u672c\u5730\u53d8\u91cf\u5c0f\u5199\u3002<\/li>\n<li>\u9519\u8bef\u5904\u7406\uff1a\u5229\u7528<code>$?<\/code>\u68c0\u67e5\u547d\u4ee4\u6267\u884c\u7ed3\u679c\u3002<\/li>\n<li>\u9ed8\u8ba4\u503c\uff1a\u811a\u672c\u4e2d\u4e3a\u5173\u952e\u53d8\u91cf\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c\u907f\u514d\u672a\u5b9a\u4e49\u9519\u8bef\u3002<\/li>\n<li>\u53ea\u8bfb\u4fdd\u62a4\uff1a\u5173\u952e\u53d8\u91cf\u8bbe\u4e3a\u53ea\u8bfb\uff08\u5982<code>readonly PATH<\/code>\uff09\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u516b\u793a\u4f8b\u6c47\u603b\">\u516b\u3001\u793a\u4f8b\u6c47\u603b<\/h3>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u672c\u5730\u53d8\u91cf\u4e0e\u73af\u5883\u53d8\u91cf\nname=\"Alice\"\nexport name  # \u5bfc\u51fa\u4e3a\u73af\u5883\u53d8\u91cf\n\n# \u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\necho \"\u811a\u672c\u540d: $0, \u53c2\u65701: $1, \u53c2\u6570\u603b\u6570: $#\"\n\n# \u68c0\u67e5\u6587\u4ef6\u5b58\u5728\u6027\nFILE=${1:-default.txt}\nif [ -f \"$FILE\" ]; then\n  echo \"\u5904\u7406\u6587\u4ef6: $FILE\"\nelse\n  echo \"\u6587\u4ef6\u4e0d\u5b58\u5728: $FILE\"\nfi\n\n# \u9000\u51fa\u72b6\u6001\u68c0\u67e5\ntar -czf backup.tar.gz \/data\nif [ $? -ne 0 ]; then\n  echo \"\u538b\u7f29\u5931\u8d25!\"\n  exit 1\nfi\n<\/code><\/pre>\n<hr \/>\n<p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6982\u5ff5\uff0c\u80fd\u591f\u6709\u6548\u7ba1\u7406Shell\u811a\u672c\u4e2d\u7684\u53d8\u91cf\uff0c\u63d0\u5347\u811a\u672c\u7684\u7075\u6d3b\u6027\u548c\u5065\u58ee\u6027\u3002<\/p>\n<h2 id=\"\u7b2c15\u7ae0-\u5f15\u53f7-\u603b\u7ed3\">\u7b2c15\u7ae0 \u5f15\u53f7 \u603b\u7ed3<\/h2>\n<h3 id=\"1-\u5f15\u7528\u7684\u5fc5\u8981\u6027\"><strong>1. \u5f15\u7528\u7684\u5fc5\u8981\u6027<\/strong><\/h3>\n<p>\u2022 \u95ee\u9898\u80cc\u666f\uff1aShell\u5728\u89e3\u6790\u811a\u672c\u65f6\u4f1a\u5bf9\u7279\u6b8a\u5b57\u7b26\uff08\u5982<code>*<\/code>\u3001<code>$<\/code>\u3001\u7a7a\u683c\u7b49\uff09\u8fdb\u884c\u89e3\u91ca\uff0c\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u884c\u4e3a\u3002\u4f8b\u5982\uff0c\u672a\u52a0\u5f15\u53f7\u7684<code>*<\/code>\u4f1a\u88ab\u5c55\u5f00\u4e3a\u6587\u4ef6\u5217\u8868\uff0c\u53d8\u91cf<code>$VAR<\/code>\u4f1a\u88ab\u66ff\u6362\u4e3a\u503c\u3002<\/p>\n<p>\u2022 \u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528\u5f15\u53f7\u5305\u88f9\u5b57\u7b26\u4e32\u6216\u53d8\u91cf\uff0c\u963b\u6b62Shell\u9519\u8bef\u89e3\u91ca\u7279\u6b8a\u5b57\u7b26\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u672a\u5f15\u7528\u65f6\uff0c*\u88ab\u5c55\u5f00\u4e3a\u6587\u4ef6\u5217\u8868\necho Hit the star button to exit*  # \u8f93\u51fa\u610f\u5916\u7ed3\u679c\n# \u53cc\u5f15\u53f7\u5305\u88f9\u540e\uff0c*\u88ab\u5f53\u4f5c\u666e\u901a\u5b57\u7b26\necho \"Hit the star button to exit*\"  # \u6b63\u786e\u8f93\u51fa\n<\/code><\/pre>\n<h3 id=\"2-\u53cc\u5f15\u53f7\"><strong>2. \u53cc\u5f15\u53f7\uff08<code>\"<\/code>\uff09<\/strong><\/h3>\n<p>\u2022 \u4f5c\u7528\uff1a\u5141\u8bb8\u53d8\u91cf\u66ff\u6362\uff08<code>$VAR<\/code>\uff09\u548c\u547d\u4ee4\u66ff\u6362\uff08<code><\/code>\uff09\uff0c\u4f46\u5c4f\u853d\u5927\u90e8\u5206\u7279\u6b8a\u5b57\u7b26\uff08\u5982<code>*<\/code>\u3001<code>?<\/code>\u7b49\uff09\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">STRING=\"MAY DAY, MAY DAY\"\necho \"$STRING\"  # \u6b63\u786e\u8f93\u51fa\u53d8\u91cf\u503c\nMYDATE=\"date\"   # MYDATE\u4fdd\u5b58\u5b57\u7b26\u4e32\"date\"\uff0c\u800c\u975e\u6267\u884c\u547d\u4ee4\ngrep \"Davey Wire\" \/etc\/passwd  # \u6b63\u786e\u641c\u7d22\u542b\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\n<\/code><\/pre>\n<p>\u2022 \u53d8\u91cf\u66ff\u6362\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">BOY=\"boy\"\necho \"The $BOY did well\"    # \u8f93\u51fa\uff1aThe boy did well\necho \"The \\$BOY did well\"   # \u8f93\u51fa\uff1aThe $BOY did well\uff08\u5c4f\u853d$\uff09\n<\/code><\/pre>\n<h3 id=\"3-\u5355\u5f15\u53f7\"><strong>3. \u5355\u5f15\u53f7\uff08<code>'<\/code>\uff09<\/strong><\/h3>\n<p>\u2022 \u4f5c\u7528\uff1a\u5b8c\u5168\u5c4f\u853d\u6240\u6709\u7279\u6b8a\u5b57\u7b26\uff0c\u5305\u62ec\u53d8\u91cf\u548c\u547d\u4ee4\u66ff\u6362\uff0c\u5185\u5bb9\u6309\u5b57\u9762\u8f93\u51fa\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">GIRL='girl'\necho 'The $GIRL did well'  # \u8f93\u51fa\uff1aThe $GIRL did well\n<\/code><\/pre>\n<h3 id=\"4-\u53cd\u5f15\u53f7\"><strong>4. \u53cd\u5f15\u53f7\uff08<code><\/code>\uff09<\/strong><\/h3>\n<p>\u2022 \u4f5c\u7528\uff1a\u6267\u884c\u547d\u4ee4\u5e76\u5c06\u8f93\u51fa\u7ed3\u679c\u4f5c\u4e3a\u53d8\u91cf\u503c\uff08\u547d\u4ee4\u66ff\u6362\uff09\u3002\u73b0\u4ee3\u811a\u672c\u63a8\u8350\u4f7f\u7528<code>$()<\/code>\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">mydate=`date +\"%A the %e of %B %Y\"`  # \u4fdd\u5b58\u683c\u5f0f\u5316\u65e5\u671f\necho \"Today is `date`\"                # \u5d4c\u5165\u547d\u4ee4\u8f93\u51fa\necho \"Users: `who | wc -l`\"           # \u7edf\u8ba1\u5f53\u524d\u7528\u6237\u6570\n<\/code><\/pre>\n<h3 id=\"5-\u53cd\u659c\u7ebf\"><strong>5. \u53cd\u659c\u7ebf\uff08<code>\\<\/code>\uff09<\/strong><\/h3>\n<p>\u2022 \u4f5c\u7528\uff1a\u8f6c\u4e49\u5355\u4e2a\u5b57\u7b26\uff0c\u5c4f\u853d\u5176\u7279\u6b8a\u542b\u4e49\uff08\u5982<code>$<\/code>\u3001<code>*<\/code>\u3001\u7a7a\u683c\uff09\u3002<\/p>\n<p>\u2022 \u5173\u952e\u573a\u666f\uff1a<\/p>\n<p>\u2022 \u8f93\u51fa\u7279\u6b8a\u5b57\u7b26\uff1a<\/p>\n<pre><code>```bash\necho \"Price: \\$19.99\"    # \u8f93\u51fa\uff1aPrice: $19.99\necho \"Copyright sign: \\251\"  # \u9700\u914d\u5408`echo -e`\u663e\u793aASCII\u5b57\u7b26\n```\n<\/code><\/pre>\n<p>\u2022 \u907f\u514d\u8bed\u6cd5\u9519\u8bef\uff1a<\/p>\n<pre><code>```bash\nexpr 12 \\* 12  # \u6b63\u786e\u8ba1\u7b97\u4e58\u6cd5\uff08\u672a\u8f6c\u4e49*\u4f1a\u62a5\u9519\uff09\n```\n<\/code><\/pre>\n<h3 id=\"6-\u5e38\u89c1\u9519\u8bef\u4e0e\u5904\u7406\"><strong>6. \u5e38\u89c1\u9519\u8bef\u4e0e\u5904\u7406<\/strong><\/h3>\n<p>\u2022 \u672a\u5f15\u7528\u542b\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">grep Davey Wire \/etc\/passwd  # \u9519\u8bef\uff1aWire\u88ab\u89c6\u4e3a\u6587\u4ef6\u540d\ngrep \"Davey Wire\" \/etc\/passwd  # \u6b63\u786e\n<\/code><\/pre>\n<p>\u2022 \u6df7\u5408\u5f15\u53f7\u4e0e\u53d8\u91cf\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u5355\u5f15\u53f7\u5185\u53d8\u91cf\u4e0d\u4f1a\u5c55\u5f00\necho 'Today is $MYDATE'     # \u8f93\u51fa\uff1aToday is $MYDATE\n# \u53cc\u5f15\u53f7\u5185\u9700\u8f6c\u4e49$\u4ee5\u8f93\u51fa\u5b57\u9762\u503c\necho \"Today is \\$MYDATE\"    # \u8f93\u51fa\uff1aToday is $MYDATE\n<\/code><\/pre>\n<h3 id=\"7-\u7ecf\u9a8c\u6cd5\u5219\"><strong>7. \u7ecf\u9a8c\u6cd5\u5219<\/strong><\/h3>\n<ol start=\"\">\n<li>\u9ed8\u8ba4\u4f7f\u7528\u53cc\u5f15\u53f7\uff1a\u5904\u7406\u5b57\u7b26\u4e32\u65f6\u4f18\u5148\u4f7f\u7528\u53cc\u5f15\u53f7\uff0c\u5141\u8bb8\u53d8\u91cf\u66ff\u6362\u4e14\u907f\u514d\u591a\u6570\u610f\u5916\u5c55\u5f00\u3002<\/li>\n<li>\u7075\u6d3b\u5207\u6362\u5f15\u7528\u65b9\u5f0f\uff1a\u82e5\u53cc\u5f15\u53f7\u4e0d\u9002\u7528\uff08\u5982\u9700\u5b8c\u5168\u5c4f\u853d\u53d8\u91cf\uff09\uff0c\u5c1d\u8bd5\u5355\u5f15\u53f7\u6216\u53cd\u659c\u7ebf\u8f6c\u4e49\u3002<\/li>\n<\/ol>\n<h3 id=\"\u603b\u7ed3\u8868\u683c\"><strong>\u603b\u7ed3\u8868\u683c<\/strong><\/h3>\n<figure class=\"table-figure\">\n<table>\n<thead>\n<tr>\n<th>\u5f15\u7528\u7c7b\u578b<\/th>\n<th>\u8bed\u6cd5<\/th>\n<th>\u4f5c\u7528<\/th>\n<th>\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u53cc\u5f15\u53f7<\/td>\n<td><code>\"...\"<\/code><\/td>\n<td>\u5141\u8bb8\u53d8\u91cf\u548c\u547d\u4ee4\u66ff\u6362\uff0c\u5c4f\u853d\u9664<code>$<\/code>\u3001<code><\/code>\u3001<code>\\<\/code>\u5916\u7684\u7279\u6b8a\u5b57\u7b26<\/td>\n<td><code>echo \"Value: $VAR\"<\/code><\/td>\n<\/tr>\n<tr>\n<td>\u5355\u5f15\u53f7<\/td>\n<td><code>'...'<\/code><\/td>\n<td>\u5b8c\u5168\u5c4f\u853d\u6240\u6709\u7279\u6b8a\u5b57\u7b26\uff0c\u6309\u5b57\u9762\u8f93\u51fa<\/td>\n<td><code>echo 'Value: $VAR'<\/code><\/td>\n<\/tr>\n<tr>\n<td>\u53cd\u5f15\u53f7<\/td>\n<td><code>...<\/code><\/td>\n<td>\u6267\u884c\u547d\u4ee4\u5e76\u66ff\u6362\u4e3a\u8f93\u51fa\u7ed3\u679c<\/td>\n<td><code>mydate=<\/code>date&#8220;<\/td>\n<\/tr>\n<tr>\n<td>\u53cd\u659c\u7ebf<\/td>\n<td><code>\\<\/code><\/td>\n<td>\u8f6c\u4e49\u5355\u4e2a\u5b57\u7b26\uff0c\u5c4f\u853d\u5176\u7279\u6b8a\u542b\u4e49<\/td>\n<td><code>echo \"Price: \\$10\"<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>\u901a\u8fc7\u6b63\u786e\u4f7f\u7528\u5f15\u53f7\uff0c\u53ef\u7cbe\u786e\u63a7\u5236Shell\u5bf9\u5b57\u7b26\u4e32\u3001\u53d8\u91cf\u548c\u547d\u4ee4\u7684\u89e3\u91ca\uff0c\u907f\u514d\u811a\u672c\u4e2d\u7684\u5e38\u89c1\u9519\u8bef\u3002<\/p>\n<h2 id=\"\u7b2c\u56db\u90e8\u5206\u57fa\u7840shell\u7f16\u7a0b-\u7b2c16\u7ae0\u603b\u7ed3\">\u7b2c\u56db\u90e8\u5206\u57fa\u7840shell\u7f16\u7a0b \u7b2c16\u7ae0\u603b\u7ed3<\/h2>\n<h3 id=\"\u4e00\u4f7f\u7528shell\u811a\u672c\u7684\u539f\u56e0\">\u4e00\u3001\u4f7f\u7528Shell\u811a\u672c\u7684\u539f\u56e0<\/h3>\n<ol>\n<li>\n<p>\u81ea\u52a8\u5316\u4e0e\u6548\u7387 \u2022 \u5904\u7406\u91cd\u590d\u6027\u4efb\u52a1\u6216\u590d\u6742\u64cd\u4f5c\u65f6\uff0c\u901a\u8fc7\u811a\u672c\u6279\u91cf\u6267\u884c\u547d\u4ee4\uff0c\u8282\u7701\u624b\u52a8\u8f93\u5165\u65f6\u95f4\u3002<\/p>\n<p>\u2022 \u652f\u6301\u7ec4\u5408\u591a\u4e2a\u547d\u4ee4\uff0c\u5904\u7406\u6587\u4ef6\u5206\u7c7b\u3001\u6587\u672c\u63d2\u5165\u3001\u65e5\u5fd7\u6e05\u7406\u7b49\u7cfb\u7edf\u7ba1\u7406\u4efb\u52a1\u3002<\/p>\n<\/li>\n<li>\n<p>\u7075\u6d3b\u6027\u4e0e\u529f\u80fd\u6269\u5c55 \u2022 \u652f\u6301\u53d8\u91cf\u3001\u6761\u4ef6\u5224\u65ad\u3001\u7b97\u672f\u8fd0\u7b97\u548c\u5faa\u73af\uff0c\u589e\u5f3a\u4efb\u52a1\u5904\u7406\u80fd\u529b\u3002<\/p>\n<p>\u2022 \u53ef\u901a\u8fc7\u63a5\u6536\u547d\u4ee4\u884c\u53c2\u6570\u52a8\u6001\u8c03\u6574\u884c\u4e3a\u3002<\/p>\n<\/li>\n<li>\n<p>\u8de8\u7cfb\u7edf\u9002\u5e94\u6027 \u2022 Shell\u811a\u672c\u672c\u8eab\u53ef\u79fb\u690d\u6027\u8f83\u597d\uff0c\u4f46\u9700\u6ce8\u610f\u4e0d\u540cUNIX\/Linux\u7cfb\u7edf\u7684\u547d\u4ee4\u5dee\u5f02\uff08\u5982\u9009\u9879\u3001\u8def\u5f84\u7b49\uff09\u3002<\/p>\n<\/li>\n<\/ol>\n<h3 id=\"\u4e8cshell\u811a\u672c\u57fa\u672c\u5143\u7d20\">\u4e8c\u3001Shell\u811a\u672c\u57fa\u672c\u5143\u7d20<\/h3>\n<ol>\n<li>\n<p>\u811a\u672c\u7ed3\u6784 \u2022 Shebang\u884c\uff1a\u9996\u884c\u5fc5\u987b\u4e3a\u00a0<code>#!\/bin\/sh<\/code>\uff0c\u6307\u5b9a\u4f7f\u7528Bourne Shell\u89e3\u91ca\u5668\u3002<\/p>\n<p>\u2022 \u6ce8\u91ca\uff1a\u4ee5\u00a0<code>#<\/code>\u00a0\u5f00\u5934\uff0c\u5efa\u8bae\u7b2c\u4e8c\u884c\u6ce8\u91ca\u6ce8\u660e\u811a\u672c\u540d\u79f0\u53ca\u529f\u80fd\u3002<\/p>\n<\/li>\n<li>\n<p>\u6267\u884c\u903b\u8f91 \u2022 \u6309\u987a\u5e8f\u9010\u884c\u89e3\u91ca\u6267\u884c\u547d\u4ee4\uff0c\u65e0\u9700\u7f16\u8bd1\u3002<\/p>\n<p>\u2022 \u9700\u8d4b\u4e88\u6267\u884c\u6743\u9650\uff1a<code>chmod u+x \u811a\u672c\u540d<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>\u7f16\u5199\u539f\u5219 \u2022 \u4f18\u5148\u8ffd\u6c42\u7b80\u6d01\u548c\u9ad8\u53ef\u8bfb\u6027\uff0c\u907f\u514d\u8fc7\u5ea6\u590d\u6742\u5316\u3002<\/p>\n<p>\u2022 \u4fdd\u7559\u8c03\u8bd5\u57fa\u7840\uff1a\u5373\u4f7f\u7ed3\u679c\u5f02\u5e38\uff0c\u4e5f\u5e94\u4fdd\u5b58\u811a\u672c\u4ee5\u4fbf\u4fee\u6539\u3002<\/p>\n<\/li>\n<\/ol>\n<h3 id=\"\u4e09\u8fd0\u884cshell\u811a\u672c\u7684\u6b65\u9aa4\">\u4e09\u3001\u8fd0\u884cShell\u811a\u672c\u7684\u6b65\u9aa4<\/h3>\n<ol>\n<li>\n<p>\u521b\u5efa\u4e0e\u6743\u9650\u8bbe\u7f6e<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u793a\u4f8b\u811a\u672c cleanup\n#!\/bin\/sh\n# name: cleanup\n# \u529f\u80fd\uff1a\u6e05\u7406\u65e5\u5fd7\u548c\u7cfb\u7edf\u6d88\u606f\u6587\u4ef6\necho \"starting cleanup...wait\"\nrm \/usr\/local\/apps\/log\/*.log          # \u5220\u9664\u65e5\u5fd7\u6587\u4ef6\ntail -40 \/var\/adm\/messages &gt; \/tmp\/messages  # \u4fdd\u7559\u6700\u65b040\u884c\u6d88\u606f\nrm \/var\/adm\/messages\nmv \/tmp\/messages \/var\/adm\/messages    # \u6062\u590d\u622a\u65ad\u540e\u7684\u6d88\u606f\u6587\u4ef6\necho \"finished cleanup\"\n<\/code><\/pre>\n<p>\u2022 \u6dfb\u52a0\u6267\u884c\u6743\u9650\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">chmod u+x cleanup\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u8fd0\u884c\u65b9\u5f0f \u2022 \u76f4\u63a5\u8fd0\u884c\uff08\u9700\u5728PATH\u4e2d\uff09\uff1a<code>.\/cleanup<\/code>\u3002<\/p>\n<p>\u2022 \u82e5\u63d0\u793a\u201ccommand not found\u201d\uff0c\u9700\u8bbe\u7f6e\u811a\u672c\u8def\u5f84\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">mkdir ~\/bin                # \u521b\u5efa\u7528\u6237\u53ef\u6267\u884c\u76ee\u5f55\uff08\u82e5\u4e0d\u5b58\u5728\uff09\nmv cleanup ~\/bin\/         # \u79fb\u52a8\u811a\u672c\u5230\u53ef\u6267\u884c\u76ee\u5f55\necho 'export PATH=$PATH:$HOME\/bin' &gt;&gt; ~\/.profile  # \u6dfb\u52a0PATH\u53d8\u91cf\nsource ~\/.profile         # \u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3 id=\"\u56db\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\">\u56db\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3<\/h3>\n<ol>\n<li>\n<p>\u8def\u5f84\u95ee\u9898 \u2022 \u786e\u4fdd\u811a\u672c\u6240\u5728\u76ee\u5f55\uff08\u5982\u00a0<code>~\/bin<\/code>\uff09\u5df2\u52a0\u5165\u00a0<code>PATH<\/code>\u00a0\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p>\u6743\u9650\u95ee\u9898 \u2022 \u68c0\u67e5\u811a\u672c\u662f\u5426\u5177\u6709\u53ef\u6267\u884c\u6743\u9650\uff08<code>ls -l<\/code>\u00a0\u67e5\u770b\u6743\u9650\u4f4d\u542b\u00a0<code>x<\/code>\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>\u811a\u672c\u8c03\u8bd5 \u2022 \u6267\u884c\u65f6\u9010\u884c\u68c0\u67e5\u9519\u8bef\u8f93\u51fa\uff0c\u5b9a\u4f4d\u547d\u4ee4\u5931\u8d25\u4f4d\u7f6e\u3002<\/p>\n<p>\u2022 \u4fdd\u7559\u4e2d\u95f4\u7ed3\u679c\uff08\u5982\u00a0<code>\/tmp\/messages<\/code>\uff09\u8f85\u52a9\u6392\u67e5\u95ee\u9898\u3002<\/p>\n<\/li>\n<\/ol>\n<h3 id=\"\u7b2c17\u7ae0\u6761\u4ef6\u6d4b\u8bd5\u603b\u7ed3\">\u7b2c17\u7ae0\u6761\u4ef6\u6d4b\u8bd5\u603b\u7ed3<\/h3>\n<p><strong>\u6838\u5fc3\u5185\u5bb9<\/strong>\u00a0\u672c\u7ae0\u4e3b\u8981\u8bb2\u89e3\u5728Shell\u811a\u672c\u4e2d\u5982\u4f55\u4f7f\u7528<code>test<\/code>\u548c<code>expr<\/code>\u547d\u4ee4\u8fdb\u884c\u6761\u4ef6\u6d4b\u8bd5\uff0c\u6db5\u76d6\u6587\u4ef6\u72b6\u6001\u3001\u5b57\u7b26\u4e32\u3001\u6570\u503c\u7684\u6d4b\u8bd5\u65b9\u6cd5\uff0c\u4ee5\u53ca\u903b\u8f91\u64cd\u4f5c\u7b26\u7684\u5e94\u7528\u3002\u6d4b\u8bd5\u7ed3\u679c\u901a\u8fc7\u9000\u51fa\u72b6\u6001<code>$?<\/code>\u5224\u65ad\uff08<code>0<\/code>\u8868\u793a\u6210\u529f\uff0c\u975e<code>0<\/code>\u8868\u793a\u5931\u8d25\uff09\uff0c\u4e3a\u540e\u7eed\u6761\u4ef6\u63a7\u5236\u7ed3\u6784\uff08\u5982<code>if<\/code>\u3001<code>case<\/code>\uff09\u63d0\u4f9b\u57fa\u7840\u3002<\/p>\n<hr \/>\n<h3 id=\"1-\u6587\u4ef6\u72b6\u6001\u6d4b\u8bd5\"><strong>1. \u6587\u4ef6\u72b6\u6001\u6d4b\u8bd5<\/strong><\/h3>\n<p>\u2022 \u8bed\u6cd5\u683c\u5f0f\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">test condition \u6216 [ condition ]  # \u65b9\u62ec\u53f7\u4e24\u4fa7\u5fc5\u987b\u6709\u7a7a\u683c\n<\/code><\/pre>\n<p>\u2022 \u5e38\u7528\u6d4b\u8bd5\u9009\u9879\uff1a<\/p>\n<figure class=\"table-figure\">\n<table>\n<thead>\n<tr>\n<th>\u9009\u9879<\/th>\n<th>\u542b\u4e49<\/th>\n<th>\u793a\u4f8b<\/th>\n<th>\u7ed3\u679c\u5224\u65ad<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>-d<\/code><\/td>\n<td>\u662f\u5426\u4e3a\u76ee\u5f55<\/td>\n<td><code>[ -d appsbin ]<\/code><\/td>\n<td>\u5b58\u5728\u76ee\u5f55\u8fd4\u56de<code>0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-f<\/code><\/td>\n<td>\u662f\u5426\u4e3a\u666e\u901a\u6587\u4ef6<\/td>\n<td><code>[ -f scores.txt ]<\/code><\/td>\n<td>\u662f\u6587\u4ef6\u8fd4\u56de<code>0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-r<\/code><\/td>\n<td>\u662f\u5426\u53ef\u8bfb<\/td>\n<td><code>[ -r file ]<\/code><\/td>\n<td>\u53ef\u8bfb\u8fd4\u56de<code>0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-w<\/code><\/td>\n<td>\u662f\u5426\u53ef\u5199<\/td>\n<td><code>[ -w scores.txt ]<\/code><\/td>\n<td>\u53ef\u5199\u8fd4\u56de<code>0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-x<\/code><\/td>\n<td>\u662f\u5426\u53ef\u6267\u884c<\/td>\n<td><code>[ -x script.sh ]<\/code><\/td>\n<td>\u53ef\u6267\u884c\u8fd4\u56de<code>0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-u<\/code><\/td>\n<td>\u662f\u5426\u6709suid\u4f4d<\/td>\n<td><code>[ -u xab ]<\/code><\/td>\n<td>\u8bbe\u7f6esuid\u8fd4\u56de<code>0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-s<\/code><\/td>\n<td>\u6587\u4ef6\u975e\u7a7a<\/td>\n<td><code>[ -s data.txt ]<\/code><\/td>\n<td>\u975e\u7a7a\u8fd4\u56de<code>0<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">$ [ -w scores.txt ]; echo $?   # \u6d4b\u8bd5\u53ef\u5199\u6027\uff0c\u8fd4\u56de0\u8868\u793a\u53ef\u5199\n$ [ -x scores.txt ]; echo $?   # \u6d4b\u8bd5\u53ef\u6267\u884c\u6027\uff0c\u8fd4\u56de1\u8868\u793a\u4e0d\u53ef\u6267\u884c\n$ [ -d appsbin ]; echo $?      # \u6d4b\u8bd5\u662f\u5426\u4e3a\u76ee\u5f55\uff0c\u8fd4\u56de0\u8868\u793a\u5b58\u5728\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"2-\u903b\u8f91\u64cd\u4f5c\u7b26\"><strong>2. \u903b\u8f91\u64cd\u4f5c\u7b26<\/strong><\/h3>\n<p>\u2022 \u64cd\u4f5c\u7b26\uff1a<\/p>\n<p>\u2022\u00a0<code>-a<\/code>\uff1a\u903b\u8f91\u4e0e\uff08\u4e24\u6761\u4ef6\u5747\u771f\uff09<\/p>\n<p>\u2022\u00a0<code>-o<\/code>\uff1a\u903b\u8f91\u6216\uff08\u4efb\u4e00\u6761\u4ef6\u4e3a\u771f\uff09<\/p>\n<p>\u2022\u00a0<code>!<\/code>\uff1a\u903b\u8f91\u975e\uff08\u53d6\u53cd\uff09<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u6d4b\u8bd5\u4e24\u4e2a\u6587\u4ef6\u5747\u53ef\u5199\n$ [ -w file1 -a -w file2 ]; echo $?  # \u8fd4\u56de0\u8868\u793a\u5747\u6ee1\u8db3\n\n# \u6d4b\u8bd5\u6587\u4ef6\u662f\u5426\u53ef\u5199\u4e14\u53ef\u6267\u884c\n$ [ -w script.sh -a -x script.sh ]; echo $?  # \u8fd4\u56de0\u8868\u793a\u6ee1\u8db3\n\n# \u6d4b\u8bd5\u6587\u4ef6\u662f\u5426\u4e0d\u53ef\u8bfb\n$ [ ! -r secret.txt ]; echo $?  # \u4e0d\u53ef\u8bfb\u8fd4\u56de0\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"3-\u5b57\u7b26\u4e32\u6d4b\u8bd5\"><strong>3. \u5b57\u7b26\u4e32\u6d4b\u8bd5<\/strong><\/h3>\n<p>\u2022 \u6d4b\u8bd5\u64cd\u4f5c\u7b26\uff1a<\/p>\n<p>\u2022\u00a0<code>=<\/code>\uff1a\u76f8\u7b49<\/p>\n<p>\u2022\u00a0<code>!=<\/code>\uff1a\u4e0d\u7b49<\/p>\n<p>\u2022\u00a0<code>-z<\/code>\uff1a\u7a7a\u5b57\u7b26\u4e32<\/p>\n<p>\u2022\u00a0<code>-n<\/code>\uff1a\u975e\u7a7a\u5b57\u7b26\u4e32<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">$ [ -z \"$EDITOR\" ]; echo $?    # \u6d4b\u8bd5\u53d8\u91cf\u662f\u5426\u4e3a\u7a7a\uff0c\u8fd4\u56de1\u8868\u793a\u975e\u7a7a\n$ [ \"$EDITOR\" = \"vi\" ]; echo $?  # \u6d4b\u8bd5\u53d8\u91cf\u662f\u5426\u4e3avi\uff0c\u8fd4\u56de0\u8868\u793a\u662f\n$ [ \"$TAPE\" != \"$TAPE2\" ]; echo $?  # \u6d4b\u8bd5\u53d8\u91cf\u4e0d\u7b49\uff0c\u8fd4\u56de0\u8868\u793a\u4e0d\u7b49\n<\/code><\/pre>\n<p>\u2022 \u6ce8\u610f\uff1a\u53d8\u91cf\u9700\u7528\u53cc\u5f15\u53f7\u5305\u88f9\uff0c\u907f\u514d\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<hr \/>\n<h3 id=\"4-\u6570\u503c\u6d4b\u8bd5\"><strong>4. \u6570\u503c\u6d4b\u8bd5<\/strong><\/h3>\n<p>\u2022 \u64cd\u4f5c\u7b26\uff1a<\/p>\n<figure class=\"table-figure\">\n<table>\n<thead>\n<tr>\n<th>\u64cd\u4f5c\u7b26<\/th>\n<th>\u542b\u4e49<\/th>\n<th>\u793a\u4f8b<\/th>\n<th>\u7ed3\u679c\u5224\u65ad<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>-eq<\/code><\/td>\n<td>\u7b49\u4e8e<\/td>\n<td><code>[ \"$a\" -eq 10 ]<\/code><\/td>\n<td>\u76f8\u7b49\u8fd4\u56de<code>0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-ne<\/code><\/td>\n<td>\u4e0d\u7b49\u4e8e<\/td>\n<td><code>[ \"$a\" -ne 5 ]<\/code><\/td>\n<td>\u4e0d\u7b49\u8fd4\u56de<code>0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-gt<\/code><\/td>\n<td>\u5927\u4e8e<\/td>\n<td><code>[ \"$a\" -gt 100 ]<\/code><\/td>\n<td>\u5927\u4e8e\u8fd4\u56de<code>0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-lt<\/code><\/td>\n<td>\u5c0f\u4e8e<\/td>\n<td><code>[ \"$a\" -lt 50 ]<\/code><\/td>\n<td>\u5c0f\u4e8e\u8fd4\u56de<code>0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-ge<\/code><\/td>\n<td>\u5927\u4e8e\u7b49\u4e8e<\/td>\n<td><code>[ \"$a\" -ge 99 ]<\/code><\/td>\n<td>\u6ee1\u8db3\u8fd4\u56de<code>0<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>-le<\/code><\/td>\n<td>\u5c0f\u4e8e\u7b49\u4e8e<\/td>\n<td><code>[ \"$a\" -le 200 ]<\/code><\/td>\n<td>\u6ee1\u8db3\u8fd4\u56de<code>0<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">$ [ \"$NUMBER\" -eq 130 ]; echo $?   # \u6d4b\u8bd5\u7b49\u4e8e130\uff0c\u8fd4\u56de0\n$ [ \"$SOURCE_COUNT\" -lt \"$DEST_COUNT\" ]; echo $?  # \u6d4b\u8bd5\u5c0f\u4e8e\uff0c\u8fd4\u56de0\n$ [ \"990\" -le \"995\" -a \"123\" -gt \"33\" ]; echo $?  # \u7ec4\u5408\u6d4b\u8bd5\uff0c\u8fd4\u56de0\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"5-expr\u547d\u4ee4\"><strong>5. expr\u547d\u4ee4<\/strong><\/h3>\n<p>\u2022 \u529f\u80fd\uff1a\u6570\u503c\u8ba1\u7b97\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001\u6a21\u5f0f\u5339\u914d\u3002<\/p>\n<p>\u2022 \u6570\u503c\u8fd0\u7b97\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">$ expr 30 \\* 3   # \u4e58\u6cd5\u9700\u8f6c\u4e49\uff0c\u8f93\u51fa90\n$ expr $LOOP + 1 # \u589e\u91cf\u8ba1\u6570\n<\/code><\/pre>\n<p>\u2022 \u6570\u503c\u6d4b\u8bd5\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">$ expr \"$VALUE\" + 0 &gt;\/dev\/null 2&gt;&amp;1  # \u6d4b\u8bd5\u662f\u5426\u4e3a\u6570\u503c\n$ echo $?  # \u8fd4\u56de0\u8868\u793a\u662f\u6570\u503c\uff0c\u975e0\u8868\u793a\u975e\u6570\u503c\n<\/code><\/pre>\n<p>\u2022 \u5b57\u7b26\u4e32\u6a21\u5f0f\u5339\u914d\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">$ expr \"$VALUE\" : '.*\\.doc'  # \u63d0\u53d6\u6587\u4ef6\u540d\u4e2d\u7684.doc\u540e\u7f00\n$ expr length=$(expr \"$STR\" : '.*')  # \u8ba1\u7b97\u5b57\u7b26\u4e32\u957f\u5ea6\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"6-\u6ce8\u610f\u4e8b\u9879\"><strong>6. \u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n<ol>\n<li>\n<p>\u7a7a\u683c\u4e0e\u5f15\u53f7\uff1a \u2022\u00a0<code>[ condition ]<\/code>\u4e2d\u7684\u6761\u4ef6\u4e24\u4fa7\u5fc5\u987b\u6709\u7a7a\u683c\u3002<\/p>\n<p>\u2022 \u53d8\u91cf\u6bd4\u8f83\u65f6\u9700\u7528\u53cc\u5f15\u53f7\u5305\u88f9\uff0c\u907f\u514d\u672a\u5b9a\u4e49\u53d8\u91cf\u6216\u542b\u7a7a\u683c\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<\/li>\n<li>\n<p>\u9000\u51fa\u72b6\u6001\uff1a \u2022\u00a0<code>test<\/code>\u548c<code>expr<\/code>\u7684\u9000\u51fa\u72b6\u6001<code>$?<\/code>\u9700\u53cd\u5411\u7406\u89e3\uff1a<code>0<\/code>\u8868\u793a\u6210\u529f\uff08\u771f\uff09\uff0c\u975e<code>0<\/code>\u4e3a\u5931\u8d25\uff08\u5047\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>\u903b\u8f91\u64cd\u4f5c\u7b26\u9650\u5236\uff1a \u2022 \u903b\u8f91\u64cd\u4f5c\u7b26\u9700\u5728\u5355\u5c42\u65b9\u62ec\u53f7\u5185\u4f7f\u7528\uff0c\u5982<code>[ cond1 -a cond2 ]<\/code>\uff0c\u907f\u514d\u591a\u5c42\u62ec\u53f7\u5bfc\u81f4\u8bed\u6cd5\u9519\u8bef\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"7-\u5e94\u7528\u573a\u666f\"><strong>7. \u5e94\u7528\u573a\u666f<\/strong><\/h3>\n<p>\u2022 \u6587\u4ef6\u68c0\u67e5\uff1a\u786e\u4fdd\u6587\u4ef6\u5b58\u5728\u3001\u53ef\u8bfb\u5199\u540e\u518d\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<p>\u2022 \u8f93\u5165\u9a8c\u8bc1\uff1a\u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u4e3a\u7a7a\u6216\u7b26\u5408\u683c\u5f0f\u3002<\/p>\n<p>\u2022 \u5faa\u73af\u63a7\u5236\uff1a\u901a\u8fc7<code>expr<\/code>\u5b9e\u73b0\u8ba1\u6570\u5668\u6216\u6570\u503c\u6761\u4ef6\u5224\u65ad\u3002<\/p>\n<p>\u2022 \u811a\u672c\u5065\u58ee\u6027\uff1a\u901a\u8fc7\u6d4b\u8bd5\u907f\u514d\u56e0\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\u95ee\u9898\u5bfc\u81f4\u811a\u672c\u4e2d\u65ad\u3002<\/p>\n<p>\u672c\u7ae0\u5185\u5bb9\u4e3aShell\u811a\u672c\u7684\u6d41\u7a0b\u63a7\u5236\u6253\u4e0b\u57fa\u7840\uff0c\u7ed3\u5408\u540e\u7eed\u7684<code>if<\/code>\u3001<code>case<\/code>\u7b49\u7ed3\u6784\uff0c\u53ef\u5b9e\u73b0\u590d\u6742\u7684\u903b\u8f91\u5224\u65ad\u4e0e\u81ea\u52a8\u5316\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"\u7b2c18\u7ae0-\u63a7\u5236\u6d41\u7ed3\u6784\u603b\u7ed3\">\u7b2c18\u7ae0 \u63a7\u5236\u6d41\u7ed3\u6784\u603b\u7ed3<\/h2>\n<h3 id=\"\u4e00\u9000\u51fa\u72b6\u6001\">\u4e00\u3001\u9000\u51fa\u72b6\u6001<\/h3>\n<ol>\n<li>\n<p>\u6982\u5ff5\uff1a\u6bcf\u4e2a\u547d\u4ee4\u6267\u884c\u540e\u8fd4\u56de\u9000\u51fa\u72b6\u6001\uff0c<code>0<\/code>\u8868\u793a\u6210\u529f\uff0c\u975e<code>0<\/code>\u8868\u793a\u5931\u8d25\u3002<\/p>\n<\/li>\n<li>\n<p>\u83b7\u53d6\u72b6\u6001\uff1a\u901a\u8fc7<code>$?<\/code>\u83b7\u53d6\u4e0a\u4e00\u4e2a\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\u3002<\/p>\n<\/li>\n<li>\n<p>\u811a\u672c\u9000\u51fa\uff1a\u4f7f\u7528<code>exit n<\/code>\u6307\u5b9a\u9000\u51fa\u7801\uff0c\u5efa\u8bae\u5728\u811a\u672c\u4e2d\u663e\u5f0f\u5904\u7406\u9519\u8bef\u5e76\u8fd4\u56de\u5408\u9002\u72b6\u6001\u3002 \u2022\u00a0<code>0<\/code>\uff1a\u6210\u529f<\/p>\n<p>\u2022\u00a0<code>1<\/code>\uff1a\u5e38\u89c1\u9519\u8bef<\/p>\n<\/li>\n<\/ol>\n<h3 id=\"\u4e8c\u63a7\u5236\u7ed3\u6784\">\u4e8c\u3001\u63a7\u5236\u7ed3\u6784<\/h3>\n<ol>\n<li>\u6d41\u63a7\u5236 \u2022 if-then-else<\/li>\n<\/ol>\n<p>\u2022 \u8bed\u6cd5\uff1a<\/p>\n<pre><code>```sh\nif condition; then\n    commands\nelif condition2; then\n    commands\nelse\n    commands\nfi\n```\n<\/code><\/pre>\n<p>\u2022 \u7528\u4f8b\uff1a<\/p>\n<pre><code>\u25e6 \u6d4b\u8bd5\u53d8\u91cf\u662f\u5426\u4e3a\u7a7a\uff1a`if [ -z \"$var\" ]`\n\n\u25e6 \u6587\u4ef6\u5b58\u5728\u6027\u68c0\u67e5\uff1a`if [ -f \"file.txt\" ]`\n\n\u25e6 \u6570\u503c\u6bd4\u8f83\uff1a`if [ \"$a\" -lt \"$b\" ]`\n<\/code><\/pre>\n<p>\u2022 case\u8bed\u53e5<\/p>\n<p>\u2022 \u8bed\u6cd5\uff1a<\/p>\n<pre><code>```sh\ncase $var in\n    pattern1) commands ;;\n    pattern2) commands ;;\n    *) default commands ;;\nesac\n```\n<\/code><\/pre>\n<p>\u2022 \u7528\u4f8b\uff1a<\/p>\n<pre><code>\u25e6 \u5904\u7406\u7528\u6237\u8f93\u5165\u9009\u9879\uff08\u5982\u83dc\u5355\u9009\u62e9\uff09\n\n\u25e6 \u5339\u914d\u6587\u4ef6\u540d\u6a21\u5f0f\uff08\u5982`.txt`, `.sh`\uff09\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u5faa\u73af\u7ed3\u6784<\/li>\n<\/ol>\n<p>\u2022 for\u5faa\u73af<\/p>\n<p>\u2022 \u8bed\u6cd5\uff1a<\/p>\n<pre><code>```sh\nfor var in list; do\n    commands\ndone\n```\n<\/code><\/pre>\n<p>\u2022 \u7528\u4f8b\uff1a<\/p>\n<pre><code>\u25e6 \u904d\u5386\u6587\u4ef6\u5217\u8868\uff1a`for file in *.txt; do ...`\n\n\u25e6 \u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\uff1a`for param in \"$@\"; do ...`\n<\/code><\/pre>\n<p>\u2022 while\u5faa\u73af<\/p>\n<p>\u2022 \u8bed\u6cd5\uff1a<\/p>\n<pre><code>```sh\nwhile condition; do\n    commands\ndone\n```\n<\/code><\/pre>\n<p>\u2022 \u7528\u4f8b\uff1a<\/p>\n<pre><code>\u25e6 \u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff1a`while read line; do ... done &lt; file`\n\n\u25e6 \u6301\u7eed\u76d1\u63a7\u6761\u4ef6\uff08\u5982\u8fdb\u7a0b\u8fd0\u884c\u72b6\u6001\uff09\n<\/code><\/pre>\n<p>\u2022 until\u5faa\u73af<\/p>\n<p>\u2022 \u8bed\u6cd5\uff1a<\/p>\n<pre><code>```sh\nuntil condition; do\n    commands\ndone\n```\n<\/code><\/pre>\n<p>\u2022 \u7528\u4f8b\uff1a<\/p>\n<pre><code>\u25e6 \u7b49\u5f85\u6587\u4ef6\u88ab\u5220\u9664\uff1a`until [ ! -f lockfile ]; do sleep 1; done`\n\n\u25e6 \u7b49\u5f85\u670d\u52a1\u542f\u52a8\n<\/code><\/pre>\n<h3 id=\"\u4e09\u5faa\u73af\u63a7\u5236\">\u4e09\u3001\u5faa\u73af\u63a7\u5236<\/h3>\n<p>\u2022 break\uff1a\u7acb\u5373\u9000\u51fa\u5faa\u73af\u3002<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">while :; do\n    if condition; then break; fi\ndone\n<\/code><\/pre>\n<p>\u2022 continue\uff1a\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\uff0c\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\u3002<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">for i in {1..5}; do\n    if [ \"$i\" -eq 3 ]; then continue; fi\n    echo $i\ndone\n<\/code><\/pre>\n<h3 id=\"\u56db\u83dc\u5355\u5b9e\u73b0\">\u56db\u3001\u83dc\u5355\u5b9e\u73b0<\/h3>\n<p>\u2022 \u7ed3\u6784\uff1a\u7ed3\u5408<code>while<\/code>\u5faa\u73af\u548c<code>case<\/code>\u8bed\u53e5\u3002<\/p>\n<p>\u2022 \u5173\u952e\u70b9\uff1a<\/p>\n<p>\u2022 \u65e0\u9650\u5faa\u73af\uff1a<code>while :; do ...<\/code><\/p>\n<p>\u2022 \u663e\u793a\u9009\u9879\uff0c\u6355\u83b7\u7528\u6237\u8f93\u5165\u3002<\/p>\n<p>\u2022 \u5904\u7406\u9000\u51fa\u9009\u9879\uff08<code>q<\/code>\u6216<code>Q<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">while :; do\n    clear\n    echo \"1. Option 1\"\n    echo \"2. Option 2\"\n    echo \"Q. Quit\"\n    read -p \"Choice: \" choice\n    case $choice in\n        1) command1 ;;\n        2) command2 ;;\n        q|Q) exit 0 ;;\n        *) echo \"Invalid option\"; sleep 1 ;;\n    esac\ndone\n<\/code><\/pre>\n<h3 id=\"\u4e94\u5b9e\u7528\u6280\u5de7\">\u4e94\u3001\u5b9e\u7528\u6280\u5de7<\/h3>\n<ol>\n<li>\n<p>\u6d4b\u8bd5\u547d\u4ee4\uff1a \u2022 \u6587\u4ef6\u6d4b\u8bd5\uff1a<code>-f<\/code>\uff08\u5b58\u5728\uff09\u3001<code>-d<\/code>\uff08\u76ee\u5f55\uff09\u3001<code>-r<\/code>\uff08\u53ef\u8bfb\uff09\u3002<\/p>\n<p>\u2022 \u5b57\u7b26\u4e32\u6bd4\u8f83\uff1a<code>=<\/code>\uff08\u76f8\u7b49\uff09\u3001<code>!=<\/code>\uff08\u4e0d\u7b49\uff09\u3002<\/p>\n<p>\u2022 \u6570\u503c\u6bd4\u8f83\uff1a<code>-eq<\/code>\uff08\u7b49\u4e8e\uff09\u3001<code>-lt<\/code>\uff08\u5c0f\u4e8e\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>\u9519\u8bef\u5904\u7406\uff1a \u2022 \u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u5230\u6807\u51c6\u9519\u8bef\uff1a<code>echo \"Error\" &gt;&amp;2<\/code><\/p>\n<p>\u2022 \u9000\u51fa\u65f6\u8fd4\u56de\u72b6\u6001\u7801\uff1a<code>exit 1<\/code><\/p>\n<\/li>\n<li>\n<p>\u8f93\u5165\u5904\u7406\uff1a \u2022 \u4f7f\u7528<code>read<\/code>\u6355\u83b7\u8f93\u5165\uff0c\u5904\u7406\u9ed8\u8ba4\u503c\uff1a<code>read -p \"Enter [default]: \" var || var=\"default\"<\/code><\/p>\n<p>\u2022 \u5c4f\u853d\u8f93\u5165\u56de\u663e\uff08\u5bc6\u7801\uff09\uff1a<code>stty -echo; read pass; stty echo<\/code><\/p>\n<\/li>\n<li>\n<p>\u73af\u5883\u53d8\u91cf\u4e0eIFS\uff1a \u2022 \u4fee\u6539<code>IFS<\/code>\u5904\u7406\u5b57\u6bb5\u5206\u9694\uff08\u5982\u9010\u884c\u89e3\u6790\u9017\u53f7\u5206\u9694\u6587\u4ef6\uff09\u3002<\/p>\n<p>\u2022 \u4e34\u65f6\u53d8\u91cf\u4fdd\u5b58\u73af\u5883\uff1a<code>OLD_IFS=$IFS; IFS=','; ...; IFS=$OLD_IFS<\/code><\/p>\n<\/li>\n<\/ol>\n<h3 id=\"\u516d\u5178\u578b\u7528\u4f8b\">\u516d\u3001\u5178\u578b\u7528\u4f8b<\/h3>\n<p>\u2022 \u5907\u4efd\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">for file in *.doc; do\n    cp \"$file\" \"${file}.bak\"\ndone\n<\/code><\/pre>\n<p>\u2022 \u76d1\u63a7\u78c1\u76d8\u7a7a\u95f4\uff1a<\/p>\n<p>df\u547d\u4ee4\u7528\u6765\u67e5\u770b\u78c1\u76d8\u76f8\u5173\u5185\u5bb9<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">while true; do\n    usage=$(df \/ | awk '{print $5}' | tr -d '%')\n    [ \"$usage\" -ge 90 ] &amp;&amp; echo \"Disk full!\" | mail admin\n    sleep 3600\ndone\n<\/code><\/pre>\n<h3 id=\"\u4e03\u6ce8\u610f\u4e8b\u9879-2\">\u4e03\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<p>\u2022 \u8bed\u6cd5\u7ec6\u8282\uff1a<\/p>\n<p>\u2022\u00a0<code>if<\/code>\u6761\u4ef6\u65b9\u62ec\u53f7\u5185\u9700\u7a7a\u683c\uff1a<code>[ \"$a\" -eq 1 ]<\/code><\/p>\n<p>\u2022\u00a0<code>case<\/code>\u8bed\u53e5\u6bcf\u4e2a\u6a21\u5f0f\u4ee5<code>;;<\/code>\u7ed3\u675f\u3002<\/p>\n<p>\u2022 \u5f15\u53f7\u4f7f\u7528\uff1a\u53d8\u91cf\u5f15\u7528\u52a0\u53cc\u5f15\u53f7\u907f\u514d\u7a7a\u683c\u95ee\u9898\uff1a<code>\"$var\"<\/code><\/p>\n<p>\u2022 \u8c03\u8bd5\uff1a\u4f7f\u7528<code>set -x<\/code>\u542f\u7528\u8c03\u8bd5\u6a21\u5f0f\uff0c\u663e\u793a\u6267\u884c\u547d\u4ee4\u3002<\/p>\n<p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u63a7\u5236\u6d41\u7ed3\u6784\uff0c\u53ef\u4ee5\u7f16\u5199\u51fa\u7075\u6d3b\u3001\u5065\u58ee\u7684Shell\u811a\u672c\uff0c\u6709\u6548\u5904\u7406\u6761\u4ef6\u5224\u65ad\u3001\u5faa\u73af\u4efb\u52a1\u548c\u7528\u6237\u4ea4\u4e92\u3002<\/p>\n<h2 id=\"\u7b2c19\u7ae0-shell\u51fd\u6570\u8be6\u7ec6\u603b\u7ed3\">\u7b2c19\u7ae0 Shell\u51fd\u6570\u8be6\u7ec6\u603b\u7ed3<\/h2>\n<hr \/>\n<h3 id=\"\u4e00shell\u51fd\u6570\u57fa\u7840\u6982\u5ff5\"><strong>\u4e00\u3001Shell\u51fd\u6570\u57fa\u7840\u6982\u5ff5<\/strong><\/h3>\n<ol>\n<li>\n<p>\u4f5c\u7528\u4e0e\u4f18\u52bf \u2022 \u5c06\u91cd\u590d\u4ee3\u7801\u5757\u5c01\u88c5\u4e3a\u53ef\u91cd\u7528\u5355\u5143\uff0c\u63d0\u5347\u4ee3\u7801\u590d\u7528\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<p>\u2022 \u51fd\u6570\u6267\u884c\u65f6\u4fdd\u7559\u5f53\u524dShell\u73af\u5883\uff08\u53d8\u91cf\u3001\u72b6\u6001\uff09\uff0c\u800c\u5916\u90e8\u811a\u672c\u4f1a\u542f\u52a8\u65b0Shell\u3002<\/p>\n<\/li>\n<li>\n<p>\u51fd\u6570\u7ed3\u6784 \u2022 \u6807\u9898\uff1a\u51fd\u6570\u540d\uff0c\u9700\u552f\u4e00\u3002<\/p>\n<p>\u2022 \u51fd\u6570\u4f53\uff1a\u5305\u542b\u5177\u4f53\u547d\u4ee4\u7684\u4ee3\u7801\u5757\u3002<\/p>\n<p>\u2022 \u5b9a\u4e49\u683c\u5f0f\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">\u51fd\u6570\u540d() { \u547d\u4ee4; }\n# \u6216\nfunction \u51fd\u6570\u540d() { \u547d\u4ee4; }\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e8c\u51fd\u6570\u5b9a\u4e49\u4e0e\u8c03\u7528\"><strong>\u4e8c\u3001\u51fd\u6570\u5b9a\u4e49\u4e0e\u8c03\u7528<\/strong><\/h3>\n<ol>\n<li>\n<p>\u5b9a\u4e49\u4f4d\u7f6e \u2022 \u5fc5\u987b\u5728\u811a\u672c\u9876\u90e8\u6216\u8c03\u7528\u524d\u5b9a\u4e49\uff0c\u786e\u4fdd\u89e3\u6790\u5668\u53ef\u8bc6\u522b\u3002<\/p>\n<\/li>\n<li>\n<p>\u7b80\u5355\u793a\u4f8b<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">hello() { echo \"Hello, today's date is $(date)\"; }\n# \u8c03\u7528\nhello\n<\/code><\/pre>\n<p>\u2022 \u8f93\u51fa\u793a\u4f8b\uff1a<\/p>\n<pre><code>Hello, today's date is Sun Jun 6 10:46:59 GMT 1999\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e09\u53c2\u6570\u4f20\u9012\u4e0e\u8fd4\u56de\u503c\"><strong>\u4e09\u3001\u53c2\u6570\u4f20\u9012\u4e0e\u8fd4\u56de\u503c<\/strong><\/h3>\n<ol>\n<li>\n<p>\u53c2\u6570\u4f20\u9012 \u2022 \u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\uff08<code>$1, $2...<\/code>\uff09\u63a5\u6536\u4f20\u5165\u503c\u3002<\/p>\n<p>\u2022 \u6700\u4f73\u5b9e\u8df5\uff1a\u5c06\u53c2\u6570\u8d4b\u503c\u7ed9\u672c\u5730\u53d8\u91cf\uff08\u5982\u00a0<code>_VAR=$1<\/code>\uff09\u4ee5\u907f\u514d\u526f\u4f5c\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>\u8fd4\u56de\u503c\u5904\u7406 \u2022\u00a0<code>return<\/code>\u8bed\u53e5\uff1a\u8fd4\u56de\u72b6\u6001\u7801\uff080\u6210\u529f\uff0c\u975e0\u9519\u8bef\uff09\u3002<\/p>\n<p>\u2022 \u83b7\u53d6\u8fd4\u56de\u503c\uff1a\u901a\u8fc7\u00a0<code>$?<\/code>\u00a0\u6216\u76f4\u63a5\u4f5c\u4e3a\u6761\u4ef6\u5224\u65ad\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">check_dir() {\n  [ -d \"$1\" ] &amp;&amp; return 0 || return 1\n}\nif check_dir \"\/path\"; then echo \"\u5b58\u5728\"; else echo \"\u4e0d\u5b58\u5728\"; fi\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u56db\u9ad8\u7ea7\u51fd\u6570\u6280\u5de7\"><strong>\u56db\u3001\u9ad8\u7ea7\u51fd\u6570\u6280\u5de7<\/strong><\/h3>\n<ol>\n<li>\n<p>\u51fd\u6570\u6587\u4ef6\u7ba1\u7406 \u2022 \u521b\u5efa\u51fd\u6570\u6587\u4ef6\uff08\u5982\u00a0<code>functions.sh<\/code>\uff09\u96c6\u4e2d\u5b58\u653e\u5e38\u7528\u51fd\u6570\u3002<\/p>\n<p>\u2022 \u52a0\u8f7d\u51fd\u6570\uff1a\u901a\u8fc7\u00a0<code>. .\/functions.sh<\/code>\u00a0\u6216\u00a0<code>source<\/code>\u00a0\u547d\u4ee4\u5bfc\u5165\u5f53\u524dShell\u3002<\/p>\n<p>\u2022 \u67e5\u770b\/\u5220\u9664\u51fd\u6570\uff1a<code>set<\/code>\u00a0\u663e\u793a\u5df2\u52a0\u8f7d\u51fd\u6570\uff0c<code>unset \u51fd\u6570\u540d<\/code>\u00a0\u5220\u9664\u3002<\/p>\n<\/li>\n<li>\n<p>\u9519\u8bef\u5904\u7406\u4e0e\u4ea4\u4e92 \u2022 \u7edf\u4e00\u9519\u8bef\u63d0\u793a\u51fd\u6570\uff1a\u5c01\u88c5\u9519\u8bef\u6d88\u606f\u683c\u5f0f\uff08\u5982\u5e26\u8702\u9e23\u58f0\uff09\u3002<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">error_msg() { echo -e \"\\a$@\\a\"; }\n<\/code><\/pre>\n<p>\u2022 \u7528\u6237\u8f93\u5165\u9a8c\u8bc1\uff1a\u5faa\u73af\u63d0\u793a\u76f4\u81f3\u8f93\u5165\u5408\u6cd5\uff08\u5982\u5b57\u6bcd\u68c0\u67e5\uff09\u3002<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">while :; do\n  read -p \"\u8f93\u5165\u59d3\u540d\uff1a\" NAME\n  char_name \"$NAME\" &amp;&amp; break || error_msg \"\u975e\u6cd5\u5b57\u7b26\uff01\"\ndone\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e94\u5b9e\u7528\u51fd\u6570\u793a\u4f8b\"><strong>\u4e94\u3001\u5b9e\u7528\u51fd\u6570\u793a\u4f8b<\/strong><\/h3>\n<ol>\n<li>\n<p>\u76ee\u5f55\u68c0\u67e5<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">is_dir() { [ -d \"$1\" ] &amp;&amp; return 0 || return 1; }\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u5b57\u7b26\u4e32\u5904\u7406 \u2022 \u5927\u5c0f\u5199\u8f6c\u6362\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">to_upper() { echo \"$@\" | tr '[:lower:]' '[:upper:]'; }\nto_lower() { echo \"$@\" | tr '[:upper:]' '[:lower:]'; }\n<\/code><\/pre>\n<p>\u2022 \u957f\u5ea6\u68c0\u67e5\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">check_len() { [ ${#1} -le $2 ] &amp;&amp; return 0 || return 1; }\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u4ea4\u4e92\u5f0f\u63d0\u793a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">confirm() {\n  read -p \"$1 [Y\/N]\uff08\u9ed8\u8ba4\uff1a$2\uff09: \" ans\n  case \"${ans:-$2}\" in [Yy]*) return 0;; *) return 1;; esac\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u6587\u4ef6\u884c\u53f7\u663e\u793a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">number_lines() {\n  local line=1\n  while IFS= read -r content; do\n    echo \"$line: $content\"\n    ((line++))\n  done &lt; \"$1\"\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u516d\u51fd\u6570\u8c03\u7528\u573a\u666f\"><strong>\u516d\u3001\u51fd\u6570\u8c03\u7528\u573a\u666f<\/strong><\/h3>\n<ol>\n<li>\n<p>\u811a\u672c\u5185\u8c03\u7528 \u2022 \u76f4\u63a5\u5728\u811a\u672c\u4e2d\u5b9a\u4e49\u5e76\u8c03\u7528\u51fd\u6570\uff0c\u9002\u5408\u529f\u80fd\u4e13\u4e00\u7684\u573a\u666f\u3002<\/p>\n<\/li>\n<li>\n<p>\u8de8\u811a\u672c\u5171\u4eab \u2022 \u901a\u8fc7\u51fd\u6570\u6587\u4ef6\u590d\u7528\u51fd\u6570\uff08\u5982\u00a0<code>source functions.sh<\/code>\uff09\uff0c\u9002\u5408\u590d\u6742\u9879\u76ee\u3002<\/p>\n<\/li>\n<li>\n<p>\u7cfb\u7edf\u914d\u7f6e\u5171\u4eab \u2022 \u5c06\u5168\u5c40\u53d8\u91cf\u548c\u51fd\u6570\u7ed3\u5408\uff08\u5982\u00a0<code>config.sh<\/code>\uff09\uff0c\u7edf\u4e00\u7ba1\u7406\u914d\u7f6e\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># config.sh\nLOG_DIR=\"\/var\/log\"\nset_log() { export LOG_FILE=\"$LOG_DIR\/$1.log\"; }\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e03\u8c03\u8bd5\u4e0e\u6700\u4f73\u5b9e\u8df5\"><strong>\u4e03\u3001\u8c03\u8bd5\u4e0e\u6700\u4f73\u5b9e\u8df5<\/strong><\/h3>\n<ol>\n<li>\n<p>\u8c03\u8bd5\u6280\u5de7 \u2022 \u5148\u4ee5\u72ec\u7acb\u811a\u672c\u6d4b\u8bd5\u51fd\u6570\u903b\u8f91\uff0c\u518d\u5c01\u88c5\u4e3a\u51fd\u6570\u3002<\/p>\n<p>\u2022 \u4f7f\u7528\u00a0<code>set -x<\/code>\u00a0\u5f00\u542f\u8c03\u8bd5\u6a21\u5f0f\uff0c\u8ffd\u8e2a\u51fd\u6570\u6267\u884c\u6d41\u7a0b\u3002<\/p>\n<\/li>\n<li>\n<p>\u6700\u4f73\u5b9e\u8df5 \u2022 \u547d\u540d\u89c4\u8303\uff1a\u51fd\u6570\u540d\u660e\u786e\u63cf\u8ff0\u529f\u80fd\uff08\u5982\u00a0<code>validate_email<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u53c2\u6570\u68c0\u67e5\uff1a\u51fd\u6570\u5f00\u5934\u9a8c\u8bc1\u53c2\u6570\u5408\u6cd5\u6027\uff0c\u907f\u514d\u5f02\u5e38\u3002<\/p>\n<p>\u2022 \u8fd4\u56de\u503c\u4e00\u81f4\u6027\uff1a\u6210\u529f\u8fd4\u56de0\uff0c\u9519\u8bef\u8fd4\u56de\u975e0\uff0c\u4fdd\u6301\u7ea6\u5b9a\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u516b\u7efc\u5408\u793a\u4f8b\"><strong>\u516b\u3001\u7efc\u5408\u793a\u4f8b<\/strong><\/h3>\n<p>\u573a\u666f\uff1a\u5907\u4efd\u811a\u672c\u4f7f\u7528\u51fd\u6570\u9a8c\u8bc1\u76ee\u5f55\u5e76\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">#!\/bin\/bash\nsource utils.sh  # \u52a0\u8f7d\u5de5\u5177\u51fd\u6570\n\nbackup() {\n  local src=$1 dest=$2\n  is_dir \"$src\" || { error_msg \"\u6e90\u76ee\u5f55\u4e0d\u5b58\u5728\uff01\"; return 1; }\n  mkdir -p \"$dest\" || return 1\n  rsync -av \"$src\" \"$dest\" &gt;&gt; \"${LOG_DIR}\/backup.log\"\n}\n\nmain() {\n  confirm \"\u786e\u8ba4\u5f00\u59cb\u5907\u4efd\uff1f\" \"Y\" || exit\n  backup \"\/data\" \"\/backup\" &amp;&amp; echo \"\u5907\u4efd\u6210\u529f\uff01\" || error_msg \"\u5907\u4efd\u5931\u8d25\uff01\"\n}\n\nmain\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u603b\u7ed3\"><strong>\u603b\u7ed3<\/strong><\/h3>\n<p>Shell\u51fd\u6570\u901a\u8fc7\u6a21\u5757\u5316\u63d0\u5347\u811a\u672c\u53ef\u7ef4\u62a4\u6027\uff0c\u652f\u6301\u53c2\u6570\u4f20\u9012\u3001\u8fd4\u56de\u503c\u5904\u7406\u53ca\u8de8\u811a\u672c\u590d\u7528\u3002\u7ed3\u5408\u51fd\u6570\u6587\u4ef6\u4e0e\u914d\u7f6e\u7ba1\u7406\uff0c\u53ef\u6784\u5efa\u7075\u6d3b\u9ad8\u6548\u7684\u811a\u672c\u7cfb\u7edf\u3002\u638c\u63e1\u9519\u8bef\u5904\u7406\u3001\u7528\u6237\u4ea4\u4e92\u548c\u8c03\u8bd5\u6280\u5de7\uff0c\u80fd\u663e\u8457\u63d0\u5347\u811a\u672c\u5065\u58ee\u6027\u3002<\/p>\n<h2 id=\"\u7b2c20\u7ae0-\u5411\u811a\u672c\u4f20\u9012\u53c2\u6570-\u603b\u7ed3\">\u7b2c20\u7ae0 \u5411\u811a\u672c\u4f20\u9012\u53c2\u6570 \u603b\u7ed3<\/h2>\n<p>\u672c\u7ae0\u6df1\u5165\u8bb2\u89e3\u4e86Shell\u811a\u672c\u4e2d\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\u7684\u4e24\u79cd\u6838\u5fc3\u65b9\u6cd5\uff1ashift\u547d\u4ee4\u548cgetopts\u547d\u4ee4\uff0c\u5e76\u7ed3\u5408\u5b9e\u9645\u6848\u4f8b\u6f14\u793a\u5176\u5e94\u7528\u573a\u666f\u548c\u6700\u4f73\u5b9e\u8df5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u5185\u5bb9\u603b\u7ed3\uff1a<\/p>\n<hr \/>\n<h3 id=\"\u4e00\u57fa\u7840\u6982\u5ff5\">\u4e00\u3001\u57fa\u7840\u6982\u5ff5<\/h3>\n<ol>\n<li>\n<p>\u4f4d\u7f6e\u53c2\u6570\u4e0e\u53c2\u6570\u7edf\u8ba1 \u2022 \u811a\u672c\u901a\u8fc7\u4f4d\u7f6e\u53c2\u6570\u00a0<code>$1<\/code>\u00a0\u5230\u00a0<code>$9<\/code>\u00a0\u63a5\u6536\u4f20\u9012\u7684\u53c2\u6570\uff0c<code>$#<\/code>\u00a0\u8868\u793a\u53c2\u6570\u603b\u6570\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a\u82e5\u811a\u672c\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff08\u5982\u00a0<code>start|stop<\/code>\u00a0\u548c\u8fdb\u7a0b\u540d\uff09\uff0c\u53ef\u901a\u8fc7\u00a0<code>if [ $# -ne 2 ]<\/code>\u00a0\u68c0\u67e5\u53c2\u6570\u4e2a\u6570\uff0c\u5e76\u914d\u5408\u00a0<code>usage()<\/code>\u00a0\u51fd\u6570\u63d0\u793a\u6b63\u786e\u7528\u6cd5\u3002<\/p>\n<\/li>\n<li>\n<p>usage\u8bed\u53e5 \u2022 \u7528\u4e8e\u5411\u7528\u6237\u8bf4\u660e\u811a\u672c\u7684\u6b63\u786e\u8c03\u7528\u65b9\u5f0f\uff0c\u901a\u5e38\u5728\u53c2\u6570\u9519\u8bef\u65f6\u89e6\u53d1\u3002\u4f8b\u5982\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">usage() {\n  echo \"Usage: $(basename $0) start|stop processname\"\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>case\u8bed\u53e5\u5904\u7406\u53c2\u6570 \u2022 \u901a\u8fc7\u00a0<code>case $1 in ... esac<\/code>\u00a0\u7ed3\u6784\u5339\u914d\u4e0d\u540c\u53c2\u6570\uff0c\u6267\u884c\u5bf9\u5e94\u64cd\u4f5c\u3002\u4f8b\u5982\u5904\u7406\u00a0<code>start<\/code>\u00a0\u548c\u00a0<code>stop<\/code>\u00a0\u547d\u4ee4\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e8cshift\u547d\u4ee4\">\u4e8c\u3001shift\u547d\u4ee4<\/h3>\n<ol>\n<li>\n<p>\u4f5c\u7528\u4e0e\u539f\u7406 \u2022\u00a0<code>shift<\/code>\u00a0\u5c06\u53c2\u6570\u5217\u8868\u5de6\u79fb\u4e00\u4f4d\uff0c\u4e22\u5f03\u5f53\u524d\u00a0<code>$1<\/code>\uff0c\u540e\u7eed\u53c2\u6570\u4f9d\u6b21\u524d\u79fb\u3002\u8fd9\u4f7f\u5f97\u811a\u672c\u80fd\u5904\u7406\u8d85\u8fc79\u4e2a\u53c2\u6570\uff0c\u5e76\u904d\u5386\u6240\u6709\u8f93\u5165\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a\u5faa\u73af\u5904\u7406\u6240\u6709\u53c2\u6570\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">while [ $# -ne 0 ]; do\n  echo $1\n  shift\ndone\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u5178\u578b\u5e94\u7528\u573a\u666f \u2022 \u904d\u5386\u53c2\u6570\uff1a\u5728\u5faa\u73af\u4e2d\u4f9d\u6b21\u5904\u7406\u6bcf\u4e2a\u53c2\u6570\u3002<\/p>\n<p>\u2022 \u5206\u79bb\u9009\u9879\u4e0e\u53c2\u6570\uff1a\u4f8b\u5982\u5904\u7406\u6587\u4ef6\u8f6c\u6362\u811a\u672c\u65f6\uff0c\u5148\u89e3\u6790\u9009\u9879\uff08\u5982\u00a0<code>-l<\/code>\u00a0\u6216\u00a0<code>-u<\/code>\uff09\uff0c\u518d\u901a\u8fc7\u00a0<code>shift<\/code>\u00a0\u8df3\u8fc7\u5df2\u5904\u7406\u7684\u9009\u9879\uff0c\u904d\u5386\u5269\u4f59\u6587\u4ef6\u540d\u3002<\/p>\n<\/li>\n<li>\n<p>\u5c40\u9650\u6027 \u2022 \u5904\u7406\u590d\u6742\u9009\u9879\uff08\u5982\u5e26\u503c\u9009\u9879\u00a0<code>-c 3<\/code>\uff09\u65f6\u4ee3\u7801\u5197\u4f59\uff0c\u9700\u624b\u52a8\u7ba1\u7406\u53c2\u6570\u4f4d\u7f6e\uff0c\u6613\u51fa\u9519\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e09getopts\u547d\u4ee4\">\u4e09\u3001getopts\u547d\u4ee4<\/h3>\n<ol>\n<li>\n<p>\u4f5c\u7528\u4e0e\u539f\u7406 \u2022\u00a0<code>getopts<\/code>\u00a0\u662f\u66f4\u5f3a\u5927\u7684\u53c2\u6570\u89e3\u6790\u5de5\u5177\uff0c\u652f\u6301\u5b9a\u4e49\u5e26\u503c\u9009\u9879\u548c\u9519\u8bef\u5904\u7406\u3002<\/p>\n<p>\u2022 \u57fa\u672c\u8bed\u6cd5\uff1a<code>while getopts \":ahfgvc:\" OPTION<\/code>\uff0c\u5176\u4e2d\u00a0<code>:<\/code>\u00a0\u8868\u793a\u9009\u9879\u540e\u9700\u8ddf\u503c\uff0c<code>OPTARG<\/code>\u00a0\u5b58\u50a8\u9009\u9879\u503c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5173\u952e\u7279\u6027 \u2022 \u9519\u8bef\u5904\u7406\uff1a\u65e0\u6548\u9009\u9879\u4f1a\u89e6\u53d1\u00a0<code>?<\/code>\u00a0\u5206\u652f\uff0c\u53ef\u81ea\u5b9a\u4e49\u9519\u8bef\u63d0\u793a\u3002<\/p>\n<p>\u2022 \u5e26\u503c\u9009\u9879\uff1a\u5982\u00a0<code>-c 3<\/code>\uff0c\u901a\u8fc7\u00a0<code>c) COPIES=$OPTARG<\/code>\u00a0\u83b7\u53d6\u503c\u3002<\/p>\n<p>\u2022 \u9759\u9ed8\u5904\u7406\uff1a\u901a\u8fc7\u00a0<code>:<\/code>\u00a0\u5f00\u5934\u5c4f\u853d\u9ed8\u8ba4\u9519\u8bef\u4fe1\u606f\uff0c\u5b8c\u5168\u81ea\u5b9a\u4e49\u53cd\u9988\u3002<\/p>\n<\/li>\n<li>\n<p>\u793a\u4f8b \u2022 \u5904\u7406\u9009\u9879\u00a0<code>-a<\/code>\u3001<code>-h<\/code>\u3001<code>-f<\/code>\u3001<code>-v<\/code>\u00a0\u53ca\u5e26\u503c\u9009\u9879\u00a0<code>-c<\/code>\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">while getopts \":ahfgvc:\" OPTION; do\n  case $OPTION in\n    a) ALL=true ;;\n    c) COPIES=$OPTARG ;;\n    ?) echo \"Usage: ...\"; exit 1 ;;\n  esac\ndone\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u56dbshift\u4e0egetopts\u5bf9\u6bd4\">\u56db\u3001shift\u4e0egetopts\u5bf9\u6bd4<\/h3>\n<figure class=\"table-figure\">\n<table>\n<thead>\n<tr>\n<th>\u7279\u6027<\/th>\n<th>shift<\/th>\n<th>getopts<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u7b80\u5355\u53c2\u6570\u904d\u5386<\/td>\n<td>\u590d\u6742\u9009\u9879\uff08\u542b\u5e26\u503c\u9009\u9879\u3001\u591a\u9009\u9879\uff09<\/td>\n<\/tr>\n<tr>\n<td>\u4ee3\u7801\u590d\u6742\u5ea6<\/td>\n<td>\u9700\u624b\u52a8\u7ba1\u7406\u53c2\u6570\u4f4d\u7f6e\uff0c\u4ee3\u7801\u8f83\u957f<\/td>\n<td>\u81ea\u52a8\u89e3\u6790\uff0c\u4ee3\u7801\u7b80\u6d01<\/td>\n<\/tr>\n<tr>\n<td>\u9519\u8bef\u5904\u7406<\/td>\n<td>\u9700\u81ea\u884c\u5b9e\u73b0<\/td>\n<td>\u5185\u7f6e\u9519\u8bef\u68c0\u6d4b\uff0c\u652f\u6301\u81ea\u5b9a\u4e49\u63d0\u793a<\/td>\n<\/tr>\n<tr>\n<td>\u5e26\u503c\u9009\u9879\u652f\u6301<\/td>\n<td>\u9700\u989d\u5916\u903b\u8f91\u5904\u7406<\/td>\n<td>\u76f4\u63a5\u901a\u8fc7\u00a0<code>OPTARG<\/code>\u00a0\u83b7\u53d6\u503c<\/td>\n<\/tr>\n<tr>\n<td>\u9075\u5faaUNIX\u9009\u9879\u60ef\u4f8b<\/td>\n<td>\u7075\u6d3b\u6027\u9ad8\uff0c\u4f46\u9700\u81ea\u884c\u8bbe\u8ba1<\/td>\n<td>\u5929\u7136\u652f\u6301\u6807\u51c6\u9009\u9879\u683c\u5f0f\uff08\u5982\u00a0<code>-c 3<\/code>\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr \/>\n<h3 id=\"\u4e94\u5b9e\u8df5\u6848\u4f8b\">\u4e94\u3001\u5b9e\u8df5\u6848\u4f8b<\/h3>\n<ol>\n<li>\n<p>\u6587\u4ef6\u5927\u5c0f\u5199\u8f6c\u6362\u811a\u672c\uff08shift\u7248\uff09 \u2022 \u901a\u8fc7\u00a0<code>-l<\/code>\u00a0\u548c\u00a0<code>-u<\/code>\u00a0\u6307\u5b9a\u8f6c\u6362\u65b9\u5411\uff0c\u7ed3\u5408\u00a0<code>shift<\/code>\u00a0\u904d\u5386\u6587\u4ef6\u53c2\u6570\u3002<\/p>\n<p>\u2022 \u5173\u952e\u903b\u8f91\uff1a\u89e3\u6790\u9009\u9879\u540e\uff0c\u7528\u00a0<code>shift<\/code>\u00a0\u8df3\u8fc7\u5df2\u5904\u7406\u53c2\u6570\uff0c\u5faa\u73af\u5904\u7406\u5269\u4f59\u6587\u4ef6\u540d\u3002<\/p>\n<\/li>\n<li>\n<p>\u6587\u4ef6\u5927\u5c0f\u5199\u8f6c\u6362\u811a\u672c\uff08getopts\u7248\uff09 \u2022 \u4f7f\u7528\u00a0<code>getopts<\/code>\u00a0\u89e3\u6790\u00a0<code>-l<\/code>\u3001<code>-u<\/code>\u00a0\u548c\u00a0<code>-v<\/code>\uff08\u8be6\u7ec6\u6a21\u5f0f\uff09\uff0c\u4ee3\u7801\u66f4\u7b80\u6d01\u3002<\/p>\n<p>\u2022 \u4f18\u52bf\uff1a\u8f7b\u677e\u652f\u6301\u5e26\u503c\u9009\u9879\u548c\u9519\u8bef\u5904\u7406\uff0c\u5982\u00a0<code>-v<\/code>\u00a0\u63a7\u5236\u8f93\u51fa\u8be6\u7ec6\u7a0b\u5ea6\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u516d\u6700\u4f73\u5b9e\u8df5\">\u516d\u3001\u6700\u4f73\u5b9e\u8df5<\/h3>\n<ol>\n<li>\u9009\u9879\u547d\u540d\u89c4\u8303 \u2022 \u9075\u5faaUNIX\u60ef\u4f8b\uff08\u5982\u00a0<code>-a<\/code>\u00a0\u8868\u793a\u6240\u6709\uff0c<code>-f<\/code>\u00a0\u8868\u793a\u6587\u4ef6\uff0c<code>-h<\/code>\u00a0\u663e\u793a\u5e2e\u52a9\uff09\u3002<\/li>\n<li>\u9519\u8bef\u5904\u7406 \u2022 \u59cb\u7ec8\u6821\u9a8c\u6587\u4ef6\u5b58\u5728\u6027\uff08\u5982\u00a0<code>if [ -f $1 ]<\/code>\uff09\u548c\u53c2\u6570\u6709\u6548\u6027\u3002<\/li>\n<li>\u7528\u6237\u53cb\u597d\u6027 \u2022 \u63d0\u4f9b\u6e05\u6670\u7684\u00a0<code>usage<\/code>\u00a0\u8bed\u53e5\uff0c\u5e76\u5728\u53c2\u6570\u9519\u8bef\u65f6\u9000\u51fa\uff08<code>exit 1<\/code>\uff09\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e03\u603b\u7ed3\">\u4e03\u3001\u603b\u7ed3<\/h3>\n<p>\u2022 shift \u9002\u5408\u7b80\u5355\u53c2\u6570\u904d\u5386\uff0c\u53ef\u7a81\u7834\u00a0<code>$1<\/code>~<code>$9<\/code>\u00a0\u7684\u9650\u5236\uff0c\u4f46\u5904\u7406\u590d\u6742\u9009\u9879\u65f6\u4ee3\u7801\u7e41\u7410\u3002<\/p>\n<p>\u2022 getopts \u63d0\u4f9b\u6807\u51c6\u5316\u9009\u9879\u89e3\u6790\uff0c\u652f\u6301\u5e26\u503c\u9009\u9879\u548c\u9519\u8bef\u5904\u7406\uff0c\u663e\u8457\u63d0\u5347\u811a\u672c\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u2022 \u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u63a8\u8350\u4f18\u5148\u4f7f\u7528\u00a0<code>getopts<\/code>\u00a0\u5904\u7406\u590d\u6742\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u7ed3\u5408\u00a0<code>shift<\/code>\u00a0\u5904\u7406\u5269\u4f59\u53c2\u6570\uff08\u5982\u6587\u4ef6\u540d\u5217\u8868\uff09\u3002<\/p>\n<p>\u5728Shell\u811a\u672c\u4e2d\uff0c<code>getopts<\/code>\u00a0\u662f\u7528\u4e8e\u89e3\u6790\u547d\u4ee4\u884c\u9009\u9879\u548c\u53c2\u6570\u7684\u6807\u51c6\u5de5\u5177\u3002<code>while getopts \":ahfgvc:\" OPTION<\/code>\u00a0\u4e2d\u7684\u9009\u9879\u5b57\u7b26\u4e32\u00a0<code>\":ahfgvc:\"<\/code>\u00a0\u5b9a\u4e49\u4e86\u811a\u672c\u63a5\u53d7\u7684\u9009\u9879\u53ca\u5176\u89c4\u5219\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u89e3\u91ca\uff1a<\/p>\n<hr \/>\n<h3 id=\"\u9009\u9879\u5b57\u7b26\u4e32-ahfgvc-\u7684\u4f5c\u7528\"><strong>\u9009\u9879\u5b57\u7b26\u4e32\u00a0<code>\":ahfgvc:\"<\/code>\u00a0\u7684\u4f5c\u7528<\/strong><\/h3>\n<ol>\n<li>\n<p>\u5f00\u5934\u7684\u5192\u53f7\u00a0<code>:<\/code>\u00a0\u2022 \u6291\u5236\u9ed8\u8ba4\u9519\u8bef\u5904\u7406\uff1a\u5f53\u7528\u6237\u8f93\u5165\u65e0\u6548\u9009\u9879\uff08\u5982\u00a0<code>-x<\/code>\uff09\u6216\u7f3a\u5c11\u53c2\u6570\u7684\u9009\u9879\uff08\u5982\u00a0<code>-c<\/code>\u00a0\u672a\u5e26\u53c2\u6570\uff09\u65f6\uff0c<code>getopts<\/code>\u00a0\u9ed8\u8ba4\u4f1a\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002\u5f00\u5934\u7684\u5192\u53f7\u00a0<code>:<\/code>\u00a0\u4f1a\u5173\u95ed\u8fd9\u4e00\u884c\u4e3a\uff0c\u5141\u8bb8\u811a\u672c\u81ea\u884c\u5904\u7406\u9519\u8bef\u3002<\/p>\n<p>\u2022 \u7279\u6b8a\u8fd4\u56de\u503c\uff1a<\/p>\n<p>\u25e6\u00a0<code>OPTION<\/code>\u00a0\u8bbe\u4e3a\u00a0<code>?<\/code>\u00a0\u8868\u793a\u65e0\u6548\u9009\u9879\uff08\u5982\u00a0<code>-x<\/code>\uff09\u3002<\/p>\n<p>\u25e6\u00a0<code>OPTION<\/code>\u00a0\u8bbe\u4e3a\u00a0<code>:<\/code>\u00a0\u8868\u793a\u9009\u9879\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570\uff08\u5982\u00a0<code>-c<\/code>\u00a0\u540e\u672a\u8ddf\u53c2\u6570\uff09\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b57\u6bcd\u00a0<code>a<\/code>,\u00a0<code>h<\/code>,\u00a0<code>f<\/code>,\u00a0<code>g<\/code>,\u00a0<code>v<\/code>\u00a0\u2022 \u8fd9\u4e9b\u662f\u65e0\u9700\u53c2\u6570\u7684\u9009\u9879\u3002\u4f8b\u5982\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">script.sh -a -h -f\n<\/code><\/pre>\n<p>\u25e6\u00a0<code>-a<\/code>,\u00a0<code>-h<\/code>,\u00a0<code>-f<\/code>,\u00a0<code>-g<\/code>,\u00a0<code>-v<\/code>\u00a0\u4f1a\u88ab\u8bc6\u522b\u4e3a\u5408\u6cd5\u9009\u9879\uff0c\u4e0d\u8981\u6c42\u540e\u7eed\u53c2\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b57\u6bcd\u00a0<code>c:<\/code>\u00a0\u2022 \u5192\u53f7\u00a0<code>:<\/code>\u00a0\u5728\u00a0<code>c<\/code>\u00a0\u540e\u8868\u793a\u00a0<code>-c<\/code>\u00a0\u9700\u8981\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">script.sh -c 5\n<\/code><\/pre>\n<p>\u25e6 \u53c2\u6570\u00a0<code>5<\/code>\u00a0\u4f1a\u88ab\u4fdd\u5b58\u5230\u00a0<code>$OPTARG<\/code>\u00a0\u53d8\u91cf\u4e2d\uff0c\u4f9b\u811a\u672c\u4f7f\u7528\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<p><strong>\u9009\u9879\u5b57\u7b26\u4e32\u7684\u7ed3\u6784<\/strong>\u00a0\u2022 \u683c\u5f0f\uff1a<code>\":\u5408\u6cd5\u9009\u9879\u5217\u8868\"<\/code><\/p>\n<p>\u2022 \u5f00\u5934\u7684\u00a0<code>:<\/code>\u00a0\u8868\u793a\u6291\u5236\u9519\u8bef\u8f93\u51fa\u3002<\/p>\n<p>\u2022 \u6bcf\u4e2a\u5b57\u6bcd\u8868\u793a\u4e00\u4e2a\u9009\u9879\uff0c\u5b57\u6bcd\u540e\u7684\u00a0<code>:<\/code>\u00a0\u8868\u793a\u8be5\u9009\u9879\u9700\u8981\u53c2\u6570\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\u5b57\u7b26\u4e32\u00a0<code>\":ahfgvc:\"<\/code>\u00a0\u7684\u89e3\u6790<\/p>\n<figure class=\"table-figure\">\n<table>\n<thead>\n<tr>\n<th>\u9009\u9879<\/th>\n<th>\u662f\u5426\u9700\u8981\u53c2\u6570<\/th>\n<th>\u793a\u4f8b\u7528\u6cd5<\/th>\n<th>\u53c2\u6570\u4f4d\u7f6e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>a<\/code><\/td>\n<td>\u5426<\/td>\n<td><code>-a<\/code><\/td>\n<td>\u65e0<\/td>\n<\/tr>\n<tr>\n<td><code>h<\/code>,\u00a0<code>f<\/code>,\u00a0<code>g<\/code>, `v<\/td>\n<td>\u5426<\/td>\n<td><code>-h<\/code>,\u00a0<code>-f<\/code><\/td>\n<td>\u65e0<\/td>\n<\/tr>\n<tr>\n<td><code>c:<\/code><\/td>\n<td>\u662f<\/td>\n<td><code>-c 5<\/code><\/td>\n<td>\u901a\u8fc7\u00a0<code>$OPTARG<\/code>\u00a0\u83b7\u53d6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<hr \/>\n<p><strong>\u5728\u811a\u672c\u4e2d\u7684\u5178\u578b\u7528\u6cd5<\/strong><\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">while getopts \":ahfgvc:\" OPTION; do\n  case $OPTION in\n    a) echo \"Option -a \u88ab\u89e6\u53d1\" ;;\n    h) echo \"Option -h \u88ab\u89e6\u53d1\" ;;\n    f) echo \"Option -f \u88ab\u89e6\u53d1\" ;;\n    g) echo \"Option -g \u88ab\u89e6\u53d1\" ;;\n    v) echo \"Option -v \u88ab\u89e6\u53d1\" ;;\n    c) echo \"Option -c \u7684\u53c2\u6570\u662f: $OPTARG\" ;;\n    :) echo \"\u9519\u8bef\uff1a\u9009\u9879 -$OPTARG \u9700\u8981\u53c2\u6570\" &gt;&amp;2 ;;\n    ?) echo \"\u9519\u8bef\uff1a\u65e0\u6548\u9009\u9879 -$OPTARG\" &gt;&amp;2 ;;\n  esac\ndone\n<\/code><\/pre>\n<p>\u2022 \u5904\u7406\u5e26\u53c2\u6570\u7684\u9009\u9879\uff08\u5982\u00a0<code>-c<\/code>\uff09<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">script.sh -c 10\n<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<code>Option -c \u7684\u53c2\u6570\u662f: 10<\/code><\/p>\n<p>\u2022 \u5904\u7406\u975e\u6cd5\u9009\u9879\uff08\u5982\u00a0<code>-x<\/code>\uff09<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">script.sh -x\n<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<code>\u9519\u8bef\uff1a\u65e0\u6548\u9009\u9879 -x<\/code><\/p>\n<p>\u2022 \u5904\u7406\u7f3a\u5c11\u53c2\u6570\u7684\u9009\u9879\uff08\u5982\u00a0<code>-c<\/code>\u00a0\u672a\u5e26\u53c2\u6570\uff09<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">script.sh -c\n<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<code>\u9519\u8bef\uff1a\u9009\u9879 -c \u9700\u8981\u53c2\u6570<\/code><\/p>\n<hr \/>\n<p><strong>\u603b\u7ed3<\/strong>\u00a0\u2022\u00a0<code>:ahfgvc:<\/code>\u00a0\u5b9a\u4e49\u4e86\u811a\u672c\u63a5\u53d7\u7684\u9009\u9879\uff1a<\/p>\n<p>\u2022\u00a0<code>a<\/code>,\u00a0<code>h<\/code>,\u00a0<code>f<\/code>,\u00a0<code>g<\/code>,\u00a0<code>v<\/code>\u00a0\u662f\u5f00\u5173\u9009\u9879\uff08\u65e0\u9700\u53c2\u6570\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>c<\/code>\u00a0\u5fc5\u987b\u540e\u8ddf\u53c2\u6570\uff08\u5982\u00a0<code>-c 5<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u5f00\u5934\u7684\u00a0<code>:<\/code>\u00a0\u5141\u8bb8\u811a\u672c\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u2022\u00a0<code>getopts<\/code>\u00a0\u4f1a\u81ea\u52a8\u5904\u7406\u9009\u9879\u7684\u987a\u5e8f\u548c\u7ec4\u5408\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">script.sh -a -c 3 -h\n<\/code><\/pre>\n<p>\u4f1a\u4f9d\u6b21\u89e3\u6790\u00a0<code>-a<\/code>,\u00a0<code>-c 3<\/code>,\u00a0<code>-h<\/code>\u3002<\/p>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u811a\u672c\u53ef\u4ee5\u7075\u6d3b\u3001\u5065\u58ee\u5730\u5904\u7406\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u7b26\u5408UNIX\/Linux\u5de5\u5177\u7684\u6807\u51c6\u884c\u4e3a\u3002<\/p>\n<h2 id=\"\u7b2c21\u7ae0\u521b\u5efa\u5c4f\u5e55\u8f93\u51fa\u7684\u8be6\u7ec6\u603b\u7ed3\">\u7b2c21\u7ae0\u521b\u5efa\u5c4f\u5e55\u8f93\u51fa\u7684\u8be6\u7ec6\u603b\u7ed3<\/h2>\n<p>\u6838\u5fc3\u76ee\u6807 \u4f7f\u7528Shell\u811a\u672c\u521b\u5efa\u4ea4\u4e92\u5f0f\u3001\u4e13\u4e1a\u7684\u5c4f\u5e55\u8f93\u51fa\uff0c\u4f9d\u8d56<code>tput<\/code>\u547d\u4ee4\u548c\u7ec8\u7aef\u63a7\u5236\u5e8f\u5217\uff08\u8f6c\u4e49\u5e8f\u5217\uff09\uff0c\u5c24\u5176\u5173\u6ce8\u989c\u8272\u3001\u5149\u6807\u63a7\u5236\u548c\u83dc\u5355\u8bbe\u8ba1\u3002<\/p>\n<hr \/>\n<h3 id=\"\u4e00tput\u547d\u4ee4\">\u4e00\u3001tput\u547d\u4ee4<\/h3>\n<p>\u4f5c\u7528\uff1a\u901a\u8fc7\u7ec8\u7aef\u6570\u636e\u5e93\uff08terminfo\/termcap\uff09\u63a7\u5236\u7ec8\u7aef\u5c5e\u6027\uff0c\u751f\u6210\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5e03\u5c14\u8f93\u51fa\u3002<\/p>\n<ol>\n<li>\u5b57\u7b26\u4e32\u8f93\u51fa \u63a7\u5236\u6587\u672c\u6837\u5f0f\u548c\u7ec8\u7aef\u884c\u4e3a\uff0c\u5e38\u7528\u9009\u9879\uff1a \u2022\u00a0<code>bel<\/code>\uff1a\u54cd\u94c3<\/li>\n<\/ol>\n<p>\u2022\u00a0<code>clear<\/code>\uff1a\u6e05\u5c4f<\/p>\n<p>\u2022\u00a0<code>cup r c<\/code>\uff1a\u79fb\u52a8\u5149\u6807\u5230\u884cr\u5217c<\/p>\n<p>\u2022\u00a0<code>bold<\/code>\uff1a\u7c97\u4f53<\/p>\n<p>\u2022\u00a0<code>smul<\/code>\/<code>rmul<\/code>\uff1a\u4e0b\u5212\u7ebf\u5f00\u5173<\/p>\n<p>\u2022\u00a0<code>rev<\/code>\uff1a\u53cd\u8272\u663e\u793a<\/p>\n<p>\u2022\u00a0<code>civis<\/code>\/<code>cnorm<\/code>\uff1a\u9690\u85cf\/\u663e\u793a\u5149\u6807<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">BOLD=$(tput bold)  # \u542f\u7528\u7c97\u4f53\necho \"${BOLD}Hello World\"\n<\/code><\/pre>\n<ol>\n<li>\u6570\u5b57\u8f93\u51fa \u83b7\u53d6\u7ec8\u7aef\u4fe1\u606f\uff1a \u2022\u00a0<code>cols<\/code>\uff1a\u7ec8\u7aef\u5217\u6570<\/li>\n<\/ol>\n<p>\u2022\u00a0<code>lines<\/code>\uff1a\u7ec8\u7aef\u884c\u6570<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">COLS=$(tput cols)  # \u83b7\u53d6\u5f53\u524d\u7ec8\u7aef\u5bbd\u5ea6\n<\/code><\/pre>\n<ol>\n<li>\u5e03\u5c14\u8f93\u51fa \u5224\u65ad\u7ec8\u7aef\u529f\u80fd\u662f\u5426\u5b58\u5728\uff1a \u2022\u00a0<code>hs<\/code>\uff1a\u662f\u5426\u6709\u72b6\u6001\u884c<\/li>\n<\/ol>\n<p>\u2022\u00a0<code>chts<\/code>\uff1a\u5149\u6807\u662f\u5426\u53ef\u89c1<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">if tput hs; then echo \"\u7ec8\u7aef\u652f\u6301\u72b6\u6001\u884c\"; fi\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u4e8c\u8f6c\u4e49\u5e8f\u5217\u4e0e\u63a7\u5236\u7801\">\u4e8c\u3001\u8f6c\u4e49\u5e8f\u5217\u4e0e\u63a7\u5236\u7801<\/h3>\n<p>\u4f5c\u7528\uff1a\u76f4\u63a5\u53d1\u9001\u63a7\u5236\u7801\u6539\u53d8\u7ec8\u7aef\u884c\u4e3a\uff0c\u5982\u6e05\u5c4f\u3001\u5149\u6807\u4f4d\u7f6e\u3001\u989c\u8272\u3002<\/p>\n<ol>\n<li>\u57fa\u672c\u8bed\u6cd5 \u2022 \u8f6c\u4e49\u5e8f\u5217\u4ee5<code>\\033[<\/code>\u5f00\u5934\uff08<code>\\033<\/code>\u4ee3\u8868Esc\u952e\uff09\u3002<\/li>\n<\/ol>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>\u2022 \u6e05\u5c4f\uff1a<code>echo -e \"\\033[2J\"<\/code><\/p>\n<p>\u2022 \u79fb\u52a8\u5149\u6807\uff1a<code>echo -e \"\\033[10;5H\"<\/code>\uff08\u7b2c10\u884c\u7b2c5\u5217\uff09<\/p>\n<ol>\n<li>\u5149\u6807\u63a7\u5236 \u2022 \u9690\u85cf\/\u663e\u793a\u5149\u6807\uff1a<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">echo -e \"\\033[?25l\"  # \u9690\u85cf\necho -e \"\\033[?25h\"  # \u663e\u793a\n<\/code><\/pre>\n<p>\u2022 \u4fdd\u5b58\/\u6062\u590d\u5149\u6807\u4f4d\u7f6e\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">echo -e \"\\033[s\"     # \u4fdd\u5b58\necho -e \"\\033[u\"     # \u6062\u590d\n<\/code><\/pre>\n<ol>\n<li>\u5c4f\u5e55\u5c45\u4e2d\u663e\u793a \u901a\u8fc7\u8ba1\u7b97\u5217\u6570\u52a8\u6001\u5c45\u4e2d\u6587\u672c\uff1a<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">center() {\n  str=$1\n  len=$(echo -n $str | wc -c)\n  cols=$(tput cols)\n  pos=$(( (cols - len) \/ 2 ))\n  tput cup $2 $pos\n  echo -n \"$str\"\n}\ncenter \"Hello\" 5  # \u5728\u7b2c5\u884c\u5c45\u4e2d\u663e\u793a\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u4e09\u989c\u8272\u8bbe\u7f6e\">\u4e09\u3001\u989c\u8272\u8bbe\u7f6e<\/h3>\n<p>ANSI\u989c\u8272\u7801\uff1a \u2022 \u524d\u666f\u8272\uff1a30-37\uff08\u9ed1\u3001\u7ea2\u3001\u7eff\u3001\u9ec4\u3001\u84dd\u3001\u7d2b\u3001\u9752\u3001\u767d\uff09<\/p>\n<p>\u2022 \u80cc\u666f\u8272\uff1a40-47\uff08\u540c\u4e0a\uff09<\/p>\n<ol>\n<li>\u989c\u8272\u8bed\u6cd5 \u2022 \u683c\u5f0f\uff1a<code>\\033[\u80cc\u666f;\u524d\u666fm<\/code><\/li>\n<\/ol>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">echo -e \"\\033[40;32m\u7eff\u5e95\u9ed1\u5b57\\033[0m\"  # \u6062\u590d\u9ed8\u8ba4\u7528\\033[0m\n<\/code><\/pre>\n<ol>\n<li>\u989c\u8272\u51fd\u6570\u5c01\u88c5<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">color() {\n  case $1 in\n    red) echo -e \"\\033[31m\";;\n    green_bg) echo -e \"\\033[42m\";;\n    reset) echo -e \"\\033[0m\";;\n  esac\n}\necho \"$(color red)Error$(color reset)\"\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u56db\u4ea4\u4e92\u5f0f\u83dc\u5355\u8bbe\u8ba1\">\u56db\u3001\u4ea4\u4e92\u5f0f\u83dc\u5355\u8bbe\u8ba1<\/h3>\n<ol>\n<li>\u529f\u80fd\u952e\u5904\u7406 \u901a\u8fc7<code>cat -v<\/code>\u6355\u83b7\u6309\u952e\u8f6c\u4e49\u5e8f\u5217\uff0c\u7ed1\u5b9a\u5230\u811a\u672c\u903b\u8f91\uff1a<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">read -rsn1 key\ncase $key in\n  $'\\x1b')  # \u5904\u7406Esc\u952e\n    read -rsn2 -t 1 key\n    case $key in\n      [A) echo \"\u4e0a\u7bad\u5934\";;\n      [B) echo \"\u4e0b\u7bad\u5934\";;\n    esac\n    ;;\nesac\n<\/code><\/pre>\n<ol>\n<li>\u6743\u9650\u63a7\u5236 \u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\uff08\u5982<code>priv.user<\/code>\uff09\u7ba1\u7406\u7528\u6237\u6743\u9650\uff1a<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">user_level() {\n  while read user priv; do\n    if [[ $user == $USER ]]; then\n      [[ $priv == \"yes\" ]] &amp;&amp; return 0 || return 1\n    fi\n  done &lt; priv.user\n  return 1\n}\nif user_level; then\n  echo \"\u5141\u8bb8\u4fee\u6539\"\nelse\n  echo \"\u6743\u9650\u4e0d\u8db3\"\nfi\n<\/code><\/pre>\n<ol>\n<li>\u5b8c\u6574\u83dc\u5355\u793a\u4f8b \u96c6\u6210tput\u3001\u989c\u8272\u3001\u5149\u6807\u63a7\u5236\u548c\u6743\u9650\u9a8c\u8bc1\uff1a<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">#!\/bin\/bash\ntrap '' 2 3 15  # \u5ffd\u7565\u4e2d\u65ad\u4fe1\u53f7\nwhile true; do\n  clear\n  tput cup 3 10\n  echo -e \"$(tput rev) \u4e3b\u83dc\u5355 $(tput sgr0)\"\n  echo \"1. \u65b0\u589e\u8bb0\u5f55\"\n  echo \"2. \u67e5\u770b\u8bb0\u5f55\"\n  echo \"Q. \u9000\u51fa\"\n  read -p \"\u9009\u62e9: \" choice\n  case $choice in\n    1) add_record;;\n    2) view_record;;\n    Q) break;;\n  esac\ndone\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u4e94\u6ce8\u610f\u4e8b\u9879\">\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ol>\n<li>\u7ec8\u7aef\u517c\u5bb9\u6027\uff1a\u4e0d\u540c\u7ec8\u7aef\u5bf9\u63a7\u5236\u5e8f\u5217\u7684\u652f\u6301\u53ef\u80fd\u4e0d\u540c\uff0c\u4f18\u5148\u4f7f\u7528<code>tput<\/code>\u800c\u975e\u786c\u7f16\u7801\u8f6c\u4e49\u5e8f\u5217\u3002<\/li>\n<li>\u989c\u8272\u8282\u5236\uff1a\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528\u989c\u8272\uff0c\u786e\u4fdd\u53ef\u8bfb\u6027\u3002<\/li>\n<li>\u5149\u6807\u63a7\u5236\uff1a\u64cd\u4f5c\u540e\u6062\u590d\u5149\u6807\u72b6\u6001\uff0c\u907f\u514d\u811a\u672c\u5f02\u5e38\u9000\u51fa\u65f6\u5149\u6807\u9690\u85cf\u3002<\/li>\n<li>\u4fe1\u53f7\u5904\u7406\uff1a\u4f7f\u7528<code>trap<\/code>\u9632\u6b62\u7528\u6237\u901a\u8fc7Ctrl+C\u4e2d\u65ad\u5173\u952e\u6d41\u7a0b\u3002<\/li>\n<\/ol>\n<hr \/>\n<h2 id=\"\u7b2c23\u7ae0-\u8c03\u8bd5\u811a\u672c-\u8be6\u7ec6\u603b\u7ed3\">\u7b2c23\u7ae0 \u8c03\u8bd5\u811a\u672c \u8be6\u7ec6\u603b\u7ed3<\/h2>\n<p>\u4e00\u3001\u8c03\u8bd5\u539f\u5219\u4e0e\u9884\u9632\u9519\u8bef<\/p>\n<ol>\n<li>\u5206\u9636\u6bb5\u6d4b\u8bd5 \u2022 \u5efa\u8bae\u5c06\u811a\u672c\u62c6\u5206\u4e3a\u591a\u4e2a\u72ec\u7acb\u4efb\u52a1\u6216\u8fc7\u7a0b\uff0c\u6bcf\u4e2a\u90e8\u5206\u5b8c\u6210\u65f6\u7acb\u5373\u6d4b\u8bd5\uff0c\u786e\u4fdd\u65e0\u8bef\u540e\u518d\u7ee7\u7eed\u5f00\u53d1\u3002<\/li>\n<li>\u9519\u8bef\u5b9a\u4f4d\u6280\u5de7 \u2022 Shell\u62a5\u9519\u65f6\u9700\u5173\u6ce8\u6574\u4e2a\u76f8\u5173\u4ee3\u7801\u6bb5\u800c\u975e\u4ec5\u9519\u8bef\u884c\u3002Shell\u7684\u9519\u8bef\u63d0\u793a\u901a\u5e38\u6307\u5411\u8bed\u6cd5\u7ed3\u6784\u7ed3\u675f\u5904\uff08\u5982<code>done<\/code>\u3001<code>fi<\/code>\u7b49\uff09\u3002<\/li>\n<\/ol>\n<hr \/>\n<p>\u4e8c\u3001\u5e38\u89c1\u9519\u8bef\u7c7b\u578b\u53ca\u89e3\u51b3\u65b9\u6cd5<\/p>\n<ol>\n<li><strong>\u5faa\u73af\u7ed3\u6784\u9519\u8bef<\/strong>\u00a0\u2022 \u8868\u73b0\uff1a\u5982<code>syntax error near unexpected token 'done'<\/code>\u3002<\/li>\n<\/ol>\n<p>\u2022 \u539f\u56e0\uff1a<code>for\/while\/until\/case<\/code>\u8bed\u53e5\u7ed3\u6784\u4e0d\u5b8c\u6574\uff0c\u53ef\u80fd\u6f0f\u5199<code>do\/done\/fi<\/code>\u7b49\u5173\u952e\u5b57\u3002<\/p>\n<p>\u2022 \u89e3\u51b3\uff1a\u56de\u6eaf\u4ee3\u7801\u6bb5\uff0c\u68c0\u67e5\u5faa\u73af\u7ed3\u6784\u662f\u5426\u5b8c\u6574\u3002<\/p>\n<ol>\n<li><strong>\u6f0f\u5199\u5f15\u53f7\u9519\u8bef<\/strong>\u00a0\u2022 \u8868\u73b0\uff1a<code>unexpected EOF while looking for \u2018\u2019\u2019<\/code>\u3002<\/li>\n<\/ol>\n<p>\u2022 \u539f\u56e0\uff1a\u5b57\u7b26\u4e32\u5f15\u53f7\u672a\u95ed\u5408\uff08\u5982\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u4e0d\u6210\u5bf9\uff09\u3002<\/p>\n<p>\u2022 \u89e3\u51b3\uff1a\u68c0\u67e5\u6240\u6709\u5b57\u7b26\u4e32\u5f15\u53f7\u662f\u5426\u6210\u5bf9\uff0c\u5c24\u5176\u662f\u591a\u884c\u5b57\u7b26\u4e32\u3002<\/p>\n<ol>\n<li><strong>\u6d4b\u8bd5\u6761\u4ef6\u9519\u8bef<\/strong>\u00a0\u2022 \u8868\u73b0\uff1a<code>[: missing ']'<\/code>\u00a0\u6216\u64cd\u4f5c\u7b26\u9519\u8bef\u3002<\/li>\n<\/ol>\n<p>\u2022 \u539f\u56e0\uff1a<\/p>\n<p>\u2022 \u6d4b\u8bd5\u6761\u4ef6\uff08<code>[ ]<\/code>\u6216<code>[[ ]]<\/code>\uff09\u5185\u90e8\u7f3a\u5c11\u64cd\u4f5c\u7b26\uff08\u5982<code>-eq<\/code>,\u00a0<code>==<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u53d8\u91cf\u4e0e\u65b9\u62ec\u53f7\u4e4b\u95f4\u7f3a\u5c11\u7a7a\u683c\uff08\u5982<code>[$VAR]<\/code>\u5e94\u4e3a<code>[ $VAR ]<\/code>\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-eq<\/code>\u7528\u4e8e\u975e\u6570\u5b57\u6bd4\u8f83\uff08\u5e94\u7528<code>=<\/code>\u6216<code>==<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u89e3\u51b3\uff1a\u786e\u4fdd\u6d4b\u8bd5\u6761\u4ef6\u8bed\u6cd5\u6b63\u786e\uff0c\u64cd\u4f5c\u7b26\u4e0e\u6570\u636e\u7c7b\u578b\u5339\u914d\u3002<\/p>\n<ol>\n<li><strong>\u53d8\u91cf\u5927\u5c0f\u5199\u4e0d\u4e00\u81f4<\/strong>\u00a0\u2022 \u8868\u73b0\uff1a\u53d8\u91cf\u672a\u6b63\u786e\u8d4b\u503c\u6216\u5f15\u7528\u3002<\/li>\n<\/ol>\n<p>\u2022 \u539f\u56e0\uff1a\u5b9a\u4e49\u53d8\u91cf\u65f6\u7528\u5927\u5199\uff08\u5982<code>NAME=\"John\"<\/code>\uff09\uff0c\u5f15\u7528\u65f6\u8bef\u7528\u5c0f\u5199\uff08\u5982<code>echo $name<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u89e3\u51b3\uff1a\u7edf\u4e00\u53d8\u91cf\u547d\u540d\u89c4\u8303\uff08\u63a8\u8350\u5168\u5c0f\u5199\u52a0\u4e0b\u5212\u7ebf\uff09\u3002<\/p>\n<ol>\n<li><strong>for\u5faa\u73af\u53d8\u91cf\u5f15\u7528\u9519\u8bef<\/strong>\u00a0\u2022 \u8868\u73b0\uff1a\u5faa\u73af\u5217\u8868\u672a\u6b63\u786e\u89e3\u6790\u53d8\u91cf\u3002<\/li>\n<\/ol>\n<p>\u2022 \u539f\u56e0\uff1a\u5728\u5faa\u73af\u5217\u8868\u4e2d\u4f7f\u7528\u53d8\u91cf\u65f6\u6f0f\u5199<code>$<\/code>\u7b26\u53f7\uff08\u5982<code>for LOOP in LIST<\/code>\u5e94\u4e3a<code>for LOOP in $LIST<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u89e3\u51b3\uff1a\u786e\u4fdd\u5f15\u7528\u53d8\u91cf\u65f6\u4f7f\u7528<code>$<\/code>\u7b26\u53f7\uff08\u5982<code>$LIST<\/code>\uff09\u3002<\/p>\n<ol>\n<li><strong>echo\u547d\u4ee4\u7684\u8bef\u7528<\/strong>\u00a0\u2022 \u8868\u73b0\uff1a\u8c03\u8bd5\u65f6\u8bef\u5224\u547d\u4ee4\u6267\u884c\u72b6\u6001\u3002<\/li>\n<\/ol>\n<p>\u2022 \u539f\u56e0\uff1a\u5728<code>echo<\/code>\u540e\u76f4\u63a5\u68c0\u67e5<code>$?<\/code>\u72b6\u6001\uff08<code>echo<\/code>\u672c\u8eab\u603b\u8fd4\u56de\u6210\u529f\u72b6\u6001\uff09\u3002<\/p>\n<p>\u2022 \u89e3\u51b3\uff1a\u5728\u5173\u952e\u547d\u4ee4\u540e\u76f4\u63a5\u68c0\u67e5<code>$?<\/code>\uff0c\u907f\u514d\u4e0e<code>echo<\/code>\u6df7\u6dc6\u3002<\/p>\n<hr \/>\n<p>\u4e09\u3001\u8c03\u8bd5\u5de5\u5177\u4e0e\u65b9\u6cd5<\/p>\n<ol>\n<li><strong>echo\u547d\u4ee4\u8c03\u8bd5<\/strong>\u00a0\u2022 \u7528\u9014\uff1a\u5728\u5173\u952e\u4f4d\u7f6e\u8f93\u51fa\u53d8\u91cf\u503c\u6216\u6267\u884c\u6d41\u7a0b\uff08\u5982<code>echo \"Processing $VAR\"<\/code>\uff09\u3002<\/li>\n<\/ol>\n<p>\u2022 \u4f18\u52bf\uff1a\u7b80\u5355\u76f4\u89c2\uff0c\u53ef\u5feb\u901f\u5b9a\u4f4d\u53d8\u91cf\u8d4b\u503c\u6216\u903b\u8f91\u9519\u8bef\u3002<\/p>\n<ol>\n<li><strong>set\u547d\u4ee4\u8c03\u8bd5\u9009\u9879<\/strong>\u00a0\u2022 \u5f00\u542f\u65b9\u5f0f\uff1a\u5728\u811a\u672c\u4e2d\u63d2\u5165<code>set -option<\/code>\uff0c\u5173\u95ed\u7528<code>set +option<\/code>\u3002<\/li>\n<\/ol>\n<p>\u2022 \u5e38\u7528\u9009\u9879\uff1a<\/p>\n<p>\u2022\u00a0<code>set -n<\/code>\uff08\u8bed\u6cd5\u68c0\u67e5\u6a21\u5f0f\uff09\uff1a\u4ec5\u89e3\u6790\u811a\u672c\u8bed\u6cd5\uff0c\u4e0d\u6267\u884c\u547d\u4ee4\u3002<\/p>\n<p>\u2022\u00a0<code>set -v<\/code>\uff08\u8be6\u7ec6\u8f93\u51fa\u6a21\u5f0f\uff09\uff1a\u9010\u884c\u663e\u793a\u811a\u672c\u8bfb\u53d6\u5185\u5bb9\u3002<\/p>\n<p>\u2022\u00a0<code>set -x<\/code>\uff08\u8ffd\u8e2a\u6267\u884c\u6a21\u5f0f\uff09\uff1a\u663e\u793a\u6bcf\u6761\u547d\u4ee4\u53ca\u5176\u53c2\u6570\uff08\u6700\u5e38\u7528\uff09\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">#!\/bin\/sh\nset -x  # \u5f00\u542f\u8ffd\u8e2a\nLIST=\"Peter Susan John\"\nfor LOOP in $LIST; do\n  echo \"Name: $LOOP\"\ndone\nset +x  # \u5173\u95ed\u8ffd\u8e2a\n<\/code><\/pre>\n<p>\u2022 \u8f93\u51fa\u6548\u679c\uff1a<\/p>\n<pre><code>+ LIST=Peter Susan John\n+ for LOOP in '$LIST'\n+ echo 'Name: Peter'\nName: Peter\n+ for LOOP in '$LIST'\n+ echo 'Name: Susan'\nName: Susan\n...\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u56db\u8c03\u8bd5\u5b9e\u4f8b\u5206\u6790\">\u56db\u3001\u8c03\u8bd5\u5b9e\u4f8b\u5206\u6790<\/h3>\n<p>\u2022 \u573a\u666f\uff1a\u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u5728\u9884\u8bbe\u5217\u8868\u4e2d\u3002<\/p>\n<p>\u2022 \u9519\u8bef\u73b0\u8c61\uff1a\u8f93\u5165\u4e0d\u5728\u5217\u8868\u4e2d\u4f46\u65e0\u63d0\u793a\u3002<\/p>\n<p>\u2022 \u8c03\u8bd5\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528<code>set -x<\/code>\u8ffd\u8e2a\u53d8\u91cf\u6bd4\u8f83\u8fc7\u7a0b\u3002<\/li>\n<li>\u53d1\u73b0<code>if [\"$LOOP\"=\"$NAME\"]<\/code>\u672a\u6b63\u786e\u5206\u9694\u64cd\u4f5c\u7b26\uff08\u5e94\u4e3a<code>\"$LOOP\" = \"$NAME\"<\/code>\uff09\u3002<\/li>\n<li>\u4fee\u6b63\u540e\u91cd\u65b0\u6d4b\u8bd5\uff0c\u786e\u8ba4\u903b\u8f91\u6b63\u786e\u6027\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u4e94\u603b\u7ed3\">\u4e94\u3001\u603b\u7ed3<\/h3>\n<p>\u2022 \u6838\u5fc3\u7b56\u7565\uff1a\u9884\u9632\u4f18\u4e8e\u8c03\u8bd5\uff0c\u5206\u9636\u6bb5\u6d4b\u8bd5\u51cf\u5c11\u9519\u8bef\u3002<\/p>\n<p>\u2022 \u8c03\u8bd5\u6d41\u7a0b\uff1a<\/p>\n<ol>\n<li>\u4f18\u5148\u4f7f\u7528<code>echo<\/code>\u8f93\u51fa\u4e2d\u95f4\u53d8\u91cf\u3002<\/li>\n<li>\u590d\u6742\u903b\u8f91\u5f00\u542f<code>set -x<\/code>\u8ffd\u8e2a\u6267\u884c\u7ec6\u8282\u3002<\/li>\n<li>\u5173\u6ce8Shell\u62a5\u9519\u63d0\u793a\u7684\u7ed3\u6784\u6027\u9519\u8bef\uff08\u5982<code>done\/fi<\/code>\u7f3a\u5931\uff09\u3002 \u2022 \u6ce8\u610f\u4e8b\u9879\uff1a<\/li>\n<\/ol>\n<p>\u2022 \u53d8\u91cf\u5f15\u7528\u4e25\u683c\u533a\u5206\u5927\u5c0f\u5199\u548c<code>$<\/code>\u7b26\u53f7\u3002<\/p>\n<p>\u2022 \u6d4b\u8bd5\u6761\u4ef6\u786e\u4fdd\u8bed\u6cd5\u6b63\u786e\uff08\u7a7a\u683c\u3001\u64cd\u4f5c\u7b26\u3001\u6570\u636e\u7c7b\u578b\uff09\u3002<\/p>\n<p>\u2022 \u591a\u884c\u5b57\u7b26\u4e32\u548c\u5faa\u73af\u7ed3\u6784\u9700\u95ed\u5408\u5f15\u53f7\u53ca\u5173\u952e\u5b57\u3002<\/p>\n<h2 id=\"\u7b2c24\u7ae0\u8be6shell\u5d4c\u5165\u547d\u4ee4\">\u7b2c24\u7ae0\u8be6Shell\u5d4c\u5165\u547d\u4ee4<\/h2>\n<p>\u8fd9\u4e9b\u547d\u4ee4\u5185\u7f6e\u4e8eBourne Shell\u4e2d\uff0c\u6267\u884c\u901f\u5ea6\u5feb\u4e8e\u5916\u90e8\u547d\u4ee4\u3002\u4ee5\u4e0b\u662f\u4e3b\u8981\u5185\u5bb9\u603b\u7ed3\uff1a<\/p>\n<hr \/>\n<h3 id=\"1-\u5d4c\u5165\u547d\u4ee4\u6982\u8ff0\"><strong>1. \u5d4c\u5165\u547d\u4ee4\u6982\u8ff0<\/strong><\/h3>\n<p>\u2022 \u5b9a\u4e49\uff1a\u5d4c\u5165\u547d\u4ee4\u662fShell\u5185\u7f6e\u7684\uff0c\u800c\u975e\u5b58\u50a8\u5728<code>\/bin<\/code>\u6216<code>usr\/bin<\/code>\u4e2d\u7684\u5916\u90e8\u547d\u4ee4\u3002<\/p>\n<p>\u2022 \u4f18\u52bf\uff1a\u6267\u884c\u6548\u7387\u66f4\u9ad8\uff0c\u65e0\u9700\u542f\u52a8\u5916\u90e8\u8fdb\u7a0b\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<code>cd<\/code>\u3001<code>pwd<\/code>\u3001<code>echo<\/code>\u7b49\u5e38\u7528\u547d\u4ee4\u5747\u4e3a\u5d4c\u5165\u547d\u4ee4\uff0c\u4f46\u4e5f\u53ef\u901a\u8fc7\u8def\u5f84\u8c03\u7528\u5916\u90e8\u7248\u672c\uff08\u5982<code>\/bin\/pwd<\/code>\uff09\u3002<\/p>\n<hr \/>\n<h3 id=\"2-\u6807\u51c6\u5d4c\u5165\u547d\u4ee4\u5217\u8868\u53ca\u8be6\u89e3\"><strong>2. \u6807\u51c6\u5d4c\u5165\u547d\u4ee4\u5217\u8868\u53ca\u8be6\u89e3<\/strong><\/h3>\n<p>\u200b<strong>\u200b\u5173\u952e\u547d\u4ee4\u529f\u80fd\u603b\u7ed3\u200b<\/strong>\u200b\u2022 \u63a7\u5236\u6d41\uff1a<\/p>\n<p>\u2022\u00a0<code>break<\/code>\uff1a\u9000\u51fa\u5faa\u73af\uff08<code>for<\/code>\/<code>while<\/code>\/<code>until<\/code>\/<code>case<\/code>\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>continue<\/code>\uff1a\u8df3\u8fc7\u5f53\u524d\u5faa\u73af\u8fed\u4ee3\uff0c\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\u3002<\/p>\n<p>\u2022\u00a0<code>return<\/code>\uff1a\u9000\u51fa\u51fd\u6570\u5e76\u8fd4\u56de\u6307\u5b9a\u503c\u3002<\/p>\n<p>\u2022\u00a0<code>exit<\/code>\uff1a\u9000\u51fa\u5f53\u524dShell\u3002<\/p>\n<p>\u2022\u00a0<code>eval<\/code>\uff1a\u89e3\u6790\u53c2\u6570\u5e76\u6267\u884c\u7ed3\u679c\u547d\u4ee4\uff08\u7528\u4e8e\u52a8\u6001\u751f\u6210\u547d\u4ee4\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>exec<\/code>\uff1a\u6267\u884c\u547d\u4ee4\u5e76\u66ff\u6362\u5f53\u524dShell\u8fdb\u7a0b\uff08\u6267\u884c\u540e\u4e0d\u8fd4\u56de\u539fShell\uff09\u3002<\/p>\n<p>\u2022 \u76ee\u5f55\u4e0e\u6587\u4ef6\uff1a<\/p>\n<p>\u2022\u00a0<code>cd<\/code>\uff1a\u5207\u6362\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<p>\u2022\u00a0<code>pwd<\/code>\uff1a\u663e\u793a\u5f53\u524d\u76ee\u5f55\uff08\u5185\u5efa\u7248\u66f4\u9ad8\u6548\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>umask<\/code>\uff1a\u8bbe\u7f6e\u9ed8\u8ba4\u6587\u4ef6\u521b\u5efa\u6743\u9650\u63a9\u7801\u3002<\/p>\n<p>\u2022\u00a0<code>ulimit<\/code>\uff1a\u8bbe\u7f6e\u6216\u663e\u793aShell\u8d44\u6e90\u9650\u5236\uff08\u5982\u6838\u5fc3\u8f6c\u50a8\u6587\u4ef6\u5927\u5c0f\u3001\u6700\u5927\u8fdb\u7a0b\u6570\u7b49\uff09\u3002<\/p>\n<p>\u2022 \u53d8\u91cf\u4e0e\u53c2\u6570\uff1a<\/p>\n<p>\u2022\u00a0<code>export<\/code>\uff1a\u5bfc\u51fa\u53d8\u91cf\uff0c\u4f7f\u5b50\u8fdb\u7a0b\u53ef\u8bbf\u95ee\u3002<\/p>\n<p>\u2022\u00a0<code>readonly<\/code>\uff1a\u6807\u8bb0\u53d8\u91cf\u4e3a\u53ea\u8bfb\u3002<\/p>\n<p>\u2022\u00a0<code>set<\/code>\uff1a\u8bbe\u7f6e\u4f4d\u7f6e\u53c2\u6570\u6216\u63a7\u5236Shell\u9009\u9879\uff08\u5982\u8c03\u8bd5\u6a21\u5f0f<code>set -x<\/code>\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>shift<\/code>\uff1a\u5de6\u79fb\u4f4d\u7f6e\u53c2\u6570\uff0c\u7528\u4e8e\u904d\u5386\u53c2\u6570\u5217\u8868\u3002<\/p>\n<p>\u2022\u00a0<code>unset<\/code>\uff1a\u5220\u9664\u53d8\u91cf\u6216\u51fd\u6570\u3002<\/p>\n<p>\u2022 \u8f93\u5165\u8f93\u51fa\uff1a<\/p>\n<p>\u2022\u00a0<code>echo<\/code>\uff1a\u8f93\u51fa\u4fe1\u606f\u5230\u6807\u51c6\u8f93\u51fa\u3002<\/p>\n<p>\u2022\u00a0<code>read<\/code>\uff1a\u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u4e00\u884c\u6570\u636e\u3002<\/p>\n<p>\u2022 \u8fdb\u7a0b\u4e0e\u4fe1\u53f7\uff1a<\/p>\n<p>\u2022\u00a0<code>wait<\/code>\uff1a\u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ed3\u675f\uff08\u53ef\u6307\u5b9a\u8fdb\u7a0bID\u6216\u7b49\u5f85\u6240\u6709\u5b50\u8fdb\u7a0b\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>trap<\/code>\uff1a\u6355\u83b7\u4fe1\u53f7\u5e76\u6267\u884c\u6307\u5b9a\u547d\u4ee4\uff08\u5982\u6e05\u7406\u4e34\u65f6\u6587\u4ef6\uff09\u3002<\/p>\n<p>\u2022 \u8c03\u8bd5\u4e0e\u6027\u80fd\uff1a<\/p>\n<p>\u2022\u00a0<code>times<\/code>\uff1a\u663e\u793aShell\u53ca\u5b50\u8fdb\u7a0b\u7684\u7528\u6237\u548c\u7cfb\u7edf\u65f6\u95f4\uff0c\u7528\u4e8e\u6027\u80fd\u5206\u6790\u3002<\/p>\n<p>\u2022\u00a0<code>type<\/code>\uff1a\u67e5\u8be2\u547d\u4ee4\u7c7b\u578b\uff08\u5d4c\u5165\/\u5916\u90e8\uff09\u53ca\u8def\u5f84\uff08\u5982<code>type pwd<\/code>\u663e\u793a\u5176\u4e3a\u5185\u5efa\u547d\u4ee4\uff09\u3002<\/p>\n<hr \/>\n<h3 id=\"3-\u91cd\u70b9\u547d\u4ee4\u8be6\u89e3\"><strong>3. \u91cd\u70b9\u547d\u4ee4\u8be6\u89e3<\/strong><\/h3>\n<p>\u200b<strong>\u200b<code>set<\/code>\u200b<\/strong>\u200b\u2022 \u7528\u9014\uff1a<\/p>\n<p>\u2022 \u8bbe\u7f6e\u4f4d\u7f6e\u53c2\u6570\uff08\u5982<code>set param1 param2<\/code>\uff09\uff0c\u53ef\u5728\u811a\u672c\u5185\u52a8\u6001\u6307\u5b9a\u53c2\u6570\u3002<\/p>\n<p>\u2022 \u63a7\u5236Shell\u9009\u9879\uff08\u5982<code>set -x<\/code>\u542f\u7528\u8c03\u8bd5\u6a21\u5f0f\uff0c<code>set +x<\/code>\u5173\u95ed\uff09\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># \u811a\u672c\u5185\u8bbe\u7f6e\u53c2\u6570\u5e76\u904d\u5386\nset accounts.doc accounts.bak\nwhile [ $# -ne 0 ]; do\n  echo $1\n  shift\ndone\n<\/code><\/pre>\n<p><strong><code>ulimit<\/code><\/strong>\u00a0\u2022 \u5e38\u7528\u9009\u9879\uff1a<\/p>\n<p>\u2022\u00a0<code>-a<\/code>\uff1a\u663e\u793a\u6240\u6709\u8d44\u6e90\u9650\u5236\u3002<\/p>\n<p>\u2022\u00a0<code>-c<\/code>\uff1a\u9650\u5236\u6838\u5fc3\u8f6c\u50a8\u6587\u4ef6\u5927\u5c0f\uff08<code>ulimit -c 0<\/code>\u7981\u7528\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>-f<\/code>\uff1a\u9650\u5236\u8fdb\u7a0b\u521b\u5efa\u6587\u4ef6\u7684\u6700\u5927\u5927\u5c0f\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">ulimit -a        # \u663e\u793a\u5f53\u524d\u9650\u5236\nulimit -c 1000   # \u5141\u8bb8\u751f\u6210\u6700\u59271000\u5757\u7684\u6838\u5fc3\u8f6c\u50a8\u6587\u4ef6\n<\/code><\/pre>\n<p><strong><code>eval<\/code>\u4e0e<code>exec<\/code><\/strong>\u00a0\u2022\u00a0<code>eval<\/code>\uff1a\u89e3\u6790\u53c2\u6570\u5e76\u6267\u884c\u751f\u6210\u7684\u547d\u4ee4\uff0c\u5e38\u7528\u4e8e\u52a8\u6001\u6784\u9020\u547d\u4ee4\u3002<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">cmd=\"ls -l\"\neval $cmd        # \u6267\u884c\"ls -l\"\n<\/code><\/pre>\n<p>\u2022\u00a0<code>exec<\/code>\uff1a\u6267\u884c\u547d\u4ee4\u5e76\u66ff\u6362\u5f53\u524dShell\u8fdb\u7a0b\uff0c\u540e\u7eed\u811a\u672c\u4e0d\u518d\u6267\u884c\u3002<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">exec \/bin\/bash   # \u66ff\u6362\u5f53\u524dShell\u4e3aBash\n<\/code><\/pre>\n<p><strong><code>type<\/code><\/strong>\u00a0\u2022 \u7528\u9014\uff1a\u5224\u65ad\u547d\u4ee4\u7c7b\u578b\uff08\u5185\u5efa\/\u5916\u90e8\uff09\u53ca\u8def\u5f84\u3002<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">type pwd     # \u8f93\u51fa\uff1apwd is a shell builtin\ntype cp      # \u8f93\u51fa\uff1acp is \/bin\/cp\n<\/code><\/pre>\n<p><strong><code>wait<\/code><\/strong>\u00a0\u2022 \u7528\u9014\uff1a\u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ed3\u675f\uff0c\u7528\u4e8e\u5e76\u53d1\u4efb\u52a1\u540c\u6b65\u3002<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">sleep 5 &amp;    # \u540e\u53f0\u8fd0\u884csleep\nwait         # \u7b49\u5f85\u6240\u6709\u540e\u53f0\u8fdb\u7a0b\u5b8c\u6210\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"4-\u5e94\u7528\u573a\u666f\u4e0e\u6700\u4f73\u5b9e\u8df5\"><strong>4. \u5e94\u7528\u573a\u666f\u4e0e\u6700\u4f73\u5b9e\u8df5<\/strong><\/h3>\n<p>\u2022 \u6027\u80fd\u4f18\u5316\uff1a\u4f18\u5148\u4f7f\u7528\u5185\u5efa\u547d\u4ee4\uff08\u5982<code>pwd<\/code>\u800c\u975e<code>\/bin\/pwd<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u811a\u672c\u8c03\u8bd5\uff1a\u5229\u7528<code>set -x<\/code>\u548c<code>times<\/code>\u5206\u6790\u6267\u884c\u6d41\u7a0b\u4e0e\u8017\u65f6\u3002<\/p>\n<p>\u2022 \u8d44\u6e90\u7ba1\u7406\uff1a\u901a\u8fc7<code>ulimit<\/code>\u9632\u6b62\u811a\u672c\u8017\u5c3d\u7cfb\u7edf\u8d44\u6e90\uff08\u5982\u9650\u5236\u6587\u4ef6\u5927\u5c0f\uff09\u3002<\/p>\n<p>\u2022 \u53c2\u6570\u5904\u7406\uff1a\u4f7f\u7528<code>set<\/code>\u548c<code>shift<\/code>\u52a8\u6001\u7ba1\u7406\u811a\u672c\u53c2\u6570\u3002<\/p>\n<p>\u2022 \u8fdb\u7a0b\u63a7\u5236\uff1a<code>wait<\/code>\u548c<code>trap<\/code>\u5b9e\u73b0\u8fdb\u7a0b\u540c\u6b65\u4e0e\u4fe1\u53f7\u5904\u7406\u3002<\/p>\n<hr \/>\n<h3 id=\"5-\u6ce8\u610f\u4e8b\u9879\"><strong>5. \u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n<p>\u2022 \u547d\u4ee4\u51b2\u7a81\uff1a\u82e5\u9700\u8c03\u7528\u5916\u90e8\u547d\u4ee4\uff0c\u9700\u6307\u5b9a\u5b8c\u6574\u8def\u5f84\uff08\u5982<code>\/bin\/pwd<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u8d44\u6e90\u9650\u5236\uff1a<code>ulimit<\/code>\u8bbe\u7f6e\u53ef\u80fd\u56e0\u7cfb\u7edf\u9ed8\u8ba4\u503c\u4e0d\u540c\u800c\u9700\u8c03\u6574\uff0c\u5efa\u8bae\u5728\u811a\u672c\u521d\u59cb\u5316\u65f6\u914d\u7f6e\u3002<\/p>\n<p>\u2022 \u5b89\u5168\u6027\uff1a<code>eval<\/code>\u6267\u884c\u52a8\u6001\u547d\u4ee4\u5b58\u5728\u98ce\u9669\uff0c\u9700\u8c28\u614e\u5904\u7406\u4e0d\u53ef\u4fe1\u8f93\u5165\u3002<\/p>\n<hr \/>\n<p>\u638c\u63e1\u8fd9\u4e9b\u5d4c\u5165\u547d\u4ee4\u7684\u4f7f\u7528\uff0c\u80fd\u591f\u663e\u8457\u63d0\u5347Shell\u811a\u672c\u7684\u6548\u7387\u548c\u53ef\u63a7\u6027\uff0c\u5c24\u5176\u5728\u8d44\u6e90\u7ba1\u7406\u3001\u8c03\u8bd5\u548c\u5e76\u53d1\u5904\u7406\u7b49\u573a\u666f\u4e2d\u8868\u73b0\u7a81\u51fa\u3002<\/p>\n<h2 id=\"\u7b2c25\u7ae0\u6df1\u5165\u8ba8\u8bba-&lt;&lt;-\u7684\u603b\u7ed3\">\u7b2c25\u7ae0\u6df1\u5165\u8ba8\u8bba\u00a0<code>&lt;&lt;<\/code>\u00a0\u7684\u603b\u7ed3<\/h2>\n<h3 id=\"1-\u57fa\u7840\u56de\u987e\">1.\u00a0<strong>\u57fa\u7840\u56de\u987e<\/strong><\/h3>\n<p><code>&lt;&lt;<\/code>\uff08here\u6587\u6863\uff09\u7528\u4e8e\u5c06\u591a\u884c\u8f93\u5165\u4f20\u9012\u7ed9\u547d\u4ee4\uff0c\u683c\u5f0f\u4e3a\uff1a<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">command &lt;&lt; delimiter\n\u8f93\u5165\u5185\u5bb9\ndelimiter\n<\/code><\/pre>\n<p><code>delimiter<\/code>\u00a0\u662f\u81ea\u5b9a\u4e49\u7684\u7ed3\u675f\u6807\u8bb0\uff0c\u8f93\u5165\u5185\u5bb9\u76f4\u5230\u518d\u6b21\u9047\u5230\u8be5\u6807\u8bb0\u4e3a\u6b62\u3002<\/p>\n<hr \/>\n<h3 id=\"2-\u6838\u5fc3\u5e94\u7528\u573a\u666f\">2.\u00a0<strong>\u6838\u5fc3\u5e94\u7528\u573a\u666f<\/strong><\/h3>\n<h5 id=\"21-\u5feb\u901f\u521b\u5efa\u6587\u4ef6\"><strong>2.1 \u5feb\u901f\u521b\u5efa\u6587\u4ef6<\/strong><\/h5>\n<ul>\n<li>\n<p>\u00a0<\/p>\n<p>\u7528\u6cd5<\/p>\n<p>\uff1a\u901a\u8fc7<\/p>\n<p>\u00a0<\/p>\n<pre><code>cat\n<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>\u547d\u4ee4\u548c<\/p>\n<p>\u00a0<\/p>\n<pre><code>&lt;&lt;\n<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>\u76f4\u63a5\u8f93\u5165\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">cat &gt;&gt; myfile &lt;&lt; NEWFILE\n\u6587\u672c\u5185\u5bb9\nNEWFILE\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u00a0<\/p>\n<p>\u6ce8\u610f\u4e8b\u9879<\/p>\n<p>\uff1a<\/p>\n<ul>\n<li>\n<p>\u82e5\u5185\u5bb9\u542b<\/p>\n<p>\u00a0<\/p>\n<pre><code>tab\n<\/code><\/pre>\n<p>\uff0c\u9700\u4f7f\u7528<\/p>\n<p>\u00a0<\/p>\n<pre><code>&lt;&lt;-\n<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>\u5ffd\u7565\u884c\u9996\u7684\u5236\u8868\u7b26\uff08\u517c\u5bb9\u8001\u7248\u672cShell\uff09\uff1a<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">cat &gt;&gt; myfile &lt;&lt;- NEWFILE\n\tTab\u7f29\u8fdb\u7684\u6587\u672c\nNEWFILE\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5 id=\"22-\u5feb\u901f\u751f\u6210\u6253\u5370\u6587\u6863\"><strong>2.2 \u5feb\u901f\u751f\u6210\u6253\u5370\u6587\u6863<\/strong><\/h5>\n<ul>\n<li>\n<p>\u00a0<\/p>\n<p>\u7528\u6cd5<\/p>\n<p>\uff1a\u76f4\u63a5\u53d1\u9001\u591a\u884c\u6587\u672c\u5230\u6253\u5370\u673a\u6216\u5176\u4ed6\u547d\u4ee4\uff08\u5982<\/p>\n<p>\u00a0<\/p>\n<pre><code>lpr\n<\/code><\/pre>\n<p>\uff09\u3002<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">lpr &lt;&lt; QUICKDOC\n\u4f1a\u8bae\u901a\u77e5\uff1a\u4e0b\u5468\u4e3e\u529e\u661f\u9645\u8ff7\u822a\u5927\u4f1a\u3002\n\u7968\u4ef7\u8bf7\u7535\u8bdd\u54a8\u8be2\u3002\nQUICKDOC\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<h5 id=\"23-\u81ea\u52a8\u9009\u62e9\u83dc\u5355\"><strong>2.3 \u81ea\u52a8\u9009\u62e9\u83dc\u5355<\/strong><\/h5>\n<ul>\n<li>\n<p>\u00a0<\/p>\n<p>\u573a\u666f<\/p>\n<p>\uff1a\u81ea\u52a8\u5316\u4ea4\u4e92\u5f0f\u811a\u672c\u7684\u83dc\u5355\u9009\u62e9\uff08\u5982\u6570\u636e\u5e93\u5907\u4efd\uff09\u3002<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">\/usr\/local\/sybin\/syb_backup &gt;&gt; $log_f 2&gt;&amp;1 &lt;&lt; MAYDAY\n2\n3\nY\nMAYDAY\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u00a0<\/p>\n<p>\u5173\u952e\u70b9<\/p>\n<p>\uff1a<\/p>\n<ul>\n<li>\u901a\u8fc7\u9884\u8bbe\u8f93\u5165\u5e8f\u5217\u6a21\u62df\u7528\u6237\u9009\u62e9\uff08\u5982\u00a0<code>2<\/code>\u00a0\u2192\u00a0<code>3<\/code>\u00a0\u2192\u00a0<code>Y<\/code>\uff09\u3002<\/li>\n<li>\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u65e5\u5fd7\u6587\u4ef6\uff08<code>&gt;&gt; $log_f 2&gt;&amp;1<\/code>\uff09\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5 id=\"24-\u81ea\u52a8ftp\u4f20\u8f93\"><strong>2.4 \u81ea\u52a8FTP\u4f20\u8f93<\/strong><\/h5>\n<ul>\n<li>\n<p>\u00a0<\/p>\n<p>\u811a\u672c\u5b9e\u73b0<\/p>\n<p>\uff1a<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">ftp -i -n $DEST_HOST &lt;&lt; FTPIT\nuser anonymous $USER@$THIS_HOST\n$MODE\nget $FILENAME $DIREC\/$FILENAME.ftp\nquit\nFTPIT\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u00a0<\/p>\n<p>\u529f\u80fd\u7ec6\u8282<\/p>\n<p>\uff1a<\/p>\n<ul>\n<li><code>-i<\/code>\u00a0\u5173\u95ed\u4ea4\u4e92\u63d0\u793a\uff0c<code>-n<\/code>\u00a0\u7981\u6b62\u81ea\u52a8\u767b\u5f55\u3002<\/li>\n<li>\u652f\u6301\u4e8c\u8fdb\u5236\/ASCII\u4f20\u8f93\u6a21\u5f0f\u9009\u62e9\uff0c\u9ed8\u8ba4\u8def\u5f84\u4e3a\u00a0<code>\/tmp<\/code>\u3002<\/li>\n<li>\u9519\u8bef\u68c0\u67e5\uff1a\u82e5\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u56de\u9000\u5230\u9ed8\u8ba4\u8def\u5f84\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5 id=\"25-\u8fde\u63a5\u6570\u636e\u5e93\"><strong>2.5 \u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h5>\n<ul>\n<li>\n<p>\u00a0<\/p>\n<p>\u573a\u666f<\/p>\n<p>\uff1a\u6279\u91cf\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff08\u5982\u542f\u7528<\/p>\n<p>\u00a0<\/p>\n<pre><code>select into\n<\/code><\/pre>\n<p>\u00a0<\/p>\n<p>\u529f\u80fd\uff09\u3002<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">for db in $DATABASES; do\n  su sybase -c '\/sybase\/bin\/isql -Usa -P$PASSWORD' &lt;&lt; MAYDAY\n  use master\n  go\n  sp_dboption $db, \"select into\/bulkcopy\/pllsort\", true\n  go\n  use $db\n  go\n  checkpoint\n  go\nMAYDAY\ndone\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u4f18\u52bf<\/strong>\uff1a\u5faa\u73af\u904d\u5386\u6570\u636e\u5e93\uff0c\u81ea\u52a8\u53d1\u9001SQL\u6307\u4ee4\uff0c\u65e0\u9700\u624b\u52a8\u4ea4\u4e92\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3 id=\"3-\u9ad8\u7ea7\u6280\u5de7\u4e0e\u6ce8\u610f\u4e8b\u9879\">3.\u00a0<strong>\u9ad8\u7ea7\u6280\u5de7\u4e0e\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u9ed8\u8ba4\u503c\u5904\u7406<\/strong>\uff1a \u4f7f\u7528\u00a0<code>${VAR:=default}<\/code>\u00a0\u8bbe\u7f6e\u53d8\u91cf\u9ed8\u8ba4\u503c\uff0c\u63d0\u5347\u811a\u672c\u5065\u58ee\u6027\u3002<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">echo -n \"Enter host [default: my_host]: \"\nread DEST_HOST\n: ${DEST_HOST:=my_host}\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u5904\u7406\u4e0e\u65e5\u5fd7<\/strong>\uff1a<\/p>\n<ul>\n<li>\u91cd\u5b9a\u5411\u8f93\u51fa\u5230\u65e5\u5fd7\u6587\u4ef6\uff1a<code>command &gt;&gt; logfile 2&gt;&amp;1<\/code>\u3002<\/li>\n<li>\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff1a<code>if [ -s $file ]; then ...<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u517c\u5bb9\u6027<\/strong>\uff1a<\/p>\n<ul>\n<li>\u907f\u514d\u4f9d\u8d56\u7279\u5b9aShell\u7279\u6027\uff08\u5982\u00a0<code>&lt;&lt;-<\/code>\u00a0\u5904\u7406\u7f29\u8fdb\uff09\u3002<\/li>\n<li>\u6d4b\u8bd5\u4e0d\u540cShell\u73af\u5883\uff08\u5982\u00a0<code>bash<\/code>\u3001<code>sh<\/code>\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<h3 id=\"4-\u5c0f\u7ed3\">4.\u00a0<strong>\u5c0f\u7ed3<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u6838\u5fc3\u4ef7\u503c<\/strong>\uff1a\u00a0<code>&lt;&lt;<\/code>\u00a0\u662fShell\u811a\u672c\u4e2d\u5f3a\u5927\u7684\u8f93\u5165\u91cd\u5b9a\u5411\u5de5\u5177\uff0c\u9002\u7528\u4e8e\uff1a<\/p>\n<ul>\n<li>\u5feb\u901f\u751f\u6210\u6587\u4ef6\u6216\u6587\u6863\u3002<\/li>\n<li>\u81ea\u52a8\u5316\u4ea4\u4e92\u5f0f\u6d41\u7a0b\uff08\u83dc\u5355\u3001FTP\u3001\u6570\u636e\u5e93\u64cd\u4f5c\uff09\u3002<\/li>\n<li>\u4e0e\u5916\u90e8\u5de5\u5177\uff08\u5982\u00a0<code>ftp<\/code>\u3001<code>isql<\/code>\uff09\u96c6\u6210\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4f18\u52bf<\/strong>\uff1a<\/p>\n<ul>\n<li>\u51cf\u5c11\u624b\u52a8\u64cd\u4f5c\uff0c\u63d0\u9ad8\u6548\u7387\u3002<\/li>\n<li>\u901a\u8fc7\u811a\u672c\u590d\u7528\u964d\u4f4e\u7ef4\u62a4\u6210\u672c\u3002<\/li>\n<li>\u7ed3\u5408\u65e5\u5fd7\u548c\u9519\u8bef\u68c0\u67e5\u589e\u5f3a\u53ef\u9760\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5b9a\u65f6\u4efb\u52a1\uff08\u5982\u00a0<code>cron<\/code>\u00a0\u81ea\u52a8\u5907\u4efd\uff09\u3002<\/li>\n<li>\u6279\u91cf\u5904\u7406\uff08\u5982\u591a\u6570\u636e\u5e93\u914d\u7f6e\uff09\u3002<\/li>\n<li>\u5feb\u901f\u751f\u6210\u4e34\u65f6\u6587\u4ef6\u6216\u62a5\u544a\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u00a0<code>&lt;&lt;<\/code>\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u7b80\u5316\u590d\u6742\u4efb\u52a1\uff0c\u5c06\u4ea4\u4e92\u5f0f\u64cd\u4f5c\u8f6c\u5316\u4e3a\u81ea\u52a8\u5316\u6d41\u7a0b\uff0c\u663e\u8457\u63d0\u5347\u811a\u672c\u7684\u529f\u80fd\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<h2 id=\"\u7b2c26\u7ae0-shell\u5de5\u5177\u603b\u7ed3\u4e0e\u8be6\u7ec6\u89e3\u91ca\">\u7b2c26\u7ae0 Shell\u5de5\u5177\u603b\u7ed3\u4e0e\u8be6\u7ec6\u89e3\u91ca<\/h2>\n<p>\u672c\u7ae0\u4ecb\u7ecd\u4e86Shell\u811a\u672c\u4e2d\u5e38\u7528\u7684\u5de5\u5177\u548c\u6280\u672f\uff0c\u5305\u62ec\u6587\u4ef6\u7ba1\u7406\u3001\u4fe1\u53f7\u5904\u7406\u3001\u547d\u4ee4\u89e3\u6790\u548c\u65e5\u5fd7\u8bb0\u5f55\u3002\u4ee5\u4e0b\u662f\u5bf9\u5404\u90e8\u5206\u7684\u8be6\u7ec6\u603b\u7ed3\uff1a<\/p>\n<hr \/>\n<h3 id=\"1-\u521b\u5efa\u65e5\u5fd7\u548c\u4e34\u65f6\u6587\u4ef6\"><strong>1. \u521b\u5efa\u65e5\u5fd7\u548c\u4e34\u65f6\u6587\u4ef6<\/strong><\/h3>\n<p>\u2022 \u76ee\u7684\uff1a\u751f\u6210\u552f\u4e00\u547d\u540d\u7684\u6587\u4ef6\uff0c\u4fbf\u4e8e\u7ba1\u7406\u548c\u6e05\u7406\u3002<\/p>\n<p>\u2022 \u5173\u952e\u5de5\u5177\uff1a<\/p>\n<p>\u2022\u00a0<code>date<\/code>\u547d\u4ee4\uff1a\u751f\u6210\u683c\u5f0f\u5316\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code>\u25e6 \u793a\u4f8b\uff1a`date +%d%m%y` \u8f93\u51fa\u65e5\u3001\u6708\u3001\u5e74\uff08\u5982 `090699`\uff09\u3002\n\n\u25e6 \u5e38\u7528\u683c\u5f0f\u7b26\uff1a`%d`\uff08\u65e5\uff09\uff0c`%m`\uff08\u6708\uff09\uff0c`%Y`\uff08\u56db\u4f4d\u5e74\uff09\uff0c`%T`\uff08\u65f6\u95f4\uff0c\u5982 `10:29:41`\uff09\u3002\n<\/code><\/pre>\n<p>\u2022 \u8fdb\u7a0bID\uff08<code>$$<\/code>\uff09\uff1a\u751f\u6210\u552f\u4e00\u7684\u4e34\u65f6\u6587\u4ef6\u540d\u540e\u7f00\u3002<\/p>\n<pre><code>\u25e6 \u793a\u4f8b\uff1a`\/tmp\/hold1.$$` \u751f\u6210\u7c7b\u4f3c `\/tmp\/hold1.1234` \u7684\u6587\u4ef6\u3002\n<\/code><\/pre>\n<p>\u2022 \u5e94\u7528\u573a\u666f\uff1a<\/p>\n<p>\u2022 \u65e5\u5fd7\u6587\u4ef6\uff1a\u6309\u65e5\u671f\u547d\u540d\uff08\u5982\u00a0<code>backup_log.090699<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u4e34\u65f6\u6587\u4ef6\uff1a\u811a\u672c\u7ed3\u675f\u65f6\u901a\u8fc7\u00a0<code>rm \/tmp\/*.$$<\/code>\u00a0\u6e05\u7406\u3002<\/p>\n<hr \/>\n<h3 id=\"2-\u4fe1\u53f7\u5904\u7406\"><strong>2. \u4fe1\u53f7\u5904\u7406<\/strong><\/h3>\n<p>\u2022 \u4fe1\u53f7\uff08Signal\uff09\uff1a\u7cfb\u7edf\u5411\u8fdb\u7a0b\u53d1\u9001\u7684\u4e8b\u4ef6\u901a\u77e5\uff08\u5982\u4e2d\u65ad\u3001\u7ec8\u6b62\uff09\u3002<\/p>\n<p>\u2022 \u5e38\u89c1\u4fe1\u53f7\uff1a<\/p>\n<pre><code>| \u4fe1\u53f7\u7f16\u53f7 | \u540d\u79f0     | \u8bf4\u660e                   |\n|----------|----------|------------------------|\n| 1        | SIGHUP   | \u7ec8\u7aef\u6302\u8d77               |\n| 2        | SIGINT   | \u952e\u76d8\u4e2d\u65ad\uff08Ctrl+C\uff09     |\n| 9        | SIGKILL  | \u5f3a\u5236\u7ec8\u6b62\uff08\u4e0d\u53ef\u6355\u83b7\uff09   |\n| 15       | SIGTERM  | \u8f6f\u4ef6\u7ec8\u6b62\uff08\u9ed8\u8ba4\u4fe1\u53f7\uff09   |\n<\/code><\/pre>\n<p>\u2022\u00a0<code>trap<\/code>\u547d\u4ee4\uff1a\u6355\u83b7\u4fe1\u53f7\u5e76\u6267\u884c\u6307\u5b9a\u64cd\u4f5c\u3002<\/p>\n<p>\u2022 \u8bed\u6cd5\uff1a<code>trap \"command\" signal_list<\/code><\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\ntrap \"my_exit\" 2 3 15  # \u6355\u83b7\u4fe1\u53f72\/3\/15\u65f6\u6267\u884cmy_exit\u51fd\u6570\ntrap \"\" 2              # \u5ffd\u7565\u4fe1\u53f72\uff08Ctrl+C\u65e0\u6548\uff09\ntrap - 2               # \u6062\u590d\u9ed8\u8ba4\u4fe1\u53f7\u5904\u7406\n```\n<\/code><\/pre>\n<p>\u2022 \u5e94\u7528\u573a\u666f\uff1a<\/p>\n<pre><code>\u25e6 \u6e05\u7406\u4e34\u65f6\u6587\u4ef6\uff1a\u811a\u672c\u9000\u51fa\u524d\u5220\u9664\u4e34\u65f6\u6587\u4ef6\u3002\n\n\u25e6 \u7528\u6237\u786e\u8ba4\u9000\u51fa\uff1a\u6355\u83b7\u4e2d\u65ad\u4fe1\u53f7\u65f6\u63d0\u793a\u7528\u6237\u786e\u8ba4\u3002\n\n\u25e6 \u9501\u7ec8\u7aef\uff1a\u9632\u6b62\u610f\u5916\u4e2d\u65ad\u5173\u952e\u64cd\u4f5c\uff08\u5982\u5bc6\u7801\u8f93\u5165\uff09\u3002\n<\/code><\/pre>\n<hr \/>\n<p><strong>3.\u00a0<code>eval<\/code>\u547d\u4ee4<\/strong>\u00a0\u2022 \u529f\u80fd\uff1a\u5bf9\u53c2\u6570\u8fdb\u884c\u4e24\u6b21\u89e3\u6790\uff08\u53d8\u91cf\u66ff\u6362 + \u547d\u4ee4\u6267\u884c\uff09\u3002<\/p>\n<p>\u2022 \u5e94\u7528\u573a\u666f\uff1a<\/p>\n<p>\u2022 \u52a8\u6001\u6267\u884c\u547d\u4ee4\uff1a\u53d8\u91cf\u5b58\u50a8\u547d\u4ee4\u5b57\u7b26\u4e32\u65f6\uff0c\u901a\u8fc7<code>eval<\/code>\u89e3\u6790\u6267\u884c\u3002<\/p>\n<pre><code>```bash\nCMD=\"echo Hello\"\neval $CMD  # \u8f93\u51fa Hello\n```\n<\/code><\/pre>\n<p>\u2022 \u95f4\u63a5\u5f15\u7528\u53d8\u91cf\uff1a\u901a\u8fc7\u53d8\u91cf\u540d\u52a8\u6001\u83b7\u53d6\u503c\u3002<\/p>\n<pre><code>```bash\nVAR=\"value\"\nNAME=\"VAR\"\neval echo \\$$NAME  # \u8f93\u51fa value\n```\n<\/code><\/pre>\n<p>\u2022 \u89e3\u6790\u590d\u6742\u8f93\u5165\uff1a\u4ece\u6587\u4ef6\u8bfb\u53d6\u53d8\u91cf\u540d\u548c\u503c\u5e76\u52a8\u6001\u8bbe\u7f6e\u3002<\/p>\n<pre><code>```bash\nwhile read NAME VALUE; do\n  eval \"$NAME=$VALUE\"\ndone &lt; data.txt\n```\n<\/code><\/pre>\n<hr \/>\n<p><strong>4.\u00a0<code>logger<\/code>\u547d\u4ee4<\/strong>\u00a0\u2022 \u529f\u80fd\uff1a\u5411\u7cfb\u7edf\u65e5\u5fd7\uff08\u5982\u00a0<code>\/var\/log\/messages<\/code>\uff09\u5199\u5165\u6d88\u606f\u3002<\/p>\n<p>\u2022 \u8bed\u6cd5\uff1a<code>logger -p \u4f18\u5148\u7ea7 -i \"\u6d88\u606f\"<\/code><\/p>\n<p>\u2022 \u4f18\u5148\u7ea7\uff1a<code>notice<\/code>\uff08\u9ed8\u8ba4\uff09\u3001<code>info<\/code>\u3001<code>debug<\/code>\u7b49\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<code>-i<\/code>\u00a0\u8bb0\u5f55\u8fdb\u7a0bID\u3002<\/p>\n<p>\u2022 \u5e94\u7528\u573a\u666f\uff1a<\/p>\n<p>\u2022 \u811a\u672c\u76d1\u63a7\uff1a\u8bb0\u5f55\u811a\u672c\u542f\u52a8\u3001\u7ed3\u675f\u6216\u9519\u8bef\u3002<\/p>\n<p>\u2022 \u5ba1\u8ba1\u65e5\u5fd7\uff1a\u8ddf\u8e2a\u7528\u6237\u767b\u5f55\u6216\u5173\u952e\u64cd\u4f5c\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\nlogger -p notice \"Script $0 started by $USER\"\n# \u65e5\u5fd7\u6761\u76ee\u793a\u4f8b\uff1aJun 17 11:02:53 hostname user: Script test.sh started by dave\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u5173\u952e\u5b9e\u8df5\u5efa\u8bae\"><strong>\u5173\u952e\u5b9e\u8df5\u5efa\u8bae<\/strong><\/h3>\n<ol>\n<li>\n<p>\u6587\u4ef6\u7ba1\u7406\uff1a \u2022 \u4f7f\u7528\u65e5\u671f\u6216\u8fdb\u7a0bID\u786e\u4fdd\u6587\u4ef6\u540d\u552f\u4e00\u3002<\/p>\n<p>\u2022 \u59cb\u7ec8\u5728\u811a\u672c\u9000\u51fa\u524d\u6e05\u7406\u4e34\u65f6\u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u4fe1\u53f7\u5904\u7406\uff1a \u2022 \u5173\u952e\u64cd\u4f5c\u671f\u95f4\u5ffd\u7565\u4e2d\u65ad\u4fe1\u53f7\uff08\u5982\u6587\u4ef6\u5199\u5165\uff09\u3002<\/p>\n<p>\u2022 \u63d0\u4f9b\u7528\u6237\u53cb\u597d\u7684\u9000\u51fa\u786e\u8ba4\u673a\u5236\u3002<\/p>\n<\/li>\n<li>\n<p><code>eval<\/code>\u4f7f\u7528\u6ce8\u610f\uff1a \u2022 \u907f\u514d\u6267\u884c\u672a\u7ecf\u9a8c\u8bc1\u7684\u5b57\u7b26\u4e32\uff0c\u9632\u6b62\u5b89\u5168\u6f0f\u6d1e\u3002<\/p>\n<\/li>\n<li>\n<p>\u65e5\u5fd7\u8bb0\u5f55\uff1a \u2022 \u91cd\u8981\u811a\u672c\u5e94\u8bb0\u5f55\u8fd0\u884c\u72b6\u6001\u81f3\u7cfb\u7edf\u65e5\u5fd7\uff0c\u4fbf\u4e8e\u6545\u969c\u6392\u67e5\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<h3 id=\"\u793a\u4f8b\u811a\u672c\u89e3\u6790\"><strong>\u793a\u4f8b\u811a\u672c\u89e3\u6790<\/strong><\/h3>\n<p>\u2022 \u6309\u65e5\u671f\u521b\u5efa\u65e5\u5fd7\u6587\u4ef6\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">LOGFILE=\"\/logs\/backup_$(date +%d%m%y)\"\ntouch $LOGFILE\n<\/code><\/pre>\n<p>\u2022 \u6355\u83b7\u4fe1\u53f7\u5e76\u6e05\u7406\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">trap \"rm -f \/tmp\/*.$$; exit\" 2 3 15\n<\/code><\/pre>\n<p>\u2022 \u52a8\u6001\u53d8\u91cf\u8d4b\u503c\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">eval \"echo \\$$VARNAME\"  # \u95f4\u63a5\u5f15\u7528\u53d8\u91cf\u503c\n<\/code><\/pre>\n<p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u7f16\u5199\u66f4\u5065\u58ee\u3001\u53ef\u7ef4\u62a4\u7684Shell\u811a\u672c\uff0c\u6709\u6548\u7ba1\u7406\u7cfb\u7edf\u8d44\u6e90\u5e76\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n<h3 id=\"\u7b2c28\u7ae0-\u8fd0\u884c\u7ea7\u522b\u811a\u672c\u603b\u7ed3\">\u7b2c28\u7ae0 \u8fd0\u884c\u7ea7\u522b\u811a\u672c\u603b\u7ed3<\/h3>\n<h3 id=\"1-\u8fd0\u884c\u7ea7\u522b\u6982\u8ff0\"><strong>1. \u8fd0\u884c\u7ea7\u522b\u6982\u8ff0<\/strong><\/h3>\n<p>\u2022 \u5b9a\u4e49\uff1a\u8fd0\u884c\u7ea7\u522b\uff08Runlevel\uff09\u5b9a\u4e49\u4e86\u7cfb\u7edf\u7684\u64cd\u4f5c\u72b6\u6001\uff0c\u6bcf\u4e2a\u7ea7\u522b\u5bf9\u5e94\u7279\u5b9a\u7684\u670d\u52a1\u96c6\u5408\u3002Linux\u7cfb\u7edf\u57fa\u4e8eSystem V\u521d\u59cb\u5316\u673a\u5236\uff0c\u4f7f\u7528\u8fd0\u884c\u7ea7\u522b\u7ba1\u7406\u542f\u52a8\u548c\u505c\u6b62\u670d\u52a1\u7684\u987a\u5e8f\u3002<\/p>\n<p>\u2022 \u7ea7\u522b\u5212\u5206\uff1a<\/p>\n<p>\u2022 0\uff1a\u5173\u673a\u3002<\/p>\n<p>\u2022 1\uff08\u5355\u7528\u6237\u6a21\u5f0f\uff09\uff1a\u4ec5root\u53ef\u767b\u5f55\uff0c\u7528\u4e8e\u7cfb\u7edf\u7ef4\u62a4\u3002<\/p>\n<p>\u2022 2\uff1a\u591a\u7528\u6237\u6a21\u5f0f\uff08\u90e8\u5206\u7f51\u7edc\u670d\u52a1\u4e0d\u542f\u52a8\uff0c\u67d0\u4e9b\u7cfb\u7edf\u4f5c\u4e3a\u9ed8\u8ba4\uff09\u3002<\/p>\n<p>\u2022 3\uff1a\u5b8c\u6574\u591a\u7528\u6237\u6a21\u5f0f\uff08\u5e26\u7f51\u7edc\u670d\u52a1\uff09\u3002<\/p>\n<p>\u2022 4\uff1a\u7528\u6237\u81ea\u5b9a\u4e49\uff08\u901a\u5e38\u672a\u4f7f\u7528\uff09\u3002<\/p>\n<p>\u2022 5\uff1a\u56fe\u5f62\u754c\u9762\u6a21\u5f0f\uff08\u67d0\u4e9b\u7cfb\u7edf\uff09\u6216\u7ef4\u62a4\u6a21\u5f0f\u3002<\/p>\n<p>\u2022 6\uff1a\u91cd\u542f\u3002<\/p>\n<h3 id=\"2-\u8fd0\u884c\u7ea7\u522b\u76ee\u5f55\u7ed3\u6784\"><strong>2. \u8fd0\u884c\u7ea7\u522b\u76ee\u5f55\u7ed3\u6784<\/strong><\/h3>\n<p>\u2022 \u76ee\u5f55\u4f4d\u7f6e\uff1a<code>\/etc\/rcN.d<\/code>\u00a0\u6216\u00a0<code>\/etc\/rc.d\/rcN.d<\/code>\uff08N\u4e3a\u8fd0\u884c\u7ea7\u522b\u6570\u5b57\uff09\u3002<\/p>\n<p>\u2022 \u76ee\u5f55\u5185\u5bb9\uff1a\u5305\u542b\u4ee5\u00a0<code>S<\/code>\uff08\u542f\u52a8\uff09\u6216\u00a0<code>K<\/code>\uff08\u505c\u6b62\uff09\u5f00\u5934\u7684\u7b26\u53f7\u94fe\u63a5\uff0c\u6307\u5411\u00a0<code>\/etc\/init.d<\/code>\u00a0\u6216\u00a0<code>\/etc\/rc.d\/init.d<\/code>\u00a0\u4e2d\u7684\u5b9e\u9645\u811a\u672c\u3002<\/p>\n<p>\u2022 \u547d\u540d\u89c4\u5219\uff1a<\/p>\n<p>\u2022\u00a0<code>SnnName<\/code>\uff1a\u542f\u52a8\u811a\u672c\uff0c<code>nn<\/code>\u4e3a\u4e24\u4f4d\u6570\u5b57\uff08\u5982<code>S20network<\/code>\uff09\uff0c\u6309\u5347\u5e8f\u6267\u884c\u3002<\/p>\n<p>\u2022\u00a0<code>KnnName<\/code>\uff1a\u505c\u6b62\u811a\u672c\uff0c\u6309\u964d\u5e8f\u6267\u884c\uff08\u5982<code>K50httpd<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u4f8b\u5982\uff1a<code>S35rc.audit<\/code>\u00a0\u8868\u793a\u5728\u8fd0\u884c\u7ea7\u522b\u542f\u52a8\u987a\u5e8f\u4e3a35\u7684\u5ba1\u6838\u670d\u52a1\u3002<\/p>\n<h3 id=\"3-\u521b\u5efa\u8fd0\u884c\u7ea7\u522b\u811a\u672c\"><strong>3. \u521b\u5efa\u8fd0\u884c\u7ea7\u522b\u811a\u672c<\/strong><\/h3>\n<p>\u2022 \u811a\u672c\u8981\u6c42\uff1a<\/p>\n<ol>\n<li>\u5fc5\u987b\u63a5\u53d7\u00a0<code>start<\/code>\u00a0\u548c\u00a0<code>stop<\/code>\u00a0\u53c2\u6570\uff0c\u53ef\u9009\u00a0<code>restart<\/code>\u3001<code>status<\/code>\u3002<\/li>\n<li>\u811a\u672c\u8def\u5f84\u901a\u5e38\u4f4d\u4e8e\u00a0<code>\/etc\/init.d<\/code>\u00a0\u6216\u00a0<code>\/etc\/rc.d\/init.d<\/code>\u3002 \u2022 \u793a\u4f8b\u811a\u672c\uff1a<\/li>\n<\/ol>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">#!\/bin\/sh\ncase \"$1\" in\n  start)\n    \/apps\/audit\/audlcp -a -p 12\n    touch \/var\/lock\/subsys\/rc.audit ;;\n  stop)\n    \/apps\/audit\/audidown -k0\n    rm -f \/var\/lock\/subsys\/rc.audit ;;\n  restart)\n    $0 stop; $0 start ;;\n  *) echo \"Usage: $0 {start|stop|restart}\"; exit 1 ;;\nesac\nexit 0\n<\/code><\/pre>\n<p>\u2022 \u90e8\u7f72\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<p>\u6d4b\u8bd5\u811a\u672c\uff1a\u786e\u4fdd\u624b\u52a8\u6267\u884c\u00a0<code>start\/stop<\/code>\u00a0\u6b63\u786e\u3002<\/p>\n<\/li>\n<li>\n<p>\u5c06\u811a\u672c\u653e\u5165\u00a0<code>\/etc\/init.d<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u76ee\u6807\u8fd0\u884c\u7ea7\u522b\u7684<\/p>\n<pre><code>rcN.d\n<\/code><\/pre>\n<p>\u76ee\u5f55\u521b\u5efa\u7b26\u53f7\u94fe\u63a5\uff1a<\/p>\n<p>bash<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">ln -s ..\/init.d\/rc.audit \/etc\/rc3.d\/S35rc.audit  # \u542f\u52a8\nln -s ..\/init.d\/rc.audit \/etc\/rc6.d\/K35rc.audit  # \u505c\u6b62\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3 id=\"4-inittab-\u6587\u4ef6\"><strong>4. inittab \u6587\u4ef6<\/strong><\/h3>\n<p>\u2022 \u4f5c\u7528\uff1a<code>\/etc\/inittab<\/code>\u00a0\u662f\u00a0<code>init<\/code>\u00a0\u8fdb\u7a0b\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u5b9a\u4e49\u9ed8\u8ba4\u8fd0\u884c\u7ea7\u522b\u548c\u7cfb\u7edf\u521d\u59cb\u5316\u6d41\u7a0b\u3002<\/p>\n<p>\u2022 \u6761\u76ee\u683c\u5f0f\uff1a<code>id:runlevels:action:process<\/code>\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```ini\nid:3:initdefault:        # \u9ed8\u8ba4\u8fd0\u884c\u7ea7\u522b3\nsi::sysinit:\/etc\/rc.d\/rc.sysinit  # \u7cfb\u7edf\u521d\u59cb\u5316\u811a\u672c\n15:5:wait:\/etc\/rc.d\/rc 5          # \u8fd0\u884c\u7ea7\u522b5\u6267\u884c\/etc\/rc.d\/rc5.d\n```\n<\/code><\/pre>\n<p>\u2022 \u7279\u6b8a\u64cd\u4f5c\uff1a<\/p>\n<p>\u2022\u00a0<code>respawn<\/code>\uff1a\u8fdb\u7a0b\u7ec8\u6b62\u540e\u81ea\u52a8\u91cd\u542f\uff08\u5982\u7ec8\u7aef\u670d\u52a1\uff09\u3002<\/p>\n<p>\u2022\u00a0<code>once<\/code>\uff1a\u8fdb\u7a0b\u8fd0\u884c\u4e00\u6b21\u540e\u4e0d\u91cd\u542f\u3002<\/p>\n<h3 id=\"5-\u5176\u4ed6\u542f\u52a8\u65b9\u6cd5\"><strong>5. \u5176\u4ed6\u542f\u52a8\u65b9\u6cd5<\/strong><\/h3>\n<p>\u2022 rc.local\uff1a\u4f4d\u4e8e\u00a0<code>\/etc\/rc.local<\/code>\uff0c\u7cfb\u7edf\u521d\u59cb\u5316\u540e\u6267\u884c\u7684\u81ea\u5b9a\u4e49\u547d\u4ee4\u3002<\/p>\n<p>\u2022 inittab \u76f4\u63a5\u8c03\u7528\uff1a\u6dfb\u52a0\u6761\u76ee\u5230\u00a0<code>\/etc\/inittab<\/code>\uff0c\u5982\uff1a<\/p>\n<p>ini<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-ini\" lang=\"ini\">rc.diskchecker:3:once:\/usr\/local\/etc\/rc.diskchecker &gt;\/dev\/console 2&gt;&amp;1\n<\/code><\/pre>\n<h3 id=\"6-\u5173\u952e\u547d\u4ee4\u4e0e\u5de5\u5177\"><strong>6. \u5173\u952e\u547d\u4ee4\u4e0e\u5de5\u5177<\/strong><\/h3>\n<p>\u2022 \u67e5\u770b\u8fd0\u884c\u7ea7\u522b\uff1a<\/p>\n<p>\u2022\u00a0<code>who -r<\/code>\u00a0\u6216\u00a0<code>runlevel<\/code>\uff08\u663e\u793a\u5f53\u524d\u548c\u524d\u4e00\u4e2a\u8fd0\u884c\u7ea7\u522b\uff09\u3002<\/p>\n<p>\u2022 \u5207\u6362\u8fd0\u884c\u7ea7\u522b\uff1a<\/p>\n<p>\u2022\u00a0<code>init N<\/code>\uff08N\u4e3a\u76ee\u6807\u7ea7\u522b\uff0c\u5982\u00a0<code>init 6<\/code>\u00a0\u91cd\u542f\uff09\u3002<\/p>\n<h3 id=\"7-\u6ce8\u610f\u4e8b\u9879-2\"><strong>7. \u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n<p>\u2022 \u5907\u4efd\u914d\u7f6e\uff1a\u4fee\u6539\u00a0<code>\/etc\/inittab<\/code>\u00a0\u524d\u9700\u5907\u4efd\uff0c\u9519\u8bef\u53ef\u80fd\u5bfc\u81f4\u7cfb\u7edf\u65e0\u6cd5\u542f\u52a8\u3002<\/p>\n<p>\u2022 \u9501\u6587\u4ef6\u7ba1\u7406\uff1a\u67d0\u4e9b\u7cfb\u7edf\u901a\u8fc7\u00a0<code>\/var\/lock\/subsys<\/code>\u00a0\u8ddf\u8e2a\u670d\u52a1\u72b6\u6001\u3002<\/p>\n<p>\u2022 \u987a\u5e8f\u51b2\u7a81\uff1a\u786e\u4fdd\u811a\u672c\u7684\u6570\u5b57\u5e8f\u53f7\u4e0d\u4e0e\u73b0\u6709\u670d\u52a1\u51b2\u7a81\uff0c\u907f\u514d\u4f9d\u8d56\u95ee\u9898\u3002<\/p>\n<h3 id=\"8-\u8de8\u53d1\u884c\u7248\u5dee\u5f02\"><strong>8. \u8de8\u53d1\u884c\u7248\u5dee\u5f02<\/strong><\/h3>\n<p>\u2022 \u8def\u5f84\u5dee\u5f02\uff1a<\/p>\n<p>\u2022 Red Hat\/CentOS\uff1a<code>\/etc\/rc.d\/init.d<\/code>\u00a0\u548c\u00a0<code>\/etc\/rc.d\/rcN.d<\/code>\u3002<\/p>\n<p>\u2022 Debian\/Ubuntu\uff1a<code>\/etc\/init.d<\/code>\u00a0\u548c\u00a0<code>\/etc\/rcN.d<\/code>\u3002<\/p>\n<p>\u2022 \u5de5\u5177\u5dee\u5f02\uff1a\u90e8\u5206\u7cfb\u7edf\u4f7f\u7528\u00a0<code>systemd<\/code>\u00a0\u66ff\u4ee3\u4f20\u7edfSysV init\uff08\u9700\u9002\u914d\uff09\u3002<\/p>\n<h3 id=\"9-\u8c03\u8bd5\u4e0e\u9a8c\u8bc1\"><strong>9. \u8c03\u8bd5\u4e0e\u9a8c\u8bc1<\/strong><\/h3>\n<p>\u2022 \u624b\u52a8\u6d4b\u8bd5\uff1a\u76f4\u63a5\u8fd0\u884c\u811a\u672c\u68c0\u67e5\u8f93\u51fa\u548c\u65e5\u5fd7\u3002<\/p>\n<p>\u2022 \u67e5\u770b\u65e5\u5fd7\uff1a<code>\/var\/log\/boot.log<\/code>\u00a0\u6216\u00a0<code>dmesg<\/code>\u00a0\u6392\u67e5\u542f\u52a8\u95ee\u9898\u3002<\/p>\n<p>\u901a\u8fc7\u638c\u63e1\u8fd0\u884c\u7ea7\u522b\u811a\u672c\u7684\u914d\u7f6e\uff0c\u53ef\u7075\u6d3b\u63a7\u5236\u7cfb\u7edf\u670d\u52a1\u7684\u542f\u52a8\u4e0e\u505c\u6b62\uff0c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u9700\u6c42\u3002\u7b2c29\u7ae0 CGI\u811a\u672c\u603b\u7ed3<\/p>\n<h2 id=\"29\u7ae0-cgi\u57fa\u7840\u4e0e\u914d\u7f6e\"><strong>29\u7ae0 CGI\u57fa\u7840\u4e0e\u914d\u7f6e<\/strong><\/h2>\n<p>\u2022 CGI\u4f5c\u7528\uff1a\u4f5c\u4e3aWeb\u670d\u52a1\u5668\u4e0e\u811a\u672c\u7684\u63a5\u53e3\uff0c\u652f\u6301\u52a8\u6001\u5185\u5bb9\u751f\u6210\uff0c\u53ef\u7528Shell\u7b49\u8bed\u8a00\u5b9e\u73b0\u3002<\/p>\n<p>\u2022 \u73af\u5883\u914d\u7f6e\uff1a<\/p>\n<p>\u2022 Web\u670d\u52a1\u5668\uff08\u5982Apache\uff09\u9700\u542f\u7528CGI\uff0c\u4fee\u6539<code>ScriptAlias<\/code>\u914d\u7f6e\u6307\u5411<code>cgi-bin<\/code>\u76ee\u5f55\u3002<\/p>\n<p>\u2022 \u811a\u672c\u9700\u6709\u6267\u884c\u6743\u9650\uff08<code>chmod 755<\/code>\uff09\uff0c\u6587\u4ef6\u540e\u7f00\u901a\u5e38\u4e3a<code>.cgi<\/code>\u3002<\/p>\n<p>\u2022 \u65e5\u5fd7\u6587\u4ef6\uff08\u5982Apache\u7684<code>error_log<\/code>\uff09\u7528\u4e8e\u8c03\u8bd5\u3002<\/p>\n<h3 id=\"2-\u57fa\u672ccgi\u811a\u672c\"><strong>2. \u57fa\u672cCGI\u811a\u672c<\/strong><\/h3>\n<p>\u2022 \u7ed3\u6784\u8981\u6c42\uff1a<\/p>\n<p>\u2022 \u9996\u884c\u6307\u5b9a\u89e3\u91ca\u5668\uff08\u5982<code>#!\/bin\/sh<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u8f93\u51fa\u5fc5\u987b\u5305\u542bMIME\u7c7b\u578b\u5934\uff08<code>Content-type: text\/html<\/code>\uff09\u548c\u7a7a\u884c\u3002<\/p>\n<p>\u2022 \u4f7f\u7528HTML\u6807\u7b7e\u6784\u5efa\u9875\u9762\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\u811a\u672c\uff1a<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># firstpage.cgi\necho \"Content-type: text\/html\"\necho \"\"\necho \"&lt;HTML&gt;&lt;H1&gt;Hello CGI!&lt;\/H1&gt;&lt;\/HTML&gt;\"\n<\/code><\/pre>\n<p>\u2022 \u8bbf\u95ee\u65b9\u5f0f\uff1a<code>http:\/\/server\/cgi-bin\/firstpage.cgi<\/code>\u3002<\/p>\n<h3 id=\"3-\u52a8\u6001\u5185\u5bb9\u751f\u6210\"><strong>3. \u52a8\u6001\u5185\u5bb9\u751f\u6210<\/strong><\/h3>\n<p>\u2022 \u5d4c\u5165Shell\u547d\u4ee4\uff1a\u901a\u8fc7\u53cd\u5f15\u53f7\u6216\u53d8\u91cf\u83b7\u53d6\u7cfb\u7edf\u4fe1\u606f\uff08\u5982<code>date<\/code>\u3001<code>who<\/code>\uff09\u3002<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\"># pagetwo.cgi\nUSERS=$(who | wc -l)\necho \"\u5f53\u524d\u7528\u6237\u6570\uff1a$USERS\"\n<\/code><\/pre>\n<p>\u2022 \u9884\u683c\u5f0f\u5316\u6587\u672c\uff1a\u4f7f\u7528<code>&lt;PRE&gt;<\/code>\u6807\u7b7e\u4fdd\u7559\u7a7a\u683c\u548c\u683c\u5f0f\uff0c\u9002\u5408\u5c55\u793a\u547d\u4ee4\u8f93\u51fa\uff08\u5982<code>df<\/code>\uff09\u3002<\/p>\n<h3 id=\"4-\u670d\u52a1\u5668\u7aef\u5185\u5d4cssi\"><strong>4. \u670d\u52a1\u5668\u7aef\u5185\u5d4c\uff08SSI\uff09<\/strong><\/h3>\n<p>\u2022 \u542f\u7528SSI\uff1a\u4fee\u6539Apache\u914d\u7f6e\uff08<code>AddType text\/html .shtml<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u5185\u5d4c\u811a\u672c\uff1a\u5728<code>.shtml<\/code>\u6587\u4ef6\u4e2d\u4f7f\u7528<code>&lt;!--#exec cgi=\"\u811a\u672c\u8def\u5f84\"--&gt;<\/code>\u3002<\/p>\n<p>\u2022 \u8bbf\u95ee\u8ba1\u6570\u5668\u793a\u4f8b\uff1a<\/p>\n<p>\u2022\u00a0<code>hitcount.cgi<\/code>\u66f4\u65b0\u8ba1\u6570\u5668\u6587\u4ef6\u3002<\/p>\n<p>\u2022 SSI\u9875\u9762\u663e\u793a\u8ba1\u6570\u548c\u6700\u540e\u4fee\u6539\u65f6\u95f4\uff1a<\/p>\n<pre><code>```html\n&lt;!-- main.shtml --&gt;\n\u8bbf\u95ee\u6b21\u6570\uff1a&lt;!--#exec cgi=\"\/cgi-bin\/hitcount.cgi\"--&gt;\n\u6700\u540e\u4fee\u6539\uff1a&lt;!--#echo var=\"LAST_MODIFIED\"--&gt;\n```\n<\/code><\/pre>\n<h3 id=\"5-get\u4e0epost\u65b9\u6cd5\"><strong>5. GET\u4e0ePOST\u65b9\u6cd5<\/strong><\/h3>\n<p>\u2022 GET\u65b9\u6cd5\uff1a<\/p>\n<p>\u2022 \u53c2\u6570\u9644\u52a0\u5728URL\uff08<code>QUERY_STRING<\/code>\u73af\u5883\u53d8\u91cf\uff09\u3002<\/p>\n<p>\u2022 \u9002\u7528\u4e8e\u7b80\u5355\u6570\u636e\uff0c\u6709\u957f\u5ea6\u9650\u5236\u4e14\u53ef\u89c1\u3002<\/p>\n<p>\u2022 POST\u65b9\u6cd5\uff1a<\/p>\n<p>\u2022 \u6570\u636e\u901a\u8fc7\u6807\u51c6\u8f93\u5165\u4f20\u9012\uff0c\u66f4\u5b89\u5168\uff0c\u9002\u5408\u654f\u611f\u4fe1\u606f\u3002<\/p>\n<p>\u2022 \u4f7f\u7528<code>CONTENT_LENGTH<\/code>\u83b7\u53d6\u6570\u636e\u957f\u5ea6\u3002<\/p>\n<p>\u2022 \u53c2\u6570\u89e3\u7801\uff1a<\/p>\n<p>\u2022 \u66ff\u6362<code>+<\/code>\u4e3a\u7a7a\u683c\uff0c<code>%xx<\/code>\u4e3a\u5bf9\u5e94ASCII\u5b57\u7b26\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\u4ee3\u7801\u7247\u6bb5\uff1a<\/p>\n<pre><code>```sh\n# \u5904\u7406POST\u6570\u636e\nif [ \"$REQUEST_METHOD\" = \"POST\" ]; then\n  QUERY_STRING=$(cat -)\nfi\n```\n<\/code><\/pre>\n<h3 id=\"6-\u8868\u5355\u5904\u7406\u4e0e\u4ea4\u4e92\"><strong>6. \u8868\u5355\u5904\u7406\u4e0e\u4ea4\u4e92<\/strong><\/h3>\n<p>\u2022 \u8868\u5355\u6784\u5efa\uff1a<\/p>\n<p>\u2022 \u4f7f\u7528<code>&lt;FORM&gt;<\/code>\u6807\u7b7e\uff0c\u6307\u5b9a<code>action<\/code>\u548c<code>method<\/code>\u3002<\/p>\n<p>\u2022 \u5143\u7d20\u5305\u62ec\u8f93\u5165\u6846\u3001\u4e0b\u62c9\u83dc\u5355\u3001\u5355\u9009\u6846\u7b49\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\u8868\u5355\u5904\u7406\uff1a<\/p>\n<p>\u2022\u00a0<code>booka.cgi<\/code>\u751f\u6210\u8868\u5355\uff0c<code>conv.cgi<\/code>\u89e3\u7801\u53c2\u6570\u3002<\/p>\n<p>\u2022 \u52a8\u6001\u751f\u6210\u62a5\u8868\uff08<code>gifts_result.cgi<\/code>\u6839\u636e\u7528\u6237\u9009\u62e9\u8ba1\u7b97\u9500\u552e\u989d\uff09\u3002<\/p>\n<h3 id=\"7-\u52a8\u6001\u586b\u5145\u4e0e\u81ea\u52a8\u5237\u65b0\"><strong>7. \u52a8\u6001\u586b\u5145\u4e0e\u81ea\u52a8\u5237\u65b0<\/strong><\/h3>\n<p>\u2022 \u586b\u5145\u4e0b\u62c9\u5217\u8868\uff1a\u4ece\u6587\u4ef6\u8bfb\u53d6\u9009\u9879\uff0c\u5faa\u73af\u751f\u6210<code>&lt;OPTION&gt;<\/code>\u3002<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">while read LINE; do\n  echo \"&lt;OPTION&gt;$LINE\"\ndone &lt; list.txt\n<\/code><\/pre>\n<p>\u2022 \u81ea\u52a8\u5237\u65b0\u9875\u9762\uff1a\u901a\u8fc7<code>&lt;meta http-equiv=\"Refresh\"&gt;<\/code>\u5b9a\u65f6\u91cd\u8f7d\u3002<\/p>\n<p>html<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-html\" lang=\"html\">&lt;meta http-equiv=\"Refresh\" content=\"60; URL=\u811a\u672c\u5730\u5740\"&gt;\n<\/code><\/pre>\n<h3 id=\"8-\u8c03\u8bd5\u4e0e\u5b89\u5168\"><strong>8. \u8c03\u8bd5\u4e0e\u5b89\u5168<\/strong><\/h3>\n<p>\u2022 \u5e38\u89c1\u9519\u8bef\uff1a<\/p>\n<p>\u2022 \u6743\u9650\u95ee\u9898\uff1a\u786e\u4fdd\u811a\u672c\u548c\u8ba1\u6570\u5668\u6587\u4ef6\u53ef\u8bfb\u5199\uff08\u5982<code>chmod 666 counter<\/code>\uff09\u3002<\/p>\n<p>\u2022 \u7f3a\u5c11MIME\u5934\u6216\u7a7a\u884c\u5bfc\u81f4\u9875\u9762\u65e0\u6cd5\u6e32\u67d3\u3002<\/p>\n<p>\u2022 \u5b89\u5168\u5efa\u8bae\uff1a<\/p>\n<p>\u2022 \u907f\u514d\u4f7f\u7528root\u8fd0\u884cWeb\u670d\u52a1\u5668\uff0c\u9650\u5236<code>nobody<\/code>\u7528\u6237\u6743\u9650\u3002<\/p>\n<p>\u2022 \u8fc7\u6ee4\u7528\u6237\u8f93\u5165\uff0c\u9632\u6b62\u547d\u4ee4\u6ce8\u5165\u3002<\/p>\n<p><strong>9. \u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/strong>\u00a0\u2022 \u6587\u4ef6\u7cfb\u7edf\u76d1\u63a7\uff1a<code>dfspace.cgi<\/code>\u5b9a\u65f6\u5237\u65b0\u663e\u793a\u78c1\u76d8\u4f7f\u7528\u60c5\u51b5\u3002<\/p>\n<p>sh<\/p>\n<p>\u590d\u5236<\/p>\n<pre><code class=\"language-shell\" lang=\"shell\">df | awk '{print $5, $6}' | while read percent mount; do\n  echo \"&lt;TR&gt;&lt;TD&gt;$percent&lt;\/TD&gt;&lt;TD&gt;$mount&lt;\/TD&gt;&lt;\/TR&gt;\"\ndone\n<\/code><\/pre>\n<h3 id=\"\u603b\u7ed3-2\"><strong>\u603b\u7ed3<\/strong><\/h3>\n<p>\u672c\u7ae0\u8be6\u7ec6\u8bb2\u89e3\u4e86\u5982\u4f55\u901a\u8fc7Shell\u7f16\u5199CGI\u811a\u672c\uff0c\u6db5\u76d6\u9759\u6001\u9875\u9762\u751f\u6210\u3001\u52a8\u6001\u5185\u5bb9\u5d4c\u5165\u3001\u8868\u5355\u4ea4\u4e92\u3001\u53c2\u6570\u89e3\u6790\u53ca\u5b89\u5168\u5b9e\u8df5\u3002\u901a\u8fc7\u7ed3\u5408SSI\u548cHTTP\u65b9\u6cd5\uff08GET\/POST\uff09\uff0c\u5f00\u53d1\u8005\u80fd\u521b\u5efa\u529f\u80fd\u4e30\u5bcc\u7684Web\u5e94\u7528\uff0c\u5982\u8bbf\u95ee\u8ba1\u6570\u5668\u3001\u52a8\u6001\u62a5\u8868\u548c\u5b9e\u65f6\u76d1\u63a7\u9875\u9762\u3002\u5173\u952e\u70b9\u5305\u62ec\u6b63\u786e\u914d\u7f6e\u670d\u52a1\u5668\u73af\u5883\u3001\u9075\u5faaCGI\u8f93\u51fa\u89c4\u8303\u3001\u5408\u7406\u5904\u7406\u7528\u6237\u8f93\u5165\uff0c\u4ee5\u53ca\u5229\u7528Shell\u811a\u672c\u7075\u6d3b\u751f\u6210HTML\u5185\u5bb9\u3002<\/p>\n<h1 id=\"\u5e38\u7528-shell-\u547d\u4ee4\u603b\u7ed3\">\u5e38\u7528 Shell \u547d\u4ee4\u603b\u7ed3<\/h1>\n<h3 id=\"1-\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\"><strong>1. \u6587\u4ef6\u8def\u5f84\u64cd\u4f5c<\/strong><\/h3>\n<p>\u2022\u00a0<code>basename<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u4ece\u8def\u5f84\u4e2d\u63d0\u53d6\u6587\u4ef6\u540d\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ basename \/home\/dave\/myscript  # \u8f93\u51fa\uff1amyscript\n```  \n<\/code><\/pre>\n<p>\u2022 \u811a\u672c\u5e94\u7528\uff1a<code>basename $0<\/code>\u00a0\u83b7\u53d6\u5f53\u524d\u811a\u672c\u540d\u3002<\/p>\n<p>\u2022\u00a0<code>dirname<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u4ece\u8def\u5f84\u4e2d\u63d0\u53d6\u76ee\u5f55\u90e8\u5206\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ dirname \/home\/dave\/myfile  # \u8f93\u51fa\uff1a\/home\/dave\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"2-\u6587\u4ef6\u5185\u5bb9\u67e5\u770b\u4e0e\u64cd\u4f5c\"><strong>2. \u6587\u4ef6\u5185\u5bb9\u67e5\u770b\u4e0e\u64cd\u4f5c<\/strong><\/h3>\n<p>\u2022\u00a0<code>cat<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u663e\u793a\u3001\u5408\u5e76\u6587\u4ef6\u5185\u5bb9\u6216\u901a\u8fc7\u7ba1\u9053\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-v`\uff1a\u663e\u793a\u63a7\u5236\u5b57\u7b26\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ cat myfile                # \u663e\u793a\u6587\u4ef6\u5185\u5bb9\n$ cat file1 file2 &gt;&gt; hold   # \u5408\u5e76\u6587\u4ef6\u5230 hold\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>head<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u663e\u793a\u6587\u4ef6\u524d N \u884c\uff08\u9ed8\u8ba4 10 \u884c\uff09\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-n &lt;\u6570\u5b57&gt;`\uff1a\u6307\u5b9a\u884c\u6570\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ head -30 logfile | more   # \u5206\u9875\u663e\u793a\u524d 30 \u884c\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>more<\/code>\u00a0\/\u00a0<code>pg<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u5206\u9875\u663e\u793a\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-c`\uff1a\u4e0d\u6eda\u5c4f\uff0c\u8986\u76d6\u6362\u9875\u3002  \n\n\u25e6 `-d`\uff1a\u663e\u793a\u5206\u9875\u63d0\u793a\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ more \/etc\/passwd          # \u5206\u9875\u67e5\u770b\u7cfb\u7edf\u8d26\u6237\u6587\u4ef6\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>nl<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u4e3a\u6587\u4ef6\u5185\u5bb9\u6dfb\u52a0\u884c\u53f7\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-i &lt;\u6570\u5b57&gt;`\uff1a\u884c\u53f7\u589e\u91cf\uff08\u9ed8\u8ba4 1\uff09\u3002  \n\n\u25e6 `-p`\uff1a\u65b0\u9875\u4e0d\u91cd\u7f6e\u884c\u53f7\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ nl myscript &gt; hold        # \u5e26\u884c\u53f7\u8f93\u51fa\u5230\u6587\u4ef6\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>strings<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u67e5\u770b\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\u7684\u53ef\u8bfb\u6587\u672c\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ strings \/bin\/ls           # \u663e\u793a ls \u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\u7684\u5b57\u7b26\u4e32\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"3-\u6587\u4ef6\u538b\u7f29\u4e0e\u89e3\u538b\"><strong>3. \u6587\u4ef6\u538b\u7f29\u4e0e\u89e3\u538b<\/strong><\/h3>\n<p>\u2022\u00a0<code>compress<\/code>\u00a0\/\u00a0<code>uncompress<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u538b\u7f29\u6587\u4ef6\uff08\u751f\u6210\u00a0<code>.Z<\/code>\u00a0\u540e\u7f00\uff09\u6216\u89e3\u538b\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-v`\uff1a\u663e\u793a\u538b\u7f29\u8fdb\u5ea6\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ compress myfile           # \u538b\u7f29\u4e3a myfile.Z\n$ uncompress myfile.Z       # \u89e3\u538b\uff08\u65e0\u9700\u6307\u5b9a\u540e\u7f00\uff09\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"4-\u6587\u4ef6\u4e0e\u76ee\u5f55\u7ba1\u7406\"><strong>4. \u6587\u4ef6\u4e0e\u76ee\u5f55\u7ba1\u7406<\/strong><\/h3>\n<p>\u2022\u00a0<code>cp<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u590d\u5236\u6587\u4ef6\u6216\u76ee\u5f55\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-i`\uff1a\u8986\u76d6\u524d\u786e\u8ba4\u3002  \n\n\u25e6 `-p`\uff1a\u4fdd\u7559\u6743\u9650\u548c\u65f6\u95f4\u6233\u3002  \n\n\u25e6 `-r`\uff1a\u9012\u5f52\u590d\u5236\u76ee\u5f55\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ cp -r \/logs \/hold\/logs    # \u9012\u5f52\u590d\u5236\u76ee\u5f55\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>rm<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u5220\u9664\u6587\u4ef6\u6216\u76ee\u5f55\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-i`\uff1a\u5220\u9664\u524d\u786e\u8ba4\u3002  \n\n\u25e6 `-r`\uff1a\u9012\u5f52\u5220\u9664\u76ee\u5f55\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ rm -r \/var\/spool\/tmp      # \u5220\u9664\u76ee\u5f55\u53ca\u5176\u5185\u5bb9\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>mkdir<\/code>\u00a0\/\u00a0<code>rmdir<\/code><\/p>\n<p>\u2022\u00a0<code>mkdir<\/code>\uff1a\u521b\u5efa\u76ee\u5f55\u3002<\/p>\n<pre><code>\u25e6 \u9009\u9879\uff1a`-m` \u8bbe\u7f6e\u6743\u9650\u3002  \n\n```bash\n$ mkdir -m 755 HOLD_AREA    # \u521b\u5efa\u5e76\u8bbe\u7f6e\u6743\u9650\u4e3a 755\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>rmdir<\/code>\uff1a\u5220\u9664\u7a7a\u76ee\u5f55\u3002<\/p>\n<pre><code>\u25e6 \u9009\u9879\uff1a`-p` \u9012\u5f52\u5220\u9664\u7a7a\u76ee\u5f55\u3002  \n\n```bash\n$ rmdir \/var\/spool\/lp_HP    # \u5220\u9664\u7a7a\u76ee\u5f55\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>touch<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u521b\u5efa\u7a7a\u6587\u4ef6\u6216\u66f4\u65b0\u6587\u4ef6\u65f6\u95f4\u6233\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-t MMDDhhmm`\uff1a\u6307\u5b9a\u65f6\u95f4\u6233\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ touch -t 06100930 myfile2 # \u521b\u5efa\u5e26\u6307\u5b9a\u65f6\u95f4\u6233\u7684\u6587\u4ef6\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"5-\u6587\u4ef6\u6bd4\u8f83\u4e0e\u7edf\u8ba1\"><strong>5. \u6587\u4ef6\u6bd4\u8f83\u4e0e\u7edf\u8ba1<\/strong><\/h3>\n<p>\u2022\u00a0<code>diff<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u6bd4\u8f83\u6587\u4ef6\u5dee\u5f02\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-c`\uff1a\u6807\u51c6\u683c\u5f0f\u8f93\u51fa\u5dee\u5f02\u3002  \n\n\u25e6 `-I`\uff1a\u5ffd\u7565\u5927\u5c0f\u5199\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ diff -c file1 file2       # \u663e\u793a\u683c\u5f0f\u5316\u5dee\u5f02\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>dircmp<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u6bd4\u8f83\u4e24\u4e2a\u76ee\u5f55\u5185\u5bb9\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-s`\uff1a\u4ec5\u663e\u793a\u4e0d\u540c\u6587\u4ef6\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ dircmp dir1 dir2          # \u5217\u51fa\u76ee\u5f55\u5dee\u5f02\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>wc<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u7edf\u8ba1\u884c\u6570\u3001\u5355\u8bcd\u6570\u3001\u5b57\u7b26\u6570\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-l`\uff1a\u4ec5\u7edf\u8ba1\u884c\u6570\u3002  \n\n\u25e6 `-w`\uff1a\u4ec5\u7edf\u8ba1\u5355\u8bcd\u6570\u3002  \n\n\u25e6 `-c`\uff1a\u4ec5\u7edf\u8ba1\u5b57\u7b26\u6570\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ who | wc -l               # \u7edf\u8ba1\u5f53\u524d\u767b\u5f55\u7528\u6237\u6570\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"6-\u7cfb\u7edf\u4fe1\u606f\u4e0e\u8fdb\u7a0b\u7ba1\u7406\"><strong>6. \u7cfb\u7edf\u4fe1\u606f\u4e0e\u8fdb\u7a0b\u7ba1\u7406<\/strong><\/h3>\n<p>\u2022\u00a0<code>fuser<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u663e\u793a\u6216\u7ec8\u6b62\u8bbf\u95ee\u6587\u4ef6\u7684\u8fdb\u7a0b\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-k`\uff1a\u7ec8\u6b62\u8fdb\u7a0b\u3002  \n\n\u25e6 `-u`\uff1a\u663e\u793a\u8fdb\u7a0b\u53ca\u7528\u6237\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ fuser -k \/dev\/hda5        # \u7ec8\u6b62\u8bbf\u95ee\u8bbe\u5907\u7684\u6240\u6709\u8fdb\u7a0b\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>uname<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u663e\u793a\u7cfb\u7edf\u4fe1\u606f\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-a`\uff1a\u663e\u793a\u5168\u90e8\u4fe1\u606f\uff08\u7cfb\u7edf\u540d\u3001\u7248\u672c\u7b49\uff09\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ uname -a                  # \u8f93\u51fa\uff1aLinux bumper...2.0.36\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>shutdown<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u5173\u95ed\u6216\u91cd\u542f\u7cfb\u7edf\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ shutdown -g60 -I6 -y      # 60 \u79d2\u540e\u5173\u673a\u5e76\u91cd\u542f\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"7-\u811a\u672c\u4e0e\u7528\u6237\u7ba1\u7406\"><strong>7. \u811a\u672c\u4e0e\u7528\u6237\u7ba1\u7406<\/strong><\/h3>\n<p>\u2022\u00a0<code>sleep<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u6682\u505c\u811a\u672c\u6267\u884c\u6307\u5b9a\u79d2\u6570\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\nsleep 10                    # \u7b49\u5f85 10 \u79d2\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>logname<\/code>\u00a0\/\u00a0<code>whoami<\/code><\/p>\n<p>\u2022\u00a0<code>logname<\/code>\uff1a\u663e\u793a\u5f53\u524d\u767b\u5f55\u7528\u6237\u540d\u3002<\/p>\n<p>\u2022\u00a0<code>whoami<\/code>\uff1a\u663e\u793a\u6267\u884c\u547d\u4ee4\u7684\u7528\u6237\u540d\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ logname                   # \u8f93\u51fa\uff1adave\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>script<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u8bb0\u5f55\u7ec8\u7aef\u4f1a\u8bdd\u5230\u6587\u4ef6\u3002<\/p>\n<p>\u2022 \u9009\u9879\uff1a<\/p>\n<pre><code>\u25e6 `-a`\uff1a\u8ffd\u52a0\u5230\u6587\u4ef6\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ script mylogin            # \u5f00\u59cb\u8bb0\u5f55\u4f1a\u8bdd\u5230 mylogin\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"8-\u5176\u4ed6\u5b9e\u7528\u547d\u4ee4\"><strong>8. \u5176\u4ed6\u5b9e\u7528\u547d\u4ee4<\/strong><\/h3>\n<p>\u2022\u00a0<code>printf<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u683c\u5f0f\u5316\u8f93\u51fa\uff08\u7c7b\u4f3c C \u8bed\u8a00\uff09\u3002<\/p>\n<p>\u2022 \u683c\u5f0f\u7b26\uff1a<\/p>\n<pre><code>\u25e6 `%s`\uff08\u5b57\u7b26\u4e32\uff09\u3001`%d`\uff08\u6570\u5b57\uff09\u3001`\\n`\uff08\u6362\u884c\uff09\u3002  \n<\/code><\/pre>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ printf \"%-10s Stand-by\\n\" \"Server\"  # \u5de6\u5bf9\u9f50\u8f93\u51fa\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>whereis<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u67e5\u627e\u547d\u4ee4\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u3001\u624b\u518c\u8def\u5f84\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ whereis ls                # \u8f93\u51fa\uff1a\/bin\/ls \/usr\/share\/man...\n```\n<\/code><\/pre>\n<p>\u2022\u00a0<code>tty<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u663e\u793a\u5f53\u524d\u7ec8\u7aef\u8bbe\u5907\u8def\u5f84\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\n$ tty                       # \u8f93\u51fa\uff1a\/dev\/tty08\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"9-\u8fdb\u7a0b\u4e0e\u7b49\u5f85\"><strong>9. \u8fdb\u7a0b\u4e0e\u7b49\u5f85<\/strong><\/h3>\n<p>\u2022\u00a0<code>wait<\/code><\/p>\n<p>\u2022 \u7528\u9014\uff1a\u7b49\u5f85\u540e\u53f0\u8fdb\u7a0b\u7ed3\u675f\u3002<\/p>\n<p>\u2022 \u793a\u4f8b\uff1a<\/p>\n<pre><code>```bash\nwait 1299                   # \u7b49\u5f85\u8fdb\u7a0b ID 1299\nwait                        # \u7b49\u5f85\u6240\u6709\u540e\u53f0\u8fdb\u7a0b\n```\n<\/code><\/pre>\n<hr \/>\n<h3 id=\"\u603b\u7ed3\u8bf4\u660e\">\u603b\u7ed3\u8bf4\u660e<\/h3>\n<p>\u4ee5\u4e0a\u547d\u4ee4\u8986\u76d6\u4e86\u6587\u4ef6\u64cd\u4f5c\u3001\u7cfb\u7edf\u7ba1\u7406\u3001\u811a\u672c\u7f16\u5199\u7b49\u5e38\u89c1\u573a\u666f\u3002\u4f7f\u7528\u9700\u6ce8\u610f\uff1a \u2022 \u5371\u9669\u64cd\u4f5c\uff08\u5982\u00a0<code>rm -r<\/code>\u3001<code>fuser -k<\/code>\uff09\u9700\u8c28\u614e\u3002<\/p>\n<p>\u2022 \u811a\u672c\u6280\u5de7\uff1a<code>basename $0<\/code>\u00a0\u83b7\u53d6\u811a\u672c\u540d\uff0c<code>sleep<\/code>\u00a0\u63a7\u5236\u6267\u884c\u8282\u594f\u3002<\/p>\n<p>\u2022 \u7cfb\u7edf\u547d\u4ee4\uff1a<code>shutdown<\/code>\u00a0\u548c\u00a0<code>uname<\/code>\u00a0\u56e0\u7cfb\u7edf\u5dee\u5f02\u53ef\u80fd\u9009\u9879\u4e0d\u540c\uff0c\u5efa\u8bae\u67e5\u9605\u624b\u518c\uff08<code>man<\/code>\uff09\u3002<\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux \u6587\u4ef6\u6743\u9650 setuid &#8212; \u4e3a\u6587\u4ef6\u6388\u4e88\u7279\u6b8a\u6743\u9650\uff0c\u5f53\u7528\u6237\u8fd0\u884c\u8be5\u6587\u4ef6\u65f6\u53ef\u4ee5\u4f7f\u7528\u6240\u6709\u8005\u6743\u9650  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-520","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts\/520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/comments?post=520"}],"version-history":[{"count":2,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts\/520\/revisions"}],"predecessor-version":[{"id":522,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts\/520\/revisions\/522"}],"wp:attachment":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/media?parent=520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/categories?post=520"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/tags?post=520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}