{"id":116359,"date":"2026-10-21T08:17:27","date_gmt":"2026-10-21T00:17:27","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/116359.html"},"modified":"2026-10-21T08:17:27","modified_gmt":"2026-10-21T00:17:27","slug":"%e3%80%90linux%e3%80%91-%e8%87%aa%e4%b8%bbshell%e7%bc%96%e5%86%99%e5%b7%a9%e5%9b%ba%e5%a4%8d%e4%b9%a0%e7%af%87","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/116359.html","title":{"rendered":"\u3010Linux\u3011 \u81ea\u4e3bshell\u7f16\u5199\u5de9\u56fa\u590d\u4e60\u7bc7"},"content":{"rendered":"<p>        \/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/<br \/>\n        #content_views .toc,<br \/>\n        \/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/<br \/>\n        #content_views.markdown_views &gt; p:empty:has(+ .toc),<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty,<br \/>\n        \/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/<br \/>\n        #content_views.htmledit_views #main-toc,<br \/>\n        #content_views.htmledit_views #hr-toc,<br \/>\n        #content_views.htmledit_views p[id*=\\&#8221;-toc\\&#8221;] {<br \/>\n          display: none !important;<br \/>\n        }<br \/>\n        \/* \u76ee\u5f55\u53bb\u6389\u540e\uff0c\u7d27\u8ddf\u7684\u9996\u4e2a\u6807\u9898\u4e0d\u518d\u591a\u51fa\u4e00\u5757\u4e0a\u8fb9\u8ddd *\/<br \/>\n        #content_views.markdown_views &gt; .toc + h1,<br \/>\n        #content_views.markdown_views &gt; .toc + h2,<br \/>\n        #content_views.markdown_views &gt; .toc + h3,<br \/>\n        #content_views.markdown_views &gt; .toc + h4,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h1,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h2,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h3,<br \/>\n        #content_views.markdown_views &gt; .toc + p:empty + h4 {<br \/>\n          margin-top: 0 !important;<br \/>\n        }<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1080\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261021001722-6ad804927d5a1.png\" width=\"1920\" \/><\/p>\n<hr \/>\n<p><span style=\"color:#0d0016\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"721\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261021001724-6ad804941b327.png\" width=\"798\" \/><\/span><\/p>\n<h3><span style=\"color:#1a439c\">\u4e00\u3001\u5168\u5c40\u914d\u7f6e\u4e0e\u72b6\u6001&#xff08;\u4ee3\u7801\u6700\u5f00\u5934&#xff09;<\/span><\/h3>\n<table>\n<tr><span style=\"color:#0d0016\">\u53d8\u91cf<\/span><span style=\"color:#0d0016\">\u7c7b\u578b<\/span><span style=\"color:#0d0016\">\u4f5c\u7528<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">basesize &#061; 1024<\/span><\/td>\n<td style=\"width:119px\"><span style=\"color:#0d0016\">const int<\/span><\/td>\n<td style=\"width:324px\"><span style=\"color:#0d0016\">\u547d\u4ee4\u884c\u7f13\u51b2\u533a\u548c pwd \u7f13\u51b2\u533a\u5927\u5c0f<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">argvnum &#061; 64<\/span><\/td>\n<td style=\"width:119px\"><span style=\"color:#0d0016\">const int<\/span><\/td>\n<td style=\"width:324px\"><span style=\"color:#0d0016\">gargv \u6700\u591a\u5b58 64 \u4e2a\u53c2\u6570<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">envnum &#061; 64<\/span><\/td>\n<td style=\"width:119px\"><span style=\"color:#0d0016\">const int<\/span><\/td>\n<td style=\"width:324px\"><span style=\"color:#0d0016\">\u81ea\u7ef4\u62a4\u73af\u5883\u8868 genv \u6700\u591a 64 \u9879<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">gargv[64]<\/span><\/td>\n<td style=\"width:119px\"><span style=\"color:#0d0016\">static char*[]<\/span><\/td>\n<td style=\"width:324px\"><span style=\"color:#0d0016\">\u89e3\u6790\u540e\u7684\u53c2\u6570\u8868&#xff0c;\u672b\u5c3e\u5fc5\u987b NULL&#xff08;exec \u65cf\u8981\u6c42&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">gargc<\/span><\/td>\n<td style=\"width:119px\"><span style=\"color:#0d0016\">static int<\/span><\/td>\n<td style=\"width:324px\"><span style=\"color:#0d0016\">\u53c2\u6570\u4e2a\u6570<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">lastcode<\/span><\/td>\n<td style=\"width:119px\"><span style=\"color:#0d0016\">static int<\/span><\/td>\n<td style=\"width:324px\"><span style=\"color:#0d0016\">\u4e0a\u4e00\u6761\u547d\u4ee4\u9000\u51fa\u7801&#xff0c;echo $? \u5c31\u8bfb\u5b83<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">genv[64]<\/span><\/td>\n<td style=\"width:119px\"><span style=\"color:#0d0016\">static char*[]<\/span><\/td>\n<td style=\"width:324px\"><span style=\"color:#0d0016\">shell \u81ea\u5df1\u7ef4\u62a4\u7684\u73af\u5883\u53d8\u91cf\u8868&#xff08;\u540e\u9762\u91cd\u70b9\u8bb2&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">pwd[1024] \/ pwdenv[1024]<\/span><\/td>\n<td style=\"width:119px\"><span style=\"color:#0d0016\">static char[]<\/span><\/td>\n<td style=\"width:324px\"><span style=\"color:#0d0016\">\u5f53\u524d\u76ee\u5f55 \/ \u62fc\u597d\u7684 &#034;PWD&#061;xxx&#034; \u5b57\u7b26\u4e32<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color:#0d0016\">\u51e0\u4e2a\u8bbe\u8ba1\u8981\u70b9&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">\u8fd9\u4e9b\u90fd\u662f\u5168\u5c40 static&#xff0c;\u751f\u547d\u5468\u671f\u7b49\u4e8e\u6574\u4e2a\u7a0b\u5e8f&#xff0c;\u540e\u7eed\u51fd\u6570\u968f\u4fbf\u7528\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">gargv \u662f char* \u6570\u7ec4&#xff0c;\u6307\u5411\u547d\u4ee4\u884c\u7f13\u51b2\u533a\u5185\u90e8\u7684\u5404\u4e2a\u5355\u8bcd&#xff08;strtok \u539f\u5730\u5207\u51fa\u6765\u7684&#xff09;&#xff0c;\u4e0d\u662f\u62f7\u8d1d\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">lastcode \u662f\u6574\u4e2a shell \u7684\u72b6\u6001\u6838\u5fc3\u2014\u2014echo $? \u7684 $? \u4e0d\u662f\u7cfb\u7edf\u53d8\u91cf&#xff0c;\u5c31\u662f\u6253\u5370\u8fd9\u4e2a\u53d8\u91cf\u3002<\/span><\/li>\n<\/ul>\n<hr \/>\n<h3><span style=\"color:#1a439c\">\u4e8c\u3001\u63d0\u793a\u7b26\u51fd\u6570\u7ec4&#xff08;6 \u4e2a\u51fd\u6570&#xff0c;\u4f5c\u7528\u4e00\u6761\u94fe&#xff09;<\/span><\/h3>\n<p>GetUserName \u2192 GetHostName \u2192 GetPwd \u2192 LastDir \u2192 MakeCommandLine \u2192 PrintCommandLine<\/p>\n<p><span style=\"color:#0d0016\">GetUserName \/ GetHostName&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0getenv(&#034;USER&#034;)\u3001getenv(&#034;HOSTNAME&#034;)&#xff0c;\u4ece\u8fdb\u7a0b\u73af\u5883\u8868\u8bfb\u7528\u6237\u540d\u548c\u4e3b\u673a\u540d\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u627e\u4e0d\u5230\u8fd4\u56de &#034;None&#034;\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6ce8\u610f getenv \u8fd4\u56de\u7684\u6307\u9488\u662f\u53ea\u8bfb\u7684&#xff08;\u6307\u5411 environ \u8868\u5185\u90e8&#xff09;&#xff0c;\u4e0d\u8981\u5c1d\u8bd5\u4fee\u6539\u3002<\/span><\/p>\n<\/p>\n<p><span style=\"color:#0d0016\">GetPwd&#xff08;\u542b\u4e00\u4e2a\u5173\u952e\u52a8\u4f5c&#xff09;&#xff1a;<\/span><\/p>\n<p>if (getcwd(pwd, sizeof(pwd)) &#061;&#061; NULL) return &#034;None&#034;;<br \/>\nsnprintf(pwdenv, sizeof(pwdenv), &#034;PWD&#061;%s&#034;, pwd);<br \/>\nputenv(pwdenv);<\/p>\n<ul>\n<li><span style=\"color:#0d0016\">getcwd \u662f\u7cfb\u7edf\u8c03\u7528&#xff1a;\u95ee\u5185\u6838&#034;\u6211\u7684\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u5728\u54ea&#034;&#xff0c;\u7ed3\u679c\u5199\u5165 pwd\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">snprintf \u62fc\u51fa &#034;PWD&#061;\/home\/xx&#034; \u5b58\u8fdb\u5168\u5c40 pwdenv\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">putenv(pwdenv) \u628a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5199\u56de\u8fdb\u7a0b\u7684\u771f\u5b9e\u73af\u5883\u8868&#xff08;\u6ce8\u610f&#xff1a;\u5199\u7684\u662f pwd \u91cc\u7684&#xff0c;\u4e0d\u662f genv&#xff0c;\u8fd9\u5c31\u662f\u540e\u9762\u7684\u5751\u4e4b\u4e00&#xff0c;\u8be6\u89c1\u7b2c\u516b\u8282&#xff09;\u3002<\/span><\/li>\n<\/ul>\n<table>\n<tr><span style=\"color:#0d0016\">\u7ef4\u5ea6<\/span><span style=\"color:#0d0016\">getcwd<\/span><span style=\"color:#0d0016\">getenv<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">\u5934\u6587\u4ef6<\/span><\/td>\n<td style=\"width:302px\"><span style=\"color:#0d0016\">&lt;unistd.h&gt;<\/span><\/td>\n<td style=\"width:263px\"><span style=\"color:#0d0016\">&lt;stdlib.h&gt;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u83b7\u53d6\u5bf9\u8c61<\/span><\/td>\n<td style=\"width:302px\"><span style=\"color:#0d0016\">\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55&#xff08;\u7edd\u5bf9\u8def\u5f84&#xff09;<\/span><\/td>\n<td style=\"width:263px\"><span style=\"color:#0d0016\">\u73af\u5883\u53d8\u91cf\u7684\u503c<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u7ed3\u679c\u5b58\u54ea<\/span><\/td>\n<td style=\"width:302px\"><span style=\"color:#0d0016\">\u4f60\u81ea\u5df1\u7684\u7f13\u51b2\u533a&#xff08;\u6216\u52a8\u6001\u5206\u914d&#xff09;<\/span><\/td>\n<td style=\"width:263px\"><span style=\"color:#0d0016\">\u8fdb\u7a0b\u73af\u5883\u5757\u91cc\u7684\u5185\u90e8\u9759\u6001\u533a<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u8c01\u7ba1\u751f\u547d\u5468\u671f<\/span><\/td>\n<td style=\"width:302px\"><span style=\"color:#0d0016\">\u8c03\u7528\u8005<\/span><\/td>\n<td style=\"width:263px\"><span style=\"color:#0d0016\">\u8fdb\u7a0b\u73af\u5883&#xff08;\u540e\u7eed\u8c03\u7528\u53ef\u80fd\u8986\u76d6&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u5931\u8d25\u8868\u73b0<\/span><\/td>\n<td style=\"width:302px\"><span style=\"color:#0d0016\">\u8fd4\u56de NULL &#043; errno&#xff08;ERANGE \u7f13\u51b2\u533a\u592a\u5c0f \/ EACCES \u65e0\u6743\u9650 \/ ENOENT \u76ee\u5f55\u88ab\u5220&#xff09;<\/span><\/td>\n<td style=\"width:263px\"><span style=\"color:#0d0016\">\u8fd4\u56de NULL&#xff0c;\u65e0 errno \u533a\u5206 &#034;\u4e0d\u5b58\u5728&#034;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">\u5bf9\u5e94\u7684\u4fee\u6539\u51fd\u6570<\/span><\/td>\n<td style=\"width:302px\"><span style=\"color:#0d0016\">chdir()<\/span><\/td>\n<td style=\"width:263px\"><span style=\"color:#0d0016\">setenv() \/ putenv()<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color:#0d0016\">LastDir&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u53d6\u6700\u540e\u4e00\u4e2a \/ \u540e\u9762\u7684\u90e8\u5206\u505a\u63d0\u793a\u7b26\u76ee\u5f55\u663e\u793a&#xff08;\/home\/xx \u2192 xx&#xff09;\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0MakeCommandLine&#xff1a;\u62fc\u6210 [user&#064;host dir]# \u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PrintCommandLine&#xff1a;<\/span><\/p>\n<p>printf(&#034;%s&#034;, MakeCommandLine().c_str());<br \/>\nfflush(stdout);     \/\/ \u5fc5\u987b fflush&#xff0c;\u56e0\u4e3a\u63d0\u793a\u7b26\u6ca1 \\\\n<\/p>\n<p><span style=\"color:#0d0016\">\u8fd9\u662f\u96be\u70b9&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0printf \u9ed8\u8ba4\u662f\u884c\u7f13\u51b2&#xff08;\u6709 \\\\n \u624d\u771f\u6b63\u8f93\u51fa&#xff09;&#xff0c;\u63d0\u793a\u7b26\u6ca1\u6709\u6362\u884c&#xff0c;\u6240\u4ee5\u5fc5\u987b\u624b\u52a8 fflush&#xff0c;\u5426\u5219\u4f60\u8f93\u5165\u547d\u4ee4\u65f6\u63d0\u793a\u7b26\u8fd8\u618b\u5728\u7f13\u51b2\u533a\u91cc\u6ca1\u663e\u793a\u51fa\u6765\u3002<\/span><\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">\u4e09\u3001GetCommandLine&#xff1a;\u8bfb\u4e00\u884c<\/span><\/h3>\n<p>if (fgets(buf, size, stdin) &#061;&#061; NULL) { printf(&#034;\\\\n&#034;); return false; }<br \/>\nbuf[strlen(buf) &#8211; 1] &#061; &#039;\\\\0&#039;;   \/\/ \u53bb\u6389 fgets \u8bfb\u8fdb\u6765\u7684 \\\\n<br \/>\nif (strlen(buf) &#061;&#061; 0) return false;<\/p>\n<ul>\n<li><span style=\"color:#0d0016\">fgets \u8bfb\u5230\u6362\u884c\u6216\u7f13\u51b2\u533a\u6ee1\u4e3a\u6b62&#xff0c;\u6362\u884c\u4f1a\u4fdd\u7559\u5728 buf \u91cc&#xff0c;\u6240\u4ee5\u8981\u624b\u52a8\u628a\u6700\u540e\u4e00\u4f4d&#xff08;\\\\n&#xff09;\u6362\u6210 &#039;\\\\0&#039;\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u4e09\u4e2a\u8fd4\u56de\u503c\u8bed\u4e49&#xff1a;<\/span>\n<ul>\n<li><span style=\"color:#0d0016\">\u6b63\u5e38\u4e00\u884c \u2192 true<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u7a7a\u884c&#xff08;\u76f4\u63a5\u56de\u8f66&#xff0c;\u53bb \\\\n \u540e\u4e3a\u7a7a&#xff09;\u2192 false&#xff0c;\u4e3b\u5faa\u73af continue \u91cd\u65b0\u6253\u5370\u63d0\u793a\u7b26<\/span><\/li>\n<li><span style=\"color:#0d0016\">EOF&#xff08;Ctrl&#043;D&#xff09;\u2192 \u6253\u5370\u6362\u884c\u3001false\u2014\u2014\u8fd9\u91cc\u57cb\u4e86\u4e2a bug&#xff0c;\u89c1\u7b2c\u516b\u8282\u3002<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<h3><span style=\"color:#1a439c\">\u56db\u3001ParseCommandLine&#xff1a;strtok \u62c6\u8bcd&#xff08;\u96be\u70b9\u2460&#xff09;<\/span><\/h3>\n<p>const char *sep &#061; &#034; &#034;;<br \/>\ngargv[gargc&#043;&#043;] &#061; strtok(buf, sep);          \/\/ \u7b2c\u4e00\u6b21\u8c03\u7528\u4f20 buf<br \/>\nwhile ((gargv[gargc&#043;&#043;] &#061; strtok(NULL, sep)) !&#061; NULL)<br \/>\n    ;<br \/>\ngargc&#8211;;<\/p>\n<p><span style=\"color:#0d0016\">strtok \u7684\u5de5\u4f5c\u539f\u7406&#xff08;\u8fd9\u662f\u6700\u503c\u5f97\u6df1\u6316\u7684\u5730\u65b9&#xff09;&#xff1a;<\/span><\/p>\n<li><span style=\"color:#0d0016\">\u7b2c\u4e00\u6b21\u8c03\u7528 strtok(buf, &#034; &#034;)&#xff1a;\u4ece buf \u5f00\u5934\u627e\u7b2c\u4e00\u4e2a\u975e\u7a7a\u683c\u5b57\u7b26&#xff0c;\u8bb0\u4e0b\u5355\u8bcd\u8d77\u70b9&#xff1b;\u518d\u4ece\u90a3\u91cc\u5f80\u540e\u627e\u7a7a\u683c&#xff0c;\u627e\u5230\u5c31\u628a\u7a7a\u683c\u66ff\u6362\u6210 &#039;\\\\0&#039;&#xff0c;\u8fd4\u56de\u5355\u8bcd\u8d77\u70b9\u6307\u9488\u3002\u540c\u65f6&#xff0c;strtok \u5185\u90e8\u7528\u4e00\u4e2a\u9759\u6001\u6307\u9488\u8bb0\u4f4f\u5f53\u524d\u4f4d\u7f6e&#xff08;\u505c\u5728\u521a\u624d\u90a3\u4e2a &#039;\\\\0&#039; \u4e4b\u540e&#xff09;\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u540e\u7eed\u8c03\u7528 strtok(NULL, &#034; &#034;)&#xff1a;\u4e0d\u4f20\u65b0\u5b57\u7b26\u4e32&#xff0c;strtok \u4ece\u5185\u90e8\u8bb0\u4f4f\u7684\u4f4d\u7f6e\u7ee7\u7eed\u5207\u4e0b\u4e00\u4e2a\u5355\u8bcd\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u8fde\u7eed\u7a7a\u683c\u81ea\u52a8\u8df3\u8fc7\u2014\u2014\u6240\u4ee5 ls -l \u548c ls -l \u7ed3\u679c\u4e00\u6837\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5b83\u76f4\u63a5\u4fee\u6539\u539f\u5b57\u7b26\u4e32&#xff1a;buf \u91cc\u7684\u7a7a\u683c\u88ab\u6539\u5199\u6210 &#039;\\\\0&#039;&#xff0c;\u4e8e\u662f buf \u88ab\u539f\u5730\u62c6\u6210\u597d\u51e0\u6bb5&#xff0c;gargv[i] \u53ea\u662f\u6307\u5411\u8fd9\u4e9b\u6bb5\u7684\u9996\u5730\u5740\u3002<\/span><\/li>\n<p><span style=\"color:#0d0016\">\u4e3e\u4f8b&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8f93\u5165 ls -l \/home&#xff0c;\u89e3\u6790\u540e\u5185\u5b58\u91cc\u662f ls\\\\0-l\\\\0\/home\\\\0&#xff0c;gargv &#061; {&#034;ls&#034;, &#034;-l&#034;, &#034;\/home&#034;, NULL}\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6700\u540e\u90a3\u4e2a gargc&#8211; \u662f\u56e0\u4e3a while \u5faa\u73af\u5728\u8d4b NULL \u4e4b\u540e\u53c8\u628a gargc \u52a0\u4e86\u4e00\u6b21&#xff08;\u5faa\u73af\u6761\u4ef6\u5148\u8d4b\u503c\u540e\u5224\u65ad&#xff09;&#xff0c;\u6240\u4ee5\u8981\u591a\u9000\u4e00\u683c&#xff0c;\u4f46 NULL \u5df2\u7ecf\u5199\u8fdb\u6570\u7ec4\u2014\u2014\u8fd9\u6b63\u662f exec \u65cf\u9700\u8981\u7684\u7ed3\u5c3e\u6807\u8bb0\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u6f5c\u5728\u5d29\u6e83\u70b9&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5982\u679c\u8f93\u5165\u662f\u7eaf\u7a7a\u683c&#xff08;\u5982 &#034; &#034;&#xff09;&#xff0c;\u7b2c\u4e00\u4e2a strtok \u8fd4\u56de NULL&#xff0c;gargv[0]&#061;NULL&#xff0c;\u540e\u9762 CheckAndExecBuiltCommand \u91cc strcmp(gargv[0], &#034;exit&#034;) \u4f1a\u5bf9 NULL \u505a strcmp \u2192 \u6bb5\u9519\u8bef\u3002<\/span><\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">\u4e94\u3001\u73af\u5883\u53d8\u91cf&#xff1a;InitEnv \/ AddEnv&#xff08;\u96be\u70b9\u2461&#xff1a;\u4e09\u5957\u73af\u5883\u8868&#xff09;<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u8fd9\u4e2a\u7a0b\u5e8f\u91cc\u5176\u5b9e\u6709\u4e09\u5957\u73af\u5883&#xff0c;\u521d\u5b66\u8005\u6700\u5bb9\u6613\u6df7\u6dc6&#xff1a;<\/span><\/p>\n<table>\n<tr><span style=\"color:#0d0016\">\u8868<\/span><span style=\"color:#0d0016\">\u662f\u8c01<\/span><span style=\"color:#0d0016\">\u600e\u4e48\u4ea7\u751f\u7684<\/span><span style=\"color:#0d0016\">\u8c01\u5728\u7528<\/span><\/tr>\n<tbody>\n<tr>\n<td style=\"width:76px\"><span style=\"color:#0d0016\">environ<\/span><\/td>\n<td style=\"width:298px\">\n<p><span style=\"color:#0d0016\">\u5185\u6838 &#043; libc \u7ef4\u62a4\u7684\u8fdb\u7a0b\u771f\u5b9e\u73af\u5883\u8868<\/span><\/p>\n<p><span style=\"color:#0d0016\">&#xff08;extern char **environ&#xff0c;\u4ee5 NULL \u7ed3\u5c3e&#xff09;<\/span><\/p>\n<\/td>\n<td><span style=\"color:#0d0016\">\u8fdb\u7a0b\u542f\u52a8\u65f6\u7531 exec \u4f20\u5165<\/span><\/td>\n<td><span style=\"color:#0d0016\">getenv \/ putenv \u64cd\u4f5c\u5b83<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width:76px\"><span style=\"color:#0d0016\">genv<\/span><\/td>\n<td style=\"width:298px\"><span style=\"color:#0d0016\">shell \u81ea\u5df1 malloc \u62f7\u8d1d\u7684\u4e00\u4efd<\/span><\/td>\n<td><span style=\"color:#0d0016\">InitEnv \u4ece environ \u62f7\u8d1d&#xff0c;export \u8ffd\u52a0<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4f20\u7ed9 execvpe \u7ed9\u5b50\u8fdb\u7a0b<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"width:76px\"><span style=\"color:#0d0016\">pwdenv<\/span><\/td>\n<td style=\"width:298px\"><span style=\"color:#0d0016\">\u5168\u5c40\u9759\u6001\u6570\u7ec4<\/span><\/td>\n<td><span style=\"color:#0d0016\">GetPwd \u6bcf\u6b21\u62fc PWD&#061;xxx \u5e76 putenv<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u53ea\u5199\u56de environ&#xff0c;\u4e0d\u8fdb genv<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color:#0d0016\">InitEnv&#xff1a;<\/span><\/p>\n<p>extern char **environ;      \/\/ \u58f0\u660e&#xff1a;\u8fdb\u7a0b\u771f\u5b9e\u73af\u5883\u8868<br \/>\nfor (; environ[i] &amp;&amp; i &lt; envnum &#8211; 1; i&#043;&#043;) {<br \/>\n    genv[i] &#061; (char *)malloc(strlen(environ[i]) &#043; 1);<br \/>\n    strcpy(genv[i], environ[i]);<br \/>\n}<br \/>\ngenv[i] &#061; NULL;<\/p>\n<p><span style=\"color:#b95514\">\u4e3a\u4ec0\u4e48\u8981 malloc \u62f7\u8d1d\u800c\u4e0d\u662f\u76f4\u63a5 genv[i] &#061; environ[i]&#xff1f;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u56e0\u4e3a putenv\u3001setenv \u53ef\u80fd\u4f1a\u79fb\u52a8\/\u6539\u5199 environ \u8868\u9879&#xff0c;\u62f7\u4e00\u4efd\u81ea\u5df1\u7684\u526f\u672c\u624d\u5b89\u5168\u3002\u8fd9\u4e2a shell \u540e\u7eed\u8981\u7ed9\u5b50\u8fdb\u7a0b\u4e00\u4efd&#034;\u81ea\u5df1\u8bf4\u4e86\u7b97&#034;\u7684\u73af\u5883&#xff08;\u7528\u6237 export \u52a0\u8fdb\u53bb\u7684\u53d8\u91cf\u4e5f\u8981\u5728&#xff09;&#xff0c;\u6240\u4ee5\u5b83\u7ef4\u62a4\u81ea\u5df1\u7684 genv\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">AddEnv&#xff1a;<\/span><\/p>\n<p>while (genv[i]) i&#043;&#043;;          \/\/ \u627e\u5230\u672b\u5c3e<br \/>\ngenv[i] &#061; malloc(strlen(item)&#043;1);<br \/>\nstrcpy(genv[i], item);<br \/>\ngenv[&#043;&#043;i] &#061; NULL;<\/p>\n<p><span style=\"color:#0d0016\">\u6ce8\u610f\u5b83\u6ca1\u6709\u68c0\u67e5 genv \u662f\u5426\u5df2\u6ee1 64 \u9879\u2014\u2014export \u8d85\u8fc7 64 \u6b21\u4f1a\u6570\u7ec4\u8d8a\u754c\u5199&#xff08;\u5185\u5b58\u7834\u574f&#xff09;\u3002<\/span><\/p>\n<hr \/>\n<h3><span style=\"color:#1a439c\">\u516d\u3001ExecuteCommand&#xff1a;fork \/ exec \/ waitpid&#xff08;\u96be\u70b9\u2462&#xff0c;\u5168\u7a0b\u5e8f\u6838\u5fc3&#xff09;<\/span><\/h3>\n<p>pid_t id &#061; fork();<br \/>\nif (id &lt; 0) { perror(&#034;fork&#034;); lastcode &#061; 1; return; }<br \/>\nif (id &#061;&#061; 0) {<br \/>\n    execvpe(gargv[0], gargv, genv);   \/\/ \u5b50\u8fdb\u7a0b<br \/>\n    perror(gargv[0]);                 \/\/ exec \u5931\u8d25\u624d\u4f1a\u8d70\u5230\u8fd9<br \/>\n    exit(127);<br \/>\n}<br \/>\nint status &#061; 0;<br \/>\nwaitpid(id, &amp;status, 0);              \/\/ \u7236\u8fdb\u7a0b<br \/>\nif (WIFEXITED(status))<br \/>\n    lastcode &#061; WEXITSTATUS(status);<br \/>\nelse if (WIFSIGNALED(status))<br \/>\n    lastcode &#061; 128 &#043; WTERMSIG(status);<br \/>\nelse<br \/>\n    lastcode &#061; 100;<\/p>\n<p><span style=\"color:#b95514\">\u2460 fork()&#xff1a;\u4e00\u6b21\u8c03\u7528&#xff0c;\u4e24\u6b21\u8fd4\u56de<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">fork \u4f1a\u590d\u5236\u5f53\u524d\u8fdb\u7a0b&#xff08;\u5199\u65f6\u62f7\u8d1d COW&#xff1a;\u4e0d\u4fee\u6539\u6570\u636e\u65f6\u7236\u5b50\u5171\u4eab\u7269\u7406\u5185\u5b58\u9875&#xff0c;\u8c01\u5148\u6539\u8c01\u590d\u5236&#xff09;&#xff0c;\u521b\u5efa\u5b50\u8fdb\u7a0b\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u8fd4\u56de\u503c\u8bed\u4e49&#xff1a;\u7236\u8fdb\u7a0b\u5f97\u5230\u5b50\u8fdb\u7a0b\u7684 PID&#xff08;&gt;0&#xff09;&#xff1b;\u5b50\u8fdb\u7a0b\u5f97\u5230 0&#xff1b;\u5931\u8d25\u5f97 -1\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5224\u65ad id &#061;&#061; 0 \u5c31\u662f\u5728\u95ee&#034;\u6211\u73b0\u5728\u662f\u4e0d\u662f\u5b50\u8fdb\u7a0b&#xff1f;&#034;\u2014\u2014\u8fd9\u5c31\u662f fork \u540e\u7236\u5b50\u4ee3\u7801\u5206\u9053\u626c\u9573\u7684\u7ecf\u5178\u5199\u6cd5\u3002<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#b95514\">\u2461 execvpe&#xff1a;exec \u5bb6\u65cf\u91cc\u529f\u80fd\u6700\u5168\u7684\u4e00\u4e2a execvpe(\u6587\u4ef6\u540d, argv, envp) \u62c6\u5f00\u770b&#xff1a;<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">exec&#xff1a;\u7528\u65b0\u7a0b\u5e8f\u66ff\u6362\u5f53\u524d\u8fdb\u7a0b\u6620\u50cf\u2014\u2014\u4ee3\u7801\u6bb5\u3001\u6570\u636e\u6bb5\u3001\u5806\u6808\u5168\u6362&#xff0c;PID \u4e0d\u53d8\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">v&#xff1a;\u53c2\u6570\u4ee5 char*[]&#xff08;argv \u6570\u7ec4&#xff09;\u4f20\u5165\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">p&#xff1a;\u81ea\u52a8\u6309 PATH \u73af\u5883\u53d8\u91cf\u641c\u7d22\u53ef\u6267\u884c\u6587\u4ef6&#xff0c;\u6240\u4ee5\u4e0d\u7528\u5199 \/bin\/ls&#xff0c;\u5199 ls \u5c31\u884c\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">e&#xff1a;\u7528\u81ea\u5b9a\u4e49\u73af\u5883 genv \u4f5c\u4e3a\u65b0\u7a0b\u5e8f\u7684\u73af\u5883\u8868&#xff08;\u4e0d\u662f\u9ed8\u8ba4\u7684 environ&#xff09;\u3002<\/span><\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u5173\u952e&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0exec \u6210\u529f\u4e0d\u8fd4\u56de\u2014\u2014\u65b0\u7a0b\u5e8f\u8dd1\u8d77\u6765\u540e&#xff0c;&#034;\u5b50\u8fdb\u7a0b&#034;\u8fd9\u4e2a\u8eab\u4efd\u5c31\u53d8\u6210 ls \u8fdb\u7a0b\u4e86&#xff0c;perror \u548c exit(127) \u90a3\u4e24\u884c\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c&#xff1b;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u53ea\u6709 exec \u5931\u8d25&#xff08;\u6bd4\u5982\u547d\u4ee4\u4e0d\u5b58\u5728&#xff09;\u624d\u8fd4\u56de -1&#xff0c;\u6253\u5370\u9519\u8bef\u5e76 exit(127)\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0127 \u662f shell \u7ea6\u5b9a&#xff1a;&#034;\u547d\u4ee4\u627e\u4e0d\u5230&#034;\u3002<\/span><\/p>\n<p><span style=\"color:#b95514\">\u2462 waitpid&#xff1a;\u7236\u8fdb\u7a0b\u6536\u5c38<\/span><\/p>\n<ul>\n<li><span style=\"color:#0d0016\">waitpid(id, &amp;status, 0) \u963b\u585e\u7b49\u5f85\u6307\u5b9a\u5b50\u8fdb\u7a0b\u7ed3\u675f&#xff0c;\u56de\u6536\u5176\u8d44\u6e90&#xff08;\u50f5\u5c38\u8fdb\u7a0b\u56de\u6536&#xff09;\u3002status \u662f\u4e00\u4e2a int&#xff0c;\u9000\u51fa\u4fe1\u606f\u88ab\u6253\u5305\u5728\u7279\u5b9a\u4f4d\u91cc&#xff0c;\u5fc5\u987b\u7528\u5b8f\u62c6\u5f00&#xff1a;<\/span>\n<ul>\n<li><span style=\"color:#0d0016\">WIFEXITED(status)&#xff1a;\u662f\u5426\u6b63\u5e38\u9000\u51fa&#xff08;\u8c03\u7528\u4e86 exit \/ main \u8fd4\u56de&#xff09;\u2192 WEXITSTATUS \u53d6\u4f4e 8 \u4f4d\u9000\u51fa\u7801\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">WIFSIGNALED(status)&#xff1a;\u662f\u5426\u88ab\u4fe1\u53f7\u6740\u6b7b&#xff08;\u5982 kill\u3001Ctrl&#043;C \u7684 SIGINT&#xff09;\u2192 128 &#043; WTERMSIG(status)&#xff0c;\u8fd9\u662f shell \u60ef\u4f8b&#xff1a;\u88ab 9 \u53f7\u4fe1\u53f7\u6740\u5c31\u662f 137\u3002<\/span><\/li>\n<\/ul>\n<\/li>\n<li><span style=\"color:#0d0016\">\u62c6\u51fa\u7684\u7ed3\u679c\u5b58\u8fdb\u5168\u5c40 lastcode&#xff0c;\u4f9b echo $? \u8bfb\u53d6\u3002<\/span><\/li>\n<\/ul>\n<hr \/>\n<h3><span style=\"color:#1a439c\">\u4e03\u3001\u5185\u5efa\u547d\u4ee4 CheckAndExecBuiltCommand<\/span><\/h3>\n<p><span style=\"color:#0d0016\">\u9010\u4e2a\u770b&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">exit&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u76f4\u63a5 exit(0) \u7ec8\u6b62\u6574\u4e2a\u8fdb\u7a0b\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u4e3a\u4ec0\u4e48 shell \u7684 exit \u5fc5\u987b\u662f\u5185\u5efa&#xff1f;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u56e0\u4e3a\u5982\u679c\u662f\u5916\u90e8\u7a0b\u5e8f&#xff0c;exit \u53ea\u80fd\u7ed3\u675f\u90a3\u4e2a\u5b50\u8fdb\u7a0b&#xff0c;shell \u81ea\u5df1\u8fd8\u6d3b\u7740\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">cd&#xff1a;<\/span><\/p>\n<p>if (gargc &#061;&#061; 2) {<br \/>\n    if (chdir(gargv[1]) !&#061; 0) perror(&#034;cd&#034;);<br \/>\n} else {<br \/>\n    chdir(getenv(&#034;HOME&#034;) ? getenv(&#034;HOME&#034;) : &#034;\/&#034;);<br \/>\n}<br \/>\nlastcode &#061; 0;<\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0chdir \u662f\u7cfb\u7edf\u8c03\u7528&#xff0c;\u53ea\u6539\u53d8\u5f53\u524d\u8fdb\u7a0b&#xff08;shell&#xff09;\u81ea\u5df1\u7684\u5de5\u4f5c\u76ee\u5f55\u2014\u2014\u8fd9\u4e5f\u662f\u5fc5\u987b\u5185\u5efa\u7684\u539f\u56e0&#xff1a;\u5916\u90e8\u5b50\u8fdb\u7a0b\u6539\u4e86\u76ee\u5f55\u8ddf shell \u65e0\u5173\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6ce8\u610f lastcode &#061; 0 \u65e0\u6761\u4ef6\u6267\u884c&#xff0c;chdir \u5931\u8d25\u4e5f\u662f 0&#xff08;\u7f3a\u9677&#xff09;\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">export&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0AddEnv(gargv[1])&#xff0c;\u628a KEY&#061;VALUE \u8ffd\u52a0\u8fdb genv \u8868\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e0b\u4e00\u8f6e fork \u51fa\u6765\u7684\u5b50\u8fdb\u7a0b\u5c31\u4f1a\u5e26\u4e0a\u8fd9\u4e2a\u53d8\u91cf\u2014\u2014\u8fd9\u5c31\u662f\u73af\u5883\u53d8\u91cf&#034;\u7ee7\u627f&#034;\u7684\u5b9e\u73b0\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">env&#xff1a;\u904d\u5386\u6253\u5370 genv\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">echo&#xff1a;<\/span><\/p>\n<p>if (gargc &#061;&#061; 2) {<br \/>\n    if (gargv[1][0] &#061;&#061; &#039;$&#039; &amp;&amp; gargv[1][1] &#061;&#061; &#039;?&#039;)<br \/>\n        printf(&#034;%d\\\\n&#034;, lastcode);<br \/>\n    else<br \/>\n        printf(&#034;%s\\\\n&#034;, gargv[1]);<br \/>\n} else {<br \/>\n    printf(&#034;\\\\n&#034;);<br \/>\n}<\/p>\n<ul>\n<li><span style=\"color:#0d0016\">echo $? \u5c31\u662f\u6253\u5370 lastcode\u3002<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u53ea\u652f\u6301\u5355\u4e2a\u53c2\u6570&#xff1a;echo a b c \u4f1a\u8d70 else \u53ea\u6253\u5370\u6362\u884c&#xff1b;echo $?x \u56e0\u4e3a\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u662f ? \u4e5f\u4f1a\u88ab\u8bef\u5224\u6210 $?&#xff08;\u7f3a\u9677&#xff09;\u3002<\/span><\/li>\n<\/ul>\n<hr \/>\n<h3><span style=\"color:#1a439c\">\u516b\u3001main \u4e3b\u5faa\u73af &#043; \u5df2\u77e5\u7f3a\u9677\u6e05\u5355<\/span><\/h3>\n<p>int main() {<br \/>\n    InitEnv();              \/\/ \u542f\u52a8\u65f6\u5148\u62f7\u8d1d\u73af\u5883<br \/>\n    char cmd[basesize];<br \/>\n    while (true) {<br \/>\n        PrintCommandLine();<br \/>\n        if (!GetCommandLine(cmd, basesize)) continue;<br \/>\n        ParseCommandLine(cmd);<br \/>\n        if (CheckAndExecBuiltCommand()) continue;<br \/>\n        ExecuteCommand();<br \/>\n    }<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u903b\u8f91\u94fe&#xff1a;\u63d0\u793a\u7b26 \u2192 \u8bfb \u2192 \u89e3\u6790 \u2192 \u5185\u5efa&#xff1f;\u2192 \u662f&#xff1a;continue \u56de\u5230\u9876\u90e8&#xff1b;\u5426&#xff1a;fork \u6267\u884c\u5916\u90e8\u547d\u4ee4 \u2192 \u56de\u5230\u9876\u90e8\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u8fd9\u4e2a\u6559\u5b66\u7248\u4ee3\u7801\u91cc\u9690\u85cf\u7740\u51e0\u4e2a\u771f\u5b9e\u7f3a\u9677&#xff0c;\u503c\u5f97\u4f60\u77e5\u9053&#xff08;\u9762\u8bd5\u5e38\u88ab\u95ee&#xff09;&#xff1a;<\/span><\/p>\n<table>\n<tr><span style=\"color:#0d0016\">#<\/span><span style=\"color:#0d0016\">\u7f3a\u9677<\/span><span style=\"color:#0d0016\">\u539f\u56e0<\/span><span style=\"color:#0d0016\">\u540e\u679c<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">1<\/span><\/td>\n<td><span style=\"color:#0d0016\">Ctrl&#043;D \u6b7b\u5faa\u73af<\/span><\/td>\n<td><span style=\"color:#0d0016\">EOF \u65f6 GetCommandLine \u8fd4\u56de false&#xff0c;main \u91cc\u5374 continue \u56de\u5230\u5faa\u73af\u9876\u90e8&#xff0c;\u4e0b\u4e00\u6b21 fgets \u53c8 EOF<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5237\u5c4f\u6b7b\u5faa\u73af&#xff0c;Ctrl&#043;D \u6839\u672c\u9000\u4e0d\u51fa\u53bb&#xff08;\u5e94\u6539\u4e3a break \u6216\u533a\u5206 EOF\/\u7a7a\u884c&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">2<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u7eaf\u7a7a\u683c\u8f93\u5165\u6bb5\u9519\u8bef<\/span><\/td>\n<td><span style=\"color:#0d0016\">strtok \u8fd4\u56de NULL&#xff0c;gargv[0]&#061;NULL&#xff0c;\u968f\u540e strcmp(NULL,&#8230;)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u76f4\u63a5\u5d29<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">3<\/span><\/td>\n<td><span style=\"color:#0d0016\">PWD \u4e0d\u540c\u6b65<\/span><\/td>\n<td><span style=\"color:#0d0016\">GetPwd \u7528 putenv \u66f4\u65b0\u7684\u662f\u771f\u5b9e environ&#xff0c;\u800c\u4f20\u7ed9\u5b50\u8fdb\u7a0b\u7684\u662f genv&#xff08;\u542f\u52a8\u65f6\u62f7\u8d1d\u7684\u65e7\u503c&#xff09;<\/span><\/td>\n<td><span style=\"color:#0d0016\">cd \u4e4b\u540e\u5916\u90e8\u547d\u4ee4\u770b\u5230\u7684 $PWD \u4ecd\u662f shell \u542f\u52a8\u65f6\u7684\u503c<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">4<\/span><\/td>\n<td><span style=\"color:#0d0016\">argv \u6ea2\u51fa<\/span><\/td>\n<td><span style=\"color:#0d0016\">ParseCommandLine \u4e0d\u68c0\u67e5 gargc &lt; argvnum<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u8d85 64 \u4e2a\u53c2\u6570\u8d8a\u754c\u5199<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">5<\/span><\/td>\n<td><span style=\"color:#0d0016\">export \u8d8a\u754c<\/span><\/td>\n<td><span style=\"color:#0d0016\">AddEnv \u4e0d\u68c0\u67e5 genv \u5bb9\u91cf<\/span><\/td>\n<td><span style=\"color:#0d0016\">export \u8d85 64 \u6b21\u8d8a\u754c\u5199<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">6<\/span><\/td>\n<td><span style=\"color:#0d0016\">echo a b c \u53ea\u8f93\u51fa\u7a7a\u884c<\/span><\/td>\n<td><span style=\"color:#0d0016\">echo \u53ea\u5904\u7406 gargc&#061;&#061;2<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u591a\u53c2\u6570\u88ab\u541e<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">7<\/span><\/td>\n<td><span style=\"color:#0d0016\">cd \u5931\u8d25\u4e5f lastcode&#061;0<\/span><\/td>\n<td><span style=\"color:#0d0016\">lastcode &#061; 0 \u65e0\u6761\u4ef6\u6267\u884c<\/span><\/td>\n<td><span style=\"color:#0d0016\">$? \u4e0d\u53cd\u6620\u771f\u5b9e\u5931\u8d25<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3><span style=\"color:#1a439c\">\u4e5d\u3001\u7f16\u8bd1\u6ce8\u610f<\/span><\/h3>\n<ul>\n<li><span style=\"color:#0d0016\">\u7f16\u8bd1\u547d\u4ee4&#xff1a;g&#043;&#043; -o mini_shell mini_shell.cpp -std&#061;c&#043;&#043;11<\/span><\/li>\n<li><span style=\"color:#0d0016\">execvpe \u662f GNU \u6269\u5c55&#xff0c;\u58f0\u660e\u5728 unistd.h \u7684 #ifdef __USE_GNU \u5757\u91cc\u3002\u5982\u679c\u7f16\u8bd1\u62a5 execvpe was not declared&#xff0c;\u9700\u8981\u52a0\u5b8f&#xff1a;g&#043;&#043; -o mini_shell mini_shell.cpp -std&#061;c&#043;&#043;11 -D_GNU_SOURCE<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u5b83\u4f9d\u8d56 Linux\/Unix \u73af\u5883&#xff08;fork\/exec\/waitpid \u90fd\u662f POSIX \u7cfb\u7edf\u8c03\u7528&#xff09;&#xff0c;Windows \u4e0a\u65e0\u6cd5\u76f4\u63a5\u7f16\u8bd1\u8fd0\u884c\u3002<\/span><\/li>\n<\/ul>\n<h3><span style=\"color:#1a439c\">\u5341\u3001\u51fd\u6570\u590d\u4e60<\/span><\/h3>\n<p>C \u8bed\u8a00 &#096;&lt;string.h&gt;&#096; \u4e2d\u4ee5 &#096;str&#096; \u5f00\u5934\u7684\u51fd\u6570&#xff08;C \u98ce\u683c\u5b57\u7b26\u4e32&#xff09;&#xff1a;<\/p>\n<p>\u8868\u683c<\/p>\n<p>| \u51fd\u6570 | \u4f5c\u7528 |<br \/>\n| &#8212; | &#8212; |<br \/>\n| &#096;strlen(s)&#096;             | \u6c42\u957f\u5ea6&#xff08;\u4e0d\u542b &#096;\\\\0&#096;&#xff09; |<br \/>\n| &#096;strcpy(dest, src)&#096;     | \u590d\u5236\u5b57\u7b26\u4e32&#xff08;\u4e0d\u68c0\u67e5\u5bb9\u91cf&#xff0c;\u5371\u9669&#xff09; |<br \/>\n| &#096;strncpy(dest, src, n)&#096; | \u590d\u5236\u6700\u591a n \u4e2a\u5b57\u7b26 |<br \/>\n| &#096;strcat(dest, src)&#096;     | \u62fc\u63a5&#xff08;\u4e0d\u68c0\u67e5\u5bb9\u91cf&#xff0c;\u5371\u9669&#xff09; |<br \/>\n| &#096;strncat(dest, src, n)&#096; | \u62fc\u63a5\u6700\u591a n \u4e2a\u5b57\u7b26 |<br \/>\n| &#096;strcmp(s1, s2)&#096;        | \u6bd4\u8f83&#xff08;&lt;0\u30010\u3001&gt;0&#xff09; |<br \/>\n| &#096;strncmp(s1, s2, n)&#096;    | \u6bd4\u8f83\u524d n \u4e2a\u5b57\u7b26 |<br \/>\n| &#096;strchr(s, c)&#096;          | \u6b63\u5411\u627e\u5b57\u7b26&#xff0c;\u8fd4\u56de\u6307\u9488&#xff0c;\u627e\u4e0d\u5230\u8fd4\u56de &#096;NULL&#096; |<br \/>\n| &#096;strrchr(s, c)&#096;         | \u53cd\u5411\u627e\u5b57\u7b26 |<br \/>\n| &#096;strstr(s, sub)&#096;        | \u627e\u5b50\u4e32&#xff0c;\u8fd4\u56de\u5b50\u4e32\u9996\u5730\u5740\u6307\u9488 |<br \/>\n| &#096;strtok(s, delim)&#096;      | \u6309\u5206\u9694\u7b26\u5207\u5206\u5b57\u7b26\u4e32 |<br \/>\n| &#096;strdup(s)&#096;             | \u590d\u5236\u4e00\u4efd\u65b0\u5b57\u7b26\u4e32&#xff08;\u9700 &#096;free&#096;&#xff09; |<br \/>\n| &#096;strspn(s, accept)&#096;     | \u8fd4\u56de s \u5f00\u5934\u8fde\u7eed\u5c5e\u4e8e accept \u7684\u5b57\u7b26\u6570 |<br \/>\n| &#096;strcspn(s, reject)&#096;    | \u8fd4\u56de s \u5f00\u5934\u8fde\u7eed\u4e0d\u5c5e\u4e8e reject \u7684\u5b57\u7b26\u6570 |<br \/>\n| &#096;strerror(errno)&#096;       | \u628a\u9519\u8bef\u7801\u8f6c\u6210\u9519\u8bef\u4fe1\u606f\u5b57\u7b26\u4e32 |<\/p>\n<p>\u5e38\u7528\u793a\u4f8b&#xff1a;<\/p>\n<p>&#096;&#096;&#096;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>char s1[] &#061; &#034;hello&#034;;<br \/>\nchar s2[20];<\/p>\n<p>strcpy(s2, s1);                    \/\/ s2 &#061; &#034;hello&#034;<br \/>\nstrcat(s2, &#034; world&#034;);              \/\/ s2 &#061; &#034;hello world&#034;<\/p>\n<p>if (strcmp(s1, &#034;hello&#034;) &#061;&#061; 0) {    \/\/ \u76f8\u7b49<br \/>\n}<\/p>\n<p>char *p &#061; strstr(s2, &#034;wor&#034;);       \/\/ \u6307\u5411 &#034;world&#034; \u5f00\u5934<br \/>\nchar *c &#061; strchr(s2, &#039;o&#039;);         \/\/ \u6307\u5411\u7b2c\u4e00\u4e2a &#039;o&#039;<\/p>\n<p>\/\/ strtok \u5207\u5206<br \/>\nchar s[] &#061; &#034;apple,banana,cherry&#034;;<br \/>\nchar *tok &#061; strtok(s, &#034;,&#034;);<br \/>\nwhile (tok) {<br \/>\n    printf(&#034;%s\\\\n&#034;, tok);           \/\/ apple \/ banana \/ cherry<br \/>\n    tok &#061; strtok(NULL, &#034;,&#034;);<br \/>\n}<br \/>\n&#096;&#096;&#096;<\/p>\n<table>\n<tr><span style=\"color:#0d0016\">\u51fd\u6570<\/span><span style=\"color:#0d0016\">\u8f93\u51fa\u76ee\u6807<\/span><span style=\"color:#0d0016\">\u5b89\u5168\u7248\u672c<\/span><span style=\"color:#0d0016\">\u8bf4\u660e<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">printf<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6807\u51c6\u8f93\u51fa<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u2014<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6700\u5e38\u7528<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">fprintf<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6307\u5b9a\u6587\u4ef6\u6d41<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u2014<\/span><\/td>\n<td><span style=\"color:#0d0016\">fprintf(stderr, &#8230;) \u6253\u9519\u8bef<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">sprintf<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5b57\u7b26\u7f13\u51b2\u533a<\/span><\/td>\n<td><span style=\"color:#0d0016\">sprintf_s<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4e0d\u68c0\u67e5\u5bb9\u91cf&#xff0c;\u4f1a\u6ea2\u51fa&#xff0c;\u614e\u7528<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">snprintf<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5b57\u7b26\u7f13\u51b2\u533a&#xff08;\u9650\u957f&#xff09;<\/span><\/td>\n<td><span style=\"color:#0d0016\">snprintf_s<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u63a8\u8350&#xff0c;\u5b89\u5168<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">asprintf<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u81ea\u52a8\u5206\u914d\u7f13\u51b2\u533a<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u2014<\/span><\/td>\n<td><span style=\"color:#0d0016\">GNU \u6269\u5c55&#xff0c;\u81ea\u52a8 malloc&#xff0c;\u9700 free<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">dprintf<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6587\u4ef6\u63cf\u8ff0\u7b26 fd<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u2014<\/span><\/td>\n<td><span style=\"color:#0d0016\">POSIX&#xff0c;\u76f4\u63a5\u5199 fd<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">sprintf_s<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5b57\u7b26\u7f13\u51b2\u533a<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u2014<\/span><\/td>\n<td><span style=\"color:#0d0016\">MSVC \u5b89\u5168\u7248&#xff0c;\u9a8c\u8bc1\u53c2\u6570<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">snprintf_s<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u5b57\u7b26\u7f13\u51b2\u533a&#xff08;\u9650\u957f&#xff09;<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u2014<\/span><\/td>\n<td><span style=\"color:#0d0016\">MSVC \u5b89\u5168\u7248<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">vprintf<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6807\u51c6\u8f93\u51fa<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u2014<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u63a5\u53d7 va_list<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">vfprintf<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6587\u4ef6\u6d41<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u2014<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u63a5\u53d7 va_list<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">vsprintf<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u7f13\u51b2\u533a<\/span><\/td>\n<td><span style=\"color:#0d0016\">vsprintf_s<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4e0d\u68c0\u67e5\u5bb9\u91cf<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">vsnprintf<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u7f13\u51b2\u533a&#xff08;\u9650\u957f&#xff09;<\/span><\/td>\n<td><span style=\"color:#0d0016\">vsnprintf_s<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u63a5\u53d7 va_list \u7684 snprintf<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span style=\"color:#1a439c\">\u7248\u672c\u4e00&#xff1a;C\u8bed\u8a00<\/span><\/h2>\n<p>#include &#034;myshell.h&#034;<\/p>\n<p>#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<\/p>\n<p>\/\/ \u63d0\u793a\u7b26(\u771f\u7684\u662f\u5b57\u7b26\u4e32)\u76f8\u5173<br \/>\nstatic char username[32];<br \/>\nstatic char hostname[64];<br \/>\nstatic char cwd[256];<br \/>\nstatic char commandline[256];<\/p>\n<p>\/\/ \u547d\u4ee4\u884c\u76f8\u5173<br \/>\nstatic char *argv[64];  \/\/ \u4e00\u822c\u6700\u591a\u4e5f\u5c31\u5341\u51e0\u4e2a<br \/>\nstatic int argc &#061; 0;<br \/>\nstatic const char *sep &#061; &#034; &#034;;<\/p>\n<p>\/\/ \u4e0e\u9000\u51fa\u7801\u6709\u5173<br \/>\nstatic int lastcode;<\/p>\n<p>\/\/ \u4e0e\u73af\u5883\u53d8\u91cf\u6709\u5173&#xff0c;\u5e94\u8be5\u7531bash\u6765\u7ef4\u62a4&#xff0c;\u4ece\u7cfb\u7edf\u914d\u7f6e\u6587\u4ef6\u8bfb&#xff0c;\u76f4\u63a5\u4ece\u7cfb\u7edfbash\u62f7\u8d1d\u5373\u53ef<br \/>\nstatic char *env[64];<\/p>\n<p>static void GetUserName()<br \/>\n{<br \/>\n    char *_username &#061; getenv(&#034;USER&#034;);   \/\/ \u83b7\u53d6\u73af\u5883\u53d8\u91cf<br \/>\n    strcpy(username,(_username ? _username : &#034;None&#034;));<br \/>\n}<\/p>\n<p>static void GetHostName()<br \/>\n{<br \/>\n    char *_hostname &#061; getenv(&#034;HOSTNAME&#034;);   \/\/ \u83b7\u53d6\u73af\u5883\u53d8\u91cf<br \/>\n    strcpy(hostname,(_hostname ? _hostname : &#034;None&#034;));<br \/>\n}<\/p>\n<p>\/\/static void GetCwdName()    \/\/ \u8981\u8be6\u7ec6\u5904\u7406&#xff0c;\u5305\u62ec\u53ea\u4fdd\u7559&#034;\/&#034;\u4e4b\u540e\u7684\u8def\u5f84<br \/>\n\/\/{<br \/>\n\/\/    char *_cwd &#061; getenv(&#034;PWD&#034;);   \/\/ \u83b7\u53d6\u73af\u5883\u53d8\u91cf<br \/>\n\/\/    strcpy(cwd,(_cwd ? _cwd : &#034;None&#034;));<br \/>\n\/\/}<\/p>\n<p>static void GetCwdName()    \/\/ \u8981\u8be6\u7ec6\u5904\u7406&#xff0c;\u5305\u62ec\u53ea\u4fdd\u7559&#034;\/&#034;\u4e4b\u540e\u7684\u8def\u5f84<br \/>\n{<br \/>\n    char _cwd[256];<br \/>\n    getcwd(_cwd,sizeof(_cwd));<br \/>\n    if(strcmp(_cwd,&#034;\/&#034;) &#061;&#061; 0)<br \/>\n    {<br \/>\n        strcpy(cwd,_cwd);<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        int end &#061; strlen(_cwd) &#8211; 1;<br \/>\n        while(end &gt;&#061; 0)<br \/>\n        {<br \/>\n            if(_cwd[end] &#061;&#061; &#039;\/&#039;)<br \/>\n            {<br \/>\n                \/\/ \u5982 \/home\/Alice<br \/>\n                strcpy(cwd,&amp;_cwd[end &#043; 1]);<br \/>\n                break;<br \/>\n            }<br \/>\n            end&#8211;;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>void PrintPrompt()<br \/>\n{<br \/>\n    GetUserName();<br \/>\n    GetHostName();<br \/>\n    GetCwdName();<br \/>\n    printf(&#034;[%s&#064;%s %s]# &#034;,username,hostname,cwd);<br \/>\n    fflush(stdout); \/\/ \u76f4\u63a5\u5237\u65b0\u7f13\u51b2\u533a<br \/>\n}<\/p>\n<p>static void GetCommandLine()<br \/>\n{<br \/>\n    if(fgets(commandline,sizeof(commandline),stdin) !&#061; NULL)<br \/>\n    {<br \/>\n        \/\/ \u6bd4\u5982&#034;abcd\\\\n&#034; -&gt; &#034;abcd&#034;<br \/>\n        commandline[strlen(commandline) &#8211; 1] &#061; 0;<br \/>\n        \/\/ printf(&#034;debug:%s\\\\n&#034;,commandline);<br \/>\n    }<br \/>\n}<\/p>\n<p>static void ParseCommandLine()<br \/>\n{<br \/>\n    \/\/ \u6e05\u7a7a<br \/>\n    argc &#061; 0;<br \/>\n    memset(argv,0,sizeof(argv));<br \/>\n    \/\/ \u5224\u7a7a<br \/>\n    if(strlen(commandline) &#061;&#061; 0)<br \/>\n        return;<br \/>\n    \/\/ \u89e3\u6790 ls -a -l<br \/>\n    argv[argc] &#061; strtok(commandline,sep);   \/\/ strtok\u5206\u5272\u5b57\u7b26\u4e32&#xff0c;\u53d8\u6210\u51e0\u4e2a\u5b50\u4e32<br \/>\n    while((argv[&#043;&#043;argc] &#061; strtok(NULL,sep)));   \/\/ argc\u7684\u5927\u5c0f\u5373\u5b50\u4e32\u4e2a\u6570<\/p>\n<p>\/\/ \/\/ \u8c03\u8bd5\u4fe1\u606f\u6ce8\u91ca\u6389<br \/>\n\/\/    printf(&#034;argc:%d\\\\n&#034;,argc);<br \/>\n\/\/    int i &#061; 0;<br \/>\n\/\/    for(;argv[i];i&#043;&#043;)<br \/>\n\/\/    {<br \/>\n\/\/        printf(&#034;argv[%d]:%s\\\\n&#034;,i,argv[i]);<br \/>\n\/\/    }<br \/>\n}<\/p>\n<p>\/\/ \u521b\u5efa\u5b50\u8fdb\u7a0b<br \/>\nvoid Execute()<br \/>\n{<br \/>\n    pid_t id &#061; fork();<br \/>\n    if(id &lt; 0)<br \/>\n    {<br \/>\n        perror(&#034;fork&#034;);<br \/>\n        return;<br \/>\n    }<br \/>\n    else if(id &#061;&#061; 0)<br \/>\n    {<br \/>\n        \/\/ printf(&#034;child running..\\\\n&#034;);<br \/>\n        \/\/ \u5b50\u8fdb\u7a0b<br \/>\n        \/\/ \u7a0b\u5e8f\u66ff\u6362<br \/>\n        execvp(argv[0],argv);<br \/>\n        exit(1);<br \/>\n    }<br \/>\n    else{<br \/>\n        \/\/ \u7236\u8fdb\u7a0b<br \/>\n        int status &#061; 0;<br \/>\n        pid_t rid &#061; waitpid(id,&amp;status,0);  \/\/ \u5f97\u5230\u72b6\u6001<br \/>\n        (void)rid;<br \/>\n        lastcode &#061; WEXITSTATUS(status);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ 1&#xff1a;yes<br \/>\n\/\/ 0&#xff1a;no<br \/>\nint CheckBuiltinAndExcute()<br \/>\n{<br \/>\n    int ret &#061; 0;<br \/>\n    if(strcmp(argv[0],&#034;cd&#034;) &#061;&#061; 0)<br \/>\n    {<br \/>\n        \/\/ \u5185\u5efa\u547d\u4ee4<br \/>\n        ret &#061; 1;<br \/>\n        if(argc &#061;&#061; 2)   \/\/ \u6709\u9009\u9879<br \/>\n        {<br \/>\n            chdir(argv[1]);<br \/>\n        }<br \/>\n    }<br \/>\n    else if(strcmp(argv[0],&#034;echo&#034;) &#061;&#061; 0)<br \/>\n    {<br \/>\n        ret &#061; 1;<br \/>\n        if(argc &#061;&#061; 2)<br \/>\n        {<br \/>\n            \/\/ echo $?<br \/>\n            \/\/ echo $PATH<br \/>\n            \/\/ echo &#034;helloworld&#034;<br \/>\n            \/\/ echo helloworld<br \/>\n            if(argv[1][0] &#061;&#061; &#039;$&#039;)<br \/>\n            {<br \/>\n                if(strcmp(argv[1],&#034;$?&#034;) &#061;&#061; 0)<br \/>\n                {<br \/>\n                    printf(&#034;%d\\\\n&#034;,lastcode);<br \/>\n                    lastcode &#061; 0;   \/\/ \u8fd4\u56de\u4e0a\u4e00\u6b21\u7684\u9000\u51fa\u7801<br \/>\n                }<br \/>\n                else<br \/>\n                {<br \/>\n                    \/\/ env&#xff1a;\u73af\u5883\u53d8\u91cf<br \/>\n                }<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                printf(&#034;%s\\\\n&#034;,argv[1]); \/\/ \u6253\u5370\u5b57\u7b26\u4e32&#xff0c;\u4e0d\u7ec6\u5206\u4ec0\u4e48&#034;&#034;\u6216\u8005\u6ca1\u6709&#034;&#034;\u4e86<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return ret;<br \/>\n}<\/p>\n<p>void Bash()<br \/>\n{<br \/>\n    while(1)    \/\/ \u8f6f\u4ef6\u90fd\u662f\u6b7b\u5faa\u73af\u7684<br \/>\n    {<br \/>\n        \/\/ \u7b2c1\u6b65&#xff1a;\u8f93\u51fa\u547d\u4ee4\u884c<br \/>\n        PrintPrompt();<\/p>\n<p>        \/\/ \u7b2c2\u6b65&#xff1a;\u7b49\u5f85\u7528\u6237\u8f93\u5165&#xff0c;\u83b7\u53d6\u7528\u6237\u8f93\u5165<br \/>\n        \/\/ char commandline[256]; -&gt; scanf()<br \/>\n        \/\/ sleep(1);   \/\/ \u7b49\u5f85\u4e00\u4f1a\u513f<br \/>\n        GetCommandLine();<\/p>\n<p>        \/\/ \u7b2c3\u6b65&#xff1a;\u89e3\u6790\u5b57\u7b26\u4e32&#xff0c;&#034;ls -a -l&#034; -&gt; &#034;ls&#034; &#034;-a&#034; &#034;-l&#034;<br \/>\n        ParseCommandLine();<br \/>\n        if(argc &#061;&#061; 0)<br \/>\n            continue;<\/p>\n<p>        \/\/ \u7b2c4\u6b65&#xff1a;\u6709\u4e9b\u547d\u4ee4&#xff0c;cd\u3001ench\u3001env\u3001export\u7b49\u547d\u4ee4&#xff0c;\u4e0d\u5e94\u8be5\u8ba9\u5b50\u8fdb\u7a0b\u6267\u884c&#xff0c;\u800c\u5e94\u8be5\u8ba9\u7236\u8fdb\u7a0b\u81ea\u5df1\u6267\u884c&#xff0c;\u5185\u5efa\u547d\u4ee4\u3002bash\u5185\u90e8\u7684\u51fd\u6570<br \/>\n        if(CheckBuiltinAndExcute())<br \/>\n        {<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        \/\/ \u7b2c5\u6b65&#xff1a;\u6267\u884c\u547d\u4ee4<br \/>\n        Execute();<br \/>\n    }<br \/>\n}<\/p>\n<h2><span style=\"color:#1a439c\">\u7248\u672c\u4e8c&#xff1a;C&#043;&#043;<\/span><\/h2>\n<p>\/******************************************************************************<br \/>\n * mini_shell.cpp<br \/>\n * \u4e00\u4e2a\u6700\u5c0f\u53ef\u7528\u7684 Linux Shell&#xff1a;<br \/>\n *   &#8211; \u652f\u6301\u666e\u901a\u5916\u90e8\u547d\u4ee4&#xff08;ls \/ ps \/ whoami \/ pwd &#8230;&#xff09;<br \/>\n *   &#8211; \u652f\u6301\u5185\u5efa\u547d\u4ee4&#xff1a;cd \/ export \/ env \/ echo $? \/ exit<br \/>\n *   &#8211; \u652f\u6301\u73af\u5883\u53d8\u91cf\u7ee7\u627f\u4e0e\u7ef4\u62a4<br \/>\n *   &#8211; \u652f\u6301 echo $? \u6253\u5370\u4e0a\u4e00\u6761\u547d\u4ee4\u9000\u51fa\u7801<br \/>\n * \u7f16\u8bd1&#xff1a;g&#043;&#043; -o mini_shell mini_shell.cpp -std&#061;c&#043;&#043;11<br \/>\n * \u8fd0\u884c&#xff1a;.\/mini_shell<br \/>\n *****************************************************************************\/<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;cstdio&gt;<br \/>\n#include &lt;cstdlib&gt;<br \/>\n#include &lt;cstring&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/wait.h&gt;<br \/>\n#include &lt;ctype.h&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/* &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u5168\u5c40\u914d\u7f6e &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; *\/<br \/>\nconst int basesize &#061; 1024;   \/\/ \u547d\u4ee4\u884c\u7f13\u51b2\u533a\u5927\u5c0f<br \/>\nconst int argvnum  &#061; 64;     \/\/ argv \u6700\u591a\u53c2\u6570\u4e2a\u6570<br \/>\nconst int envnum   &#061; 64;     \/\/ \u73af\u5883\u53d8\u91cf\u6700\u591a\u4e2a\u6570<\/p>\n<p>\/* &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u5168\u5c40\u72b6\u6001 &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; *\/<br \/>\nstatic char *gargv[argvnum]; \/\/ \u547d\u4ee4\u884c\u53c2\u6570\u8868&#xff0c;\u672b\u5c3e\u5fc5\u987b\u4e3a NULL<br \/>\nstatic int   gargc &#061; 0;      \/\/ \u53c2\u6570\u4e2a\u6570<\/p>\n<p>static int lastcode &#061; 0;     \/\/ \u4e0a\u4e00\u6761\u547d\u4ee4\u7684\u9000\u51fa\u7801&#xff08;echo $? \u7528&#xff09;<\/p>\n<p>static char *genv[envnum];   \/\/ shell \u81ea\u5df1\u7ef4\u62a4\u7684\u73af\u5883\u53d8\u91cf\u8868<\/p>\n<p>static char pwd[basesize];   \/\/ \u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<br \/>\nstatic char pwdenv[basesize];\/\/ &#034;PWD&#061;xxx&#034; \u5b57\u7b26\u4e32<\/p>\n<p>\/* &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u63d0\u793a\u7b26\u76f8\u5173 &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; *\/<br \/>\nstatic string GetUserName() {<br \/>\n    const char *name &#061; getenv(&#034;USER&#034;);<br \/>\n    return name ? name : &#034;None&#034;;<br \/>\n}<\/p>\n<p>static string GetHostName() {<br \/>\n    const char *host &#061; getenv(&#034;HOSTNAME&#034;);<br \/>\n    return host ? host : &#034;None&#034;;<br \/>\n}<\/p>\n<p>static string GetPwd() {<br \/>\n    if (getcwd(pwd, sizeof(pwd)) &#061;&#061; NULL) return &#034;None&#034;;<br \/>\n    snprintf(pwdenv, sizeof(pwdenv), &#034;PWD&#061;%s&#034;, pwd);<br \/>\n    putenv(pwdenv);<br \/>\n    return pwd;<br \/>\n}<\/p>\n<p>static string LastDir() {<br \/>\n    string curr &#061; GetPwd();<br \/>\n    if (curr &#061;&#061; &#034;\/&#034; || curr &#061;&#061; &#034;None&#034;) return curr;<br \/>\n    size_t pos &#061; curr.rfind(&#034;\/&#034;);<br \/>\n    if (pos &#061;&#061; string::npos) return curr;<br \/>\n    return curr.substr(pos &#043; 1);<br \/>\n}<\/p>\n<p>static string MakeCommandLine() {<br \/>\n    char line[basesize];<br \/>\n    snprintf(line, basesize, &#034;[%s&#064;%s %s]# &#034;,<br \/>\n             GetUserName().c_str(),<br \/>\n             GetHostName().c_str(),<br \/>\n             LastDir().c_str());<br \/>\n    return line;<br \/>\n}<\/p>\n<p>static void PrintCommandLine() {<br \/>\n    printf(&#034;%s&#034;, MakeCommandLine().c_str());<br \/>\n    fflush(stdout);     \/\/ \u5fc5\u987b fflush&#xff0c;\u56e0\u4e3a\u63d0\u793a\u7b26\u6ca1 \\\\n<br \/>\n}<\/p>\n<p>\/* &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u8bfb\u547d\u4ee4\u884c &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; *\/<br \/>\nstatic bool GetCommandLine(char buf[], int size) {<br \/>\n    if (fgets(buf, size, stdin) &#061;&#061; NULL) {<br \/>\n        printf(&#034;\\\\n&#034;);          \/\/ Ctrl&#043;D \u9000\u51fa<br \/>\n        return false;<br \/>\n    }<br \/>\n    buf[strlen(buf) &#8211; 1] &#061; &#039;\\\\0&#039;;   \/\/ \u53bb\u6389 fgets \u8bfb\u8fdb\u6765\u7684 \\\\n<br \/>\n    if (strlen(buf) &#061;&#061; 0) return false;<br \/>\n    return true;<br \/>\n}<\/p>\n<p>\/* &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u89e3\u6790\u547d\u4ee4\u884c &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; *\/<br \/>\nstatic void ParseCommandLine(char buf[]) {<br \/>\n    memset(gargv, 0, sizeof(gargv));<br \/>\n    gargc &#061; 0;<br \/>\n    const char *sep &#061; &#034; &#034;;<br \/>\n    gargv[gargc&#043;&#043;] &#061; strtok(buf, sep);<br \/>\n    while ((gargv[gargc&#043;&#043;] &#061; strtok(NULL, sep)) !&#061; NULL)<br \/>\n        ;<br \/>\n    gargc&#8211;;<br \/>\n}<\/p>\n<p>\/* &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u73af\u5883\u53d8\u91cf\u64cd\u4f5c &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; *\/<br \/>\nstatic void InitEnv() {<br \/>\n    extern char **environ;<br \/>\n    int i &#061; 0;<br \/>\n    for (; environ[i] &amp;&amp; i &lt; envnum &#8211; 1; i&#043;&#043;) {<br \/>\n        genv[i] &#061; (char *)malloc(strlen(environ[i]) &#043; 1);<br \/>\n        strcpy(genv[i], environ[i]);<br \/>\n    }<br \/>\n    genv[i] &#061; NULL;<br \/>\n}<\/p>\n<p>static void AddEnv(const char *item) {<br \/>\n    int i &#061; 0;<br \/>\n    while (genv[i]) i&#043;&#043;;<br \/>\n    genv[i] &#061; (char *)malloc(strlen(item) &#043; 1);<br \/>\n    strcpy(genv[i], item);<br \/>\n    genv[&#043;&#043;i] &#061; NULL;<br \/>\n}<\/p>\n<p>\/* &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u5916\u90e8\u547d\u4ee4\u6267\u884c &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; *\/<br \/>\nstatic void ExecuteCommand() {<br \/>\n    pid_t id &#061; fork();<br \/>\n    if (id &lt; 0) { perror(&#034;fork&#034;); lastcode &#061; 1; return; }<\/p>\n<p>    if (id &#061;&#061; 0) {<br \/>\n        \/\/ \u5b50\u8fdb\u7a0b&#xff1a;\u66ff\u6362\u6210\u5916\u90e8\u7a0b\u5e8f<br \/>\n        execvpe(gargv[0], gargv, genv);<br \/>\n        perror(gargv[0]);       \/\/ exec \u5931\u8d25\u624d\u4f1a\u8d70\u5230\u8fd9<br \/>\n        exit(127);<br \/>\n    }<\/p>\n<p>    \/\/ \u7236\u8fdb\u7a0b&#xff1a;\u963b\u585e\u7b49\u5b50\u8fdb\u7a0b<br \/>\n    int status &#061; 0;<br \/>\n    waitpid(id, &amp;status, 0);<\/p>\n<p>    if (WIFEXITED(status))<br \/>\n        lastcode &#061; WEXITSTATUS(status);<br \/>\n    else if (WIFSIGNALED(status))<br \/>\n        lastcode &#061; 128 &#043; WTERMSIG(status);   \/\/ \u88ab\u4fe1\u53f7\u6740<br \/>\n    else<br \/>\n        lastcode &#061; 100;<br \/>\n}<\/p>\n<p>\/* &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u5185\u5efa\u547d\u4ee4 &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; *\/<br \/>\nstatic bool CheckAndExecBuiltCommand() {<br \/>\n    \/* exit&#xff1a;\u9000\u51fa shell *\/<br \/>\n    if (strcmp(gargv[0], &#034;exit&#034;) &#061;&#061; 0) { printf(&#034;bye~\\\\n&#034;); exit(0); }<\/p>\n<p>    \/* cd&#xff1a;\u6539 shell \u81ea\u5df1\u7684\u5de5\u4f5c\u76ee\u5f55 *\/<br \/>\n    if (strcmp(gargv[0], &#034;cd&#034;) &#061;&#061; 0) {<br \/>\n        if (gargc &#061;&#061; 2) {<br \/>\n            if (chdir(gargv[1]) !&#061; 0) perror(&#034;cd&#034;);<br \/>\n        } else {<br \/>\n            chdir(getenv(&#034;HOME&#034;) ? getenv(&#034;HOME&#034;) : &#034;\/&#034;);<br \/>\n        }<br \/>\n        lastcode &#061; 0;<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    \/* export KEY&#061;VALUE *\/<br \/>\n    if (strcmp(gargv[0], &#034;export&#034;) &#061;&#061; 0) {<br \/>\n        if (gargc &#061;&#061; 2) { AddEnv(gargv[1]); lastcode &#061; 0; }<br \/>\n        else lastcode &#061; 1;<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    \/* env&#xff1a;\u5217\u73af\u5883\u53d8\u91cf *\/<br \/>\n    if (strcmp(gargv[0], &#034;env&#034;) &#061;&#061; 0) {<br \/>\n        for (int i &#061; 0; genv[i]; i&#043;&#043;) printf(&#034;%s\\\\n&#034;, genv[i]);<br \/>\n        lastcode &#061; 0;<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    \/* echo&#xff1a;\u652f\u6301 echo $? \/ echo xxx *\/<br \/>\n    if (strcmp(gargv[0], &#034;echo&#034;) &#061;&#061; 0) {<br \/>\n        if (gargc &#061;&#061; 2) {<br \/>\n            if (gargv[1][0] &#061;&#061; &#039;$&#039; &amp;&amp; gargv[1][1] &#061;&#061; &#039;?&#039;)<br \/>\n                printf(&#034;%d\\\\n&#034;, lastcode);<br \/>\n            else<br \/>\n                printf(&#034;%s\\\\n&#034;, gargv[1]);<br \/>\n        } else {<br \/>\n            printf(&#034;\\\\n&#034;);<br \/>\n        }<br \/>\n        lastcode &#061; 0;<br \/>\n        return true;<br \/>\n    }<\/p>\n<p>    return false;   \/\/ \u4e0d\u662f\u5185\u5efa&#xff0c;\u53bb fork<br \/>\n}<\/p>\n<p>\/* &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u4e3b\u5faa\u73af &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; *\/<br \/>\nint main() {<br \/>\n    InitEnv();<br \/>\n    char cmd[basesize];<\/p>\n<p>    while (true) {<br \/>\n        PrintCommandLine();<br \/>\n        if (!GetCommandLine(cmd, basesize)) continue;<br \/>\n        ParseCommandLine(cmd);<br \/>\n        if (CheckAndExecBuiltCommand()) continue;<br \/>\n        ExecuteCommand();<br \/>\n    }<br \/>\n    return 0;<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0WIFEXITED \u662f &lt;sys\/wait.h&gt; \u4e2d\u7684\u5b8f&#xff0c;\u5224\u65ad\u5b50\u8fdb\u7a0b\u662f\u5426\u6b63\u5e38\u9000\u51fa&#xff08;\u901a\u8fc7 exit() \u6216 return&#xff09;&#xff0c;\u914d\u5408 wait() \/ waitpid() \u4f7f\u7528&#xff1a;<\/span><\/p>\n<p>#include &lt;sys\/wait.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;stdio.h&gt;<\/p>\n<p>pid_t pid &#061; fork();<br \/>\nif (pid &#061;&#061; 0) {<br \/>\n    exit(42);                    \/\/ \u5b50\u8fdb\u7a0b\u6b63\u5e38\u9000\u51fa<br \/>\n}<\/p>\n<p>int status;<br \/>\nwaitpid(pid, &amp;status, 0);<\/p>\n<p>if (WIFEXITED(status)) {<br \/>\n    printf(&#034;\u6b63\u5e38\u9000\u51fa&#xff0c;\u9000\u51fa\u7801 &#061; %d\\\\n&#034;, WEXITSTATUS(status));  \/\/ 42<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">wait \u72b6\u6001\u5b8f\u5168\u5bb6\u6876&#xff1a;<\/span><\/p>\n<table>\n<tr><span style=\"color:#0d0016\">\u5b8f<\/span><span style=\"color:#0d0016\">\u4f5c\u7528<\/span><\/tr>\n<tbody>\n<tr>\n<td><span style=\"color:#0d0016\">WIFEXITED(status)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u662f\u5426\u6b63\u5e38\u9000\u51fa&#xff08;exit\/return&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">WEXITSTATUS(status)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6b63\u5e38\u9000\u51fa\u65f6\u7684\u9000\u51fa\u7801&#xff08;\u4ec5 WIFEXITED \u4e3a\u771f\u65f6\u7528&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">WIFSIGNALED(status)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u662f\u5426\u88ab\u4fe1\u53f7\u6740\u6b7b<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">WTERMSIG(status)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u6740\u6b7b\u5b83\u7684\u4fe1\u53f7\u7f16\u53f7&#xff08;\u4ec5 WIFSIGNALED \u4e3a\u771f\u65f6\u7528&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">WCOREDUMP(status)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u662f\u5426\u4ea7\u751f\u6838\u5fc3\u8f6c\u50a8&#xff08;\u975e POSIX \u6807\u51c6&#xff0c;\u90e8\u5206\u7cfb\u7edf\u6709&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">WIFSTOPPED(status)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u662f\u5426\u56e0\u4fe1\u53f7\u6682\u505c&#xff08;\u914d\u5408 WUNTRACED&#xff09;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">WSTOPSIG(status)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u4f7f\u5176\u6682\u505c\u7684\u4fe1\u53f7\u7f16\u53f7<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color:#0d0016\">WIFCONTINUED(status)<\/span><\/td>\n<td><span style=\"color:#0d0016\">\u662f\u5426\u88ab SIGCONT \u6062\u590d&#xff08;\u914d\u5408 WCONTINUED&#xff09;<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color:#0d0016\">\u5b8c\u6574\u5224\u65ad\u6a21\u677f&#xff1a;<\/span><\/p>\n<p>if (WIFEXITED(status)) {<br \/>\n    printf(&#034;\u6b63\u5e38\u9000\u51fa&#xff0c;\u7801 %d\\\\n&#034;, WEXITSTATUS(status));<br \/>\n} else if (WIFSIGNALED(status)) {<br \/>\n    printf(&#034;\u88ab\u4fe1\u53f7 %d \u6740\u6b7b\\\\n&#034;, WTERMSIG(status));<br \/>\n} else if (WIFSTOPPED(status)) {<br \/>\n    printf(&#034;\u88ab\u4fe1\u53f7 %d \u6682\u505c\\\\n&#034;, WSTOPSIG(status));<br \/>\n}<\/p>\n<p><span style=\"color:#0d0016\">\u6838\u5fc3\u70b9&#xff1a;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0 \u00a0status \u662f\u6253\u5305\u7684\u4f4d\u5b57\u6bb5&#xff0c;\u4e0d\u80fd\u76f4\u63a5\u6bd4\u8f83&#xff0c;\u5fc5\u987b\u7528\u5b8f\u89e3\u5305\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5224\u65ad &#034;\u662f\u5426\u6b63\u5e38\u9000\u51fa&#034; \u770b WIFEXITED&#xff0c;\u53d6\u9000\u51fa\u7801\u770b WEXITSTATUS&#xff0c;\u4e24\u8005\u901a\u5e38\u6210\u5bf9\u51fa\u73b0\u3002<\/span><\/p>\n<\/p>\n<hr \/>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1292\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261021001724-6ad80494504ed.png\" width=\"2558\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content<\/p>\n","protected":false},"author":2,"featured_media":116356,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[11820,58,190,43,44],"topic":[],"class_list":["post-116359","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-atomgit","tag-linux","tag-190","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010Linux\u3011 \u81ea\u4e3bshell\u7f16\u5199\u5de9\u56fa\u590d\u4e60\u7bc7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/116359.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Linux\u3011 \u81ea\u4e3bshell\u7f16\u5199\u5de9\u56fa\u590d\u4e60\u7bc7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/116359.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-21T00:17:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261021001722-6ad804927d5a1.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/116359.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/116359.html\",\"name\":\"\u3010Linux\u3011 \u81ea\u4e3bshell\u7f16\u5199\u5de9\u56fa\u590d\u4e60\u7bc7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-21T00:17:27+00:00\",\"dateModified\":\"2026-10-21T00:17:27+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/116359.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/116359.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/116359.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Linux\u3011 \u81ea\u4e3bshell\u7f16\u5199\u5de9\u56fa\u590d\u4e60\u7bc7\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010Linux\u3011 \u81ea\u4e3bshell\u7f16\u5199\u5de9\u56fa\u590d\u4e60\u7bc7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/116359.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u3011 \u81ea\u4e3bshell\u7f16\u5199\u5de9\u56fa\u590d\u4e60\u7bc7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\/* MD \/ \u5bcc\u6587\u672c\u4e2d\u7684 .toc\uff08\u542b\u535a\u5ba2\u56ed\u642c\u5bb6\u7b49\u5d4c\u5957\u7ed3\u6784\uff09\uff1b.toc-box \u5728\u4fa7\u680f\uff0c\u4e0d\u53d7\u5f71\u54cd *\/#content_views .toc,\/* \u7f16\u8f91\u5668\u5e38\u5728\u76ee\u5f55\u524d\u540e\u63d2\u5165\u7a7a p\uff08:empty \u4ecd\u5360 20px\uff09\uff0c\u4e00\u5e76\u53bb\u6389\u907f\u514d\u9876\u7a7a\u9699 *\/#content_views.markdown_views &gt; p:empty:has(+ .toc),#content_views.markdown_views &gt; .toc + p:empty,\/* \u5bcc\u6587\u672c\u65e7\u7248\u76ee\u5f55\u6807\u8bb0 *\/#content","og_url":"https:\/\/www.wsisp.com\/helps\/116359.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-21T00:17:27+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/10\/20261021001722-6ad804927d5a1.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"12 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/116359.html","url":"https:\/\/www.wsisp.com\/helps\/116359.html","name":"\u3010Linux\u3011 \u81ea\u4e3bshell\u7f16\u5199\u5de9\u56fa\u590d\u4e60\u7bc7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-21T00:17:27+00:00","dateModified":"2026-10-21T00:17:27+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/116359.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/116359.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/116359.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Linux\u3011 \u81ea\u4e3bshell\u7f16\u5199\u5de9\u56fa\u590d\u4e60\u7bc7"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/116359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=116359"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/116359\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/116356"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=116359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=116359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=116359"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=116359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}