{"id":33080,"date":"2025-04-26T13:30:29","date_gmt":"2025-04-26T05:30:29","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/33080.html"},"modified":"2025-04-26T13:30:29","modified_gmt":"2025-04-26T05:30:29","slug":"%e3%80%90linux%e3%80%91%e8%bf%9b%e7%a8%8b%e6%9b%bf%e6%8d%a2%e4%b8%8e%e8%87%aa%e5%ae%9a%e4%b9%89-shell%ef%bc%9a%e5%8e%9f%e7%90%86%e4%b8%8e%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/33080.html","title":{"rendered":"\u3010Linux\u3011\u8fdb\u7a0b\u66ff\u6362\u4e0e\u81ea\u5b9a\u4e49 Shell\uff1a\u539f\u7406\u4e0e\u5b9e\u6218"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E4%B8%80%E3%80%81%E8%BF%9B%E7%A8%8B%E7%A8%8B%E5%BA%8F%E6%9B%BF%E6%8D%A2-toc\" style=\"margin-left:0px\">\u4e00\u3001\u8fdb\u7a0b\u7a0b\u5e8f\u66ff\u6362<\/p>\n<p id=\"1%E3%80%81%E6%9B%BF%E6%8D%A2%E5%8E%9F%E7%90%86-toc\" style=\"margin-left:40px\">1\u3001\u66ff\u6362\u539f\u7406<\/p>\n<p id=\"2%E3%80%81%E6%9B%BF%E6%8D%A2%E5%87%BD%E6%95%B0-toc\" style=\"margin-left:40px\">2\u3001\u66ff\u6362\u51fd\u6570<\/p>\n<p id=\"%EF%BC%881%EF%BC%89%E5%87%BD%E6%95%B0%E8%A7%A3%E9%87%8A-toc\" style=\"margin-left:80px\">&#xff08;1&#xff09;\u51fd\u6570\u89e3\u91ca<\/p>\n<p id=\"%C2%A0%20%E2%91%A0%20filename%20%2F%20pathname-toc\" style=\"margin-left:120px\">\u00a0 \u2460 filename \/ pathname<\/p>\n<p id=\"%C2%A0%E2%91%A1%20%E5%8F%82%E6%95%B0%E8%A1%A8%E4%BC%A0%E9%80%92-toc\" style=\"margin-left:120px\">\u00a0\u2461 \u53c2\u6570\u8868\u4f20\u9012<\/p>\n<p id=\"%C2%A0%E2%91%A2%20%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E8%A1%A8%E4%BC%A0%E9%80%92-toc\" style=\"margin-left:120px\">\u00a0\u2462 \u73af\u5883\u53d8\u91cf\u8868\u4f20\u9012<\/p>\n<p id=\"%EF%BC%882%EF%BC%89%E5%91%BD%E5%90%8D%E7%90%86%E8%A7%A3-toc\" style=\"margin-left:80px\">&#xff08;2&#xff09;\u547d\u540d\u7406\u89e3<\/p>\n<p id=\"%C2%A0%E4%BA%8C%E3%80%81%E8%87%AA%E5%AE%9A%E4%B9%89shell%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%A7%A3%E9%87%8A%E5%99%A8-toc\" style=\"margin-left:0px\">\u00a0\u4e8c\u3001\u81ea\u5b9a\u4e49shell\u547d\u4ee4\u884c\u89e3\u91ca\u5668<\/p>\n<p id=\"1%E3%80%81%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86-toc\" style=\"margin-left:40px\">1\u3001\u5b9e\u73b0\u539f\u7406<\/p>\n<p id=\"2%E3%80%81%E5%AE%9E%E7%8E%B0%E4%BB%A3%E7%A0%81-toc\" style=\"margin-left:40px\">2\u3001\u5b9e\u73b0\u4ee3\u7801<\/p>\n<p id=\"%EF%BC%881%EF%BC%89%E8%8E%B7%E5%8F%96%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F-toc\" style=\"margin-left:80px\">&#xff08;1&#xff09;\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/p>\n<p id=\"%EF%BC%882%EF%BC%89%E8%BE%93%E5%87%BA%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%8F%90%E7%A4%BA%E7%AC%A6-toc\" style=\"margin-left:80px\">&#xff08;2&#xff09;\u8f93\u51fa\u547d\u4ee4\u884c\u63d0\u793a\u7b26<\/p>\n<p id=\"%EF%BC%883%EF%BC%89%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E8%BE%93%E5%85%A5%E7%9A%84%E5%91%BD%E4%BB%A4-toc\" style=\"margin-left:80px\">&#xff08;3&#xff09;\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u547d\u4ee4<\/p>\n<p id=\"%EF%BC%884%EF%BC%89%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%88%86%E6%9E%90-toc\" style=\"margin-left:80px\">&#xff08;4&#xff09;\u547d\u4ee4\u884c\u5206\u6790<\/p>\n<p id=\"%EF%BC%885%EF%BC%89%E6%A3%80%E6%B5%8B%E5%B9%B6%E6%89%A7%E8%A1%8C%E5%86%85%E9%94%AE%E5%91%BD%E4%BB%A4-toc\" style=\"margin-left:80px\">&#xff08;5&#xff09;\u68c0\u6d4b\u5e76\u6267\u884c\u5185\u952e\u547d\u4ee4<\/p>\n<p id=\"%EF%BC%886%EF%BC%89%E6%89%A7%E8%A1%8C%E5%91%BD%E4%BB%A4-toc\" style=\"margin-left:80px\">&#xff08;6&#xff09;\u6267\u884c\u547d\u4ee4<\/p>\n<p id=\"%EF%BC%887%EF%BC%89%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81-toc\" style=\"margin-left:80px\">&#xff08;7&#xff09;\u5b8c\u6574\u4ee3\u7801<\/p>\n<p id=\"%C2%A0%E4%B8%89%E3%80%81%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%9B%E7%A8%8B%E4%B9%8B%E9%97%B4%E7%9A%84%E7%9B%B8%E4%BC%BC%E6%80%A7-toc\" style=\"margin-left:0px\">\u00a0\u4e09\u3001\u51fd\u6570\u548c\u8fdb\u7a0b\u4e4b\u95f4\u7684\u76f8\u4f3c\u6027<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 style=\"background-color:transparent\">\u4e00\u3001\u8fdb\u7a0b\u7a0b\u5e8f\u66ff\u6362<\/h2>\n<p>fork&#xff08;&#xff09;\u51fd\u6570\u521b\u5efa\u65b0\u7684\u5b50\u8fdb\u7a0b\u540e&#xff0c;\u5b50\u8fdb\u7a0b\u5982\u679c\u60f3\u6267\u884c\u4e00\u4e2a\u5168\u65b0\u7684\u7a0b\u5e8f\u5462&#xff1f;\u8fdb\u7a0b\u7684\u7a0b\u5e8f\u66ff\u6362\u6765\u5b8c\u6210\u8fd9\u4e2a\u529f\u80fd&#xff01;\u7a0b\u5e8f\u66ff\u6362\u662f\u901a\u8fc7\u7279\u5b9a\u7684\u63a5\u53e3&#xff0c;\u52a0\u8f7d\u78c1\u76d8\u4e0a\u7684\u4e00\u4e2a\u5168\u65b0\u7a0b\u5e8f&#xff08;\u4ee3\u7801\u548c\u7a0b\u5e8f&#xff09;&#xff0c;\u52a0\u8f7d\u5230\u8c03\u7528\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\u4e2d&#xff01;<\/p>\n<h3 id=\"1%E3%80%81%E6%9B%BF%E6%8D%A2%E5%8E%9F%E7%90%86\">1\u3001\u66ff\u6362\u539f\u7406<\/h3>\n<p>\u7528 fork \u521b\u5efa\u5b50\u8fdb\u7a0b\u540e\u6267\u884c\u7684\u662f\u548c\u7236\u8fdb\u7a0b\u76f8\u540c\u7684\u7a0b\u5e8f&#xff08;\u4f46\u6709\u53ef\u80fd\u6267\u884c\u4e0d\u540c\u7684\u4ee3\u7801\u5206\u652f&#xff09;&#xff0c;\u5b50\u8fdb\u7a0b\u5f80\u5f80\u8981\u8c03\u7528\u4e00\u79cd exec \u51fd\u6570\u4ee5\u6267\u884c\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u3002\u5f53\u8fdb\u7a0b\u8c03\u7528\u4e00\u79cd exec \u51fd\u6570\u65f6&#xff0c;\u8be5\u8fdb\u7a0b\u7684\u7528\u6237\u7a7a\u95f4\u4ee3\u7801\u548c\u6570\u636e\u5b8c\u5168\u88ab\u65b0\u7a0b\u5e8f\u66ff\u6362&#xff0c;\u4ece\u65b0\u7a0b\u5e8f\u7684\u542f\u52a8\u4f8b\u7a0b\u5f00\u59cb\u6267\u884c\u3002\u8c03\u7528 exec \u5e76\u4e0d\u521b\u5efa\u65b0\u8fdb\u7a0b&#xff0c;\u6240\u4ee5\u8c03\u7528 exec \u524d\u540e\u8be5\u8fdb\u7a0b\u7684ID\u5e76\u672a\u6539\u53d8\u3002exec\u53ea\u662f\u7528\u78c1\u76d8\u4e0a\u7684\u4e00\u4e2a\u65b0\u7a0b\u5e8f\u66ff\u6362\u4e86\u5f53\u524d\u8fdb\u7a0b\u7684\u6b63\u6587\u6bb5\u3001\u6570\u636e\u6bb5\u3001\u5806\u6bb5\u548c\u6808\u6bb5\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"814\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250426053026-680c6f7275656.png\" width=\"2041\" \/><\/p>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>#include&lt;stdio.h&gt;<br \/>\n#include&lt;unistd.h&gt;<\/p>\n<p>int main()<br \/>\n{<br \/>\n    printf(&#034;\u7a0b\u5e8f\u8fd0\u884c&#xff01;\\\\n&#034;);<br \/>\n    execl(&#034;\/usr\/bin\/ls&#034;, &#034;ls&#034;, &#034;-l&#034;, &#034;-a&#034;, NULL);<br \/>\n    printf(&#034;\u7a0b\u5e8f\u8fd0\u884c\u5b8c\u6bd5&#xff01;\\\\n&#034;);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>$ .\/proc # \u539f\u59cb\u4ee3\u7801\u4e2d\u7684\u7b2c\u4e8c\u4e2aprintf\u51fd\u6570\u88ab\u66ff\u6362&#xff0c;\u4e0d\u6267\u884c&#xff01;<br \/>\n\u7a0b\u5e8f\u8fd0\u884c&#xff01;<br \/>\ntotal 32<br \/>\ndrwxrwxr-x  2 zyt zyt  4096 Apr 18 17:13 .<br \/>\ndrwxrwxr-x 16 zyt zyt  4096 Apr 18 17:07 ..<br \/>\n-rw-rw-r&#8211;  1 zyt zyt    58 Apr 18 17:12 Makefile<br \/>\n-rwxrwxr-x  1 zyt zyt 16000 Apr 18 17:13 proc<br \/>\n-rw-rw-r&#8211;  1 zyt zyt   190 Apr 18 17:13 test.c<\/p>\n<p>\u4e00\u65e6\u7a0b\u5e8f\u66ff\u6362\u6210\u529f&#xff0c;\u5c31\u53bb\u6267\u884c\u65b0\u4ee3\u7801\u4e86&#xff0c;\u540e\u7eed\u7684\u539f\u59cb\u4ee3\u7801\u4e5f\u5c31\u4e0d\u5b58\u5728\u4e86&#xff08;\u88ab\u8986\u76d6\u4e86&#xff09;\u3002<\/p>\n<p>\u00a0exec* \u51fd\u6570\u53ea\u6709\u5931\u8d25\u8fd4\u56de\u503c&#xff0c;\u6ca1\u6709\u6210\u529f\u8fd4\u56de\u503c\u3002\u6240\u4ee5\u5bf9\u4e8eexec\u51fd\u6570\u4e0d\u7528\u5bf9\u8fd4\u56de\u503c\u505a\u5224\u65ad\u3002<\/p>\n<h3 id=\"2%E3%80%81%E6%9B%BF%E6%8D%A2%E5%87%BD%E6%95%B0\">2\u3001\u66ff\u6362\u51fd\u6570<\/h3>\n<p>\u25cf \u8fd9\u4e9b\u51fd\u6570\u5982\u679c\u8c03\u7528\u6210\u529f\u5219\u52a0\u8f7d\u65b0\u7684\u7a0b\u5e8f\u4ece\u542f\u52a8\u4ee3\u7801\u5f00\u59cb\u6267\u884c&#xff0c;\u4e0d\u518d\u8fd4\u56de\u3002<\/p>\n<p>\u25cf \u5982\u679c\u8c03\u7528\u51fa\u9519\u5219\u8fd4\u56de-1&#xff0c;\u82e5\u6210\u529f&#xff0c;\u4e0d\u8fd4\u56de\u3002<\/p>\n<p>#include &lt;unistd.h&gt;<br \/>\nint execl(const char *path, const char *arg, &#8230;);<br \/>\nint execlp(const char *file, const char *arg, &#8230;);<br \/>\nint execle(const char *path, const char *arg, &#8230;,char *const envp[]);<br \/>\nint execv(const char *path, char *const argv[]);<br \/>\nint execvp(const char *file, char *const argv[]);<br \/>\nint execve(const char *path, char *const argv[], char *const envp[]);<br \/>\nint fexecve(int fd, char *const argv[], char *const envp[]); <\/p>\n<h4 id=\"%EF%BC%881%EF%BC%89%E5%87%BD%E6%95%B0%E8%A7%A3%E9%87%8A\">&#xff08;1&#xff09;\u51fd\u6570\u89e3\u91ca<\/h4>\n<h5 id=\"%C2%A0%20%E2%91%A0%20filename%20%2F%20pathname\">\u00a0 \u2460 filename \/ pathname<\/h5>\n<p>pathname\u5c31\u662f\u53d6\u3010\u8def\u5f84\u540d&#043;\u7a0b\u5e8f\u3011\u4f5c\u4e3a\u53c2\u6570&#xff1b;filename\u5c31\u662f\u6587\u4ef6\u540d\u4f5c\u4e3a\u53c2\u6570&#xff0c;\u5f53\u6307\u5b9afilename\u4f5c\u4e3a\u53c2\u6570\u65f6&#xff1a;<\/p>\n<p>\u25cf \u5982\u679cfilename\u4e2d\u5305\u542b\u201c\/\u201d&#xff0c;\u5219\u5c06\u5176\u89c6\u4e3a\u8def\u5f84\u540d\u3002<\/p>\n<p>\u25cf \u5426\u5219\u5c31\u6309\u7167PATH\u73af\u5883\u53d8\u91cf&#xff0c;\u5728\u5b83\u6240\u6307\u5b9a\u7684\u5404\u76ee\u5f55\u4e2d\u641c\u7d22\u53ef\u6267\u884c\u6587\u4ef6\u3002\u3010\u4e0d\u77e5\u9053PATH\u73af\u5883\u53d8\u91cf\u7684\u8bdd\u770b\u524d\u6587&#xff1a;\u6df1\u5165\u6d45\u51fa&#xff1a;\u73af\u5883\u53d8\u91cf\u4e0e\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u7684\u6838\u5fc3\u673a\u5236-CSDN\u535a\u5ba2\u3011<\/p>\n<h5 id=\"%C2%A0%E2%91%A1%20%E5%8F%82%E6%95%B0%E8%A1%A8%E4%BC%A0%E9%80%92\">\u00a0\u2461 \u53c2\u6570\u8868\u4f20\u9012<\/h5>\n<p>\u51fd\u6570execl\u3001execlp \u548c execle \u8981\u6c42\u65b0\u7a0b\u5e8f\u7684\u6bcf\u4e00\u4e2a\u547d\u4ee4\u884c\u53c2\u6570\u90fd\u8bf4\u660e\u4e00\u4e2a\u5355\u72ec\u7684\u53c2\u6570\u3002\u8fd9\u79cd\u53c2\u6570\u8868\u4ee5\u7a7a\u6307\u9488\u7ed3\u5c3e\u3002\u5bf9execl\u3001execlp \u548c execle\u4e09\u4e2a\u51fd\u6570\u8868\u793a\u547d\u4ee4\u884c\u53c2\u6570\u7684\u4e00\u822c\u65b9\u6cd5&#xff1a;<\/p>\n<p>char *arg(), char *argl, &#8230;, char *argn, (char *)0 <\/p>\n<p>\u00a0\u8fd9\u79cd\u8bed\u6cd5\u663e\u793a\u7684\u8bf4\u660e\u4e86\u6700\u540e\u4e00\u4e2a\u547d\u4ee4\u884c\u53c2\u6570\u4e4b\u540e\u8ddf\u4e86\u4e00\u4e2a\u7a7a\u6307\u9488\u3002\u5982\u679c\u7528\u5e38\u91cf0\u6765\u8868\u793a\u4e00\u4e2a\u7a7a\u6307\u9488&#xff0c;\u5219\u5fc5\u987b\u5c06\u5b83\u5f3a\u5236\u8f6c\u5316\u6210\u4e3a\u4e00\u4e2a\u6307\u9488&#xff1a;\u5426\u5219\u5b83\u5c06\u88ab\u89e3\u91ca\u6210\u6574\u5f62\u53c2\u6570\u3002\u5982\u679c\u4e00\u4e2a\u6574\u5f62\u6570\u7684\u957f\u5ea6\u4e0echar*\u7684\u957f\u5ea6\u4e0d\u540c&#xff0c;\u90a3\u4e48exec\u51fd\u6570\u7684\u5b9e\u9645\u53c2\u6570\u5c06\u51fa\u9519\u3002<\/p>\n<p>\u5bf9\u4e8e\u5176\u4f59\u76844\u4e2a\u51fd\u6570&#xff0c;\u5219\u5e94\u5148\u6784\u9020\u4e00\u4e2a\u6307\u5411\u5404\u53c2\u6570\u7684\u6307\u9488\u6570\u7ec4&#xff0c;\u7136\u540e\u5c06\u8be5\u6570\u7ec4\u6307\u9488\u5730\u5740\u4f5c\u4e3a\u8fd94\u4e2a\u51fd\u6570\u7684\u53c2\u6570&#xff08;\u5efa\u7acbargv&#xff09;\u3002<\/p>\n<h5 id=\"%C2%A0%E2%91%A2%20%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E8%A1%A8%E4%BC%A0%E9%80%92\">\u00a0\u2462 \u73af\u5883\u53d8\u91cf\u8868\u4f20\u9012<\/h5>\n<p>\u4ee5e\u7ed3\u5c3e\u7684\u4e09\u4e2a\u51fd\u6570&#xff08;execle\u3001execve\u3001fexecve&#xff09;\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u6307\u5411\u73af\u5883\u5b57\u7b26\u4e32\u6307\u9488\u6570\u7ec4\u7684\u6307\u9488\u3002\u5728ISO C\u539f\u578b&#xff08;\u56fd\u9645\u6807\u51c6\u5316\u7ec4\u7ec7\u5236\u5b9a\u7684C\u8bed\u8a00\u6807\u51c6&#xff09;\u4e4b\u524d&#xff0c;execle\u7684\u53c2\u6570\u662f&#xff1a;<\/p>\n<p>char *pathname, char *arg(), &#8230;, char *argn, (char *)0, char *envp[]   <\/p>\n<p>\u4ece\u4e2d\u53ef\u89c1&#xff0c;\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u662f\u6307\u5411\u73af\u5883\u5b57\u7b26\u4e32\u7684\u5404\u5b57\u7b26\u6307\u9488\u6784\u6210\u7684\u6570\u7ec4\u7684\u6307\u9488\u3002\u800c\u5728ISO C\u539f\u578b\u4e2d&#xff0c;\u6240\u6709\u547d\u4ee4\u884c\u53c2\u6570\u3001\u7a7a\u6307\u9488\u548cenvp\u6307\u9488\u90fd\u662f\u7528\u7701\u7565\u53f7&#xff08;&#8230;&#xff09;\u8868\u793a\u3002\u00a0<\/p>\n<p>\u5176\u4ed6\u56db\u4e2a\u51fd\u6570&#xff0c;\u5219\u4f7f\u7528\u8c03\u7528\u8fdb\u7a0b\u4e2d\u7684environ\u53d8\u91cf\u4e3a\u65b0\u7a0b\u5e8f\u590d\u5236\u73b0\u6709\u7684\u73af\u5883\u3002\u901a\u5e38&#xff0c;\u4e00\u4e2a\u8fdb\u7a0b\u5141\u8bb8\u5c06\u5176\u73af\u5883\u4f20\u64ad\u7ed9\u5176\u5b50\u8fdb\u7a0b&#xff0c;\u4f46\u6709\u65f6\u4e5f\u6709\u8fd9\u79cd\u60c5\u51b5&#xff0c;\u8fdb\u7a0b\u60f3\u4e3a\u5b50\u8fdb\u7a0b\u6307\u5b9a\u67d0\u4e00\u4e2a\u786e\u5b9a\u7684\u73af\u5883&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u7528putenv&#xff08;&#xff09;\u51fd\u6570\u3010\u6dfb\u52a0\u6216\u4fee\u6539\u73af\u5883\u53d8\u91cf&#xff0c;\u5b83\u63a5\u53d7\u4e00\u4e2a\u5f62\u5f0f\u4e3a &#034;name&#061;value&#034; \u7684\u5b57\u7b26\u4e32&#xff0c;\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5f53\u524d\u8fdb\u7a0b\u7684\u73af\u5883\u53d8\u91cf\u5217\u8868\u4e2d\u3011&#xff1b;\u6216\u8005\u901a\u8fc7\u7b2c\u4e09\u65b9\u53d8\u91cfenviron\u3002<\/p>\n<h4 id=\"%EF%BC%882%EF%BC%89%E5%91%BD%E5%90%8D%E7%90%86%E8%A7%A3\">&#xff08;2&#xff09;\u547d\u540d\u7406\u89e3<\/h4>\n<p>\u8fd97\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u5f88\u96be\u8bb0\u5fc6\u3002\u51fd\u6570\u540d\u7684\u5b57\u7b26\u4f1a\u7ed9\u6211\u4eec\u4e00\u4e9b\u5e2e\u52a9\u3002<\/p>\n<p>\u25cf \u5b57\u6bcdp\u8868\u793a\u8be5\u51fd\u6570\u53d6filename\u4f5c\u4e3a\u53c2\u6570&#xff0c;\u5e76\u4e14\u7528PATH\u73af\u5883\u53d8\u91cf\u5bfb\u627e\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n<p>\u25cf \u5b57\u6bcdl\u8868\u793a\u8be5\u51fd\u6570\u53d6\u4e00\u4e2a\u53c2\u6570\u8868&#xff0c;\u53c2\u6570\u4ee5\u53ef\u53d8\u53c2\u6570\u5217\u8868\u7684\u5f62\u5f0f\u4f20\u9012\u3002\u00a0<\/p>\n<p>\u25cf \u5b57\u6bcdv\u4e0e\u5b57\u6bcdl\u4e92\u65a5&#xff0c;v\u8868\u793a\u8be5\u51fd\u6570\u53d6\u4e86\u4e00\u4e2aargv[]\u77e2\u91cf&#xff0c;\u53c2\u6570\u4ee5\u5b57\u7b26\u6307\u9488\u6570\u7ec4\u7684\u5f62\u5f0f\u4f20\u9012\u3002<\/p>\n<p>\u25cf \u5b57\u6bcde\u8868\u793a\u8be5\u51fd\u6570\u53d6envp[]\u6570\u7ec4&#xff0c;\u81ea\u5df1\u7ef4\u62a4\u73af\u5883\u53d8\u91cf&#xff0c;\u800c\u4e0d\u4f7f\u7528\u5f53\u524d\u73af\u5883\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"371\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250426053027-680c6f731e34c.png\" width=\"1191\" \/><\/p>\n<p>\u8c03\u7528\u793a\u4f8b\u5982\u4e0b&#xff1a;<\/p>\n<p>#include &lt;unistd.h&gt;<\/p>\n<p>int main() {<br \/>\n    char *const argv[] &#061; {&#034;ps&#034;, &#034;-ef&#034;, NULL};<br \/>\n    char *const envp[] &#061; {&#034;PATH&#061;\/bin:\/usr\/bin&#034;, &#034;TERM&#061;console&#034;, NULL};<\/p>\n<p>    \/\/ \u4f7f\u7528 execl \u6267\u884c \/bin\/ps \u547d\u4ee4&#xff0c;\u9700\u8981\u5b8c\u6574\u8def\u5f84<br \/>\n    execl(&#034;\/bin\/ps&#034;, &#034;ps&#034;, &#034;-ef&#034;, NULL);<\/p>\n<p>    \/\/ \u4f7f\u7528 execlp \u6267\u884c ps \u547d\u4ee4&#xff0c;\u5e26 p \u7684&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u73af\u5883\u53d8\u91cf PATH&#xff0c;\u65e0\u9700\u5199\u5168\u8def\u5f84<br \/>\n    execlp(&#034;ps&#034;, &#034;ps&#034;, &#034;-ef&#034;, NULL);<\/p>\n<p>    \/\/ \u4f7f\u7528 execle \u6267\u884c ps \u547d\u4ee4&#xff0c;\u5e26 e \u7684&#xff0c;\u9700\u8981\u81ea\u5df1\u7ec4\u88c5\u73af\u5883\u53d8\u91cf<br \/>\n    execle(&#034;\/bin\/ps&#034;, &#034;ps&#034;, &#034;-ef&#034;, NULL, envp);<\/p>\n<p>    \/\/ \u4f7f\u7528 execv \u6267\u884c \/bin\/ps \u547d\u4ee4&#xff0c;\u9700\u8981\u5b8c\u6574\u8def\u5f84<br \/>\n    execv(&#034;\/bin\/ps&#034;, argv);<\/p>\n<p>    \/\/ \u4f7f\u7528 execvp \u6267\u884c ps \u547d\u4ee4&#xff0c;\u5e26 p \u7684&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u73af\u5883\u53d8\u91cf PATH&#xff0c;\u65e0\u9700\u5199\u5168\u8def\u5f84<br \/>\n    execvp(&#034;ps&#034;, argv);<\/p>\n<p>    \/\/ \u4f7f\u7528 execve \u6267\u884c \/bin\/ps \u547d\u4ee4&#xff0c;\u5e26 e \u7684&#xff0c;\u9700\u8981\u81ea\u5df1\u7ec4\u88c5\u73af\u5883\u53d8\u91cf<br \/>\n    execve(&#034;\/bin\/ps&#034;, argv, envp);<\/p>\n<p>    exit(0);<br \/>\n} <\/p>\n<p>\u5728\u5f88\u591aUnix\u7684\u5b9e\u73b0\u4e2d&#xff0c;\u8fd97\u4e2a\u51fd\u6570\u4e2d<span style=\"color:#1a439c\"><span style=\"background-color:#eaf4fc\">\u53ea\u6709execve\u662f\u5185\u6838\u7684\u7cfb\u7edf\u8c03\u7528\u3002\u53e6\u59166\u4e2a\u53ea\u662f\u5e93\u51fd\u6570&#xff0c;\u5b83\u4eec\u6700\u7ec8\u90fd\u8981\u8c03\u7528\u8be5\u7cfb\u7edf\u8c03\u7528\u3002<\/span><\/span>\u8fd97\u4e2a\u51fd\u6570\u4e4b\u95f4\u7684\u5173\u7cfb\u5982\u56fe&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"434\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250426053027-680c6f736d89d.png\" width=\"902\" \/><\/p>\n<h2 id=\"%C2%A0%E4%BA%8C%E3%80%81%E8%87%AA%E5%AE%9A%E4%B9%89shell%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%A7%A3%E9%87%8A%E5%99%A8\" style=\"background-color:transparent\">\u00a0\u4e8c\u3001\u81ea\u5b9a\u4e49shell\u547d\u4ee4\u884c\u89e3\u91ca\u5668<\/h2>\n<h3 id=\"1%E3%80%81%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86\">1\u3001\u5b9e\u73b0\u539f\u7406<\/h3>\n<p>\u8003\u8651\u4e0b\u9762\u8fd9\u4e2a\u4e0eshell\u7684\u4e92\u52a8&#xff1a;<\/p>\n<p>[root&#064;localhost epoll]# ls<br \/>\nclient.cpp  readme.md  server.cpp  utility.h<\/p>\n<p>[root&#064;localhost epoll]# ps<br \/>\n  PID TTY      TIME     CMD<br \/>\n 3451 pts\/0    00:00:00 bash<br \/>\n 3514 pts\/0    00:00:00 ps <\/p>\n<p>\u7528\u4e0b\u56fe\u7684\u65f6\u95f4\u8f74\u6765\u8868\u793a\u4e8b\u4ef6\u7684\u53d1\u751f\u6b21\u5e8f\u3002\u5176\u4e2d\u65f6\u95f4\u4ece\u5de6\u5411\u53f3&#xff0c;shell\u7531\u6807\u8bc6\u4e3ash\u7684\u65b9\u5757\u8868\u793a&#xff0c;\u5b83\u968f\u7740\u65f6\u95f4\u7684\u6d41\u901d\u4ece\u5de6\u5411\u53f3\u79fb\u52a8\u3002shell\u4ece\u7528\u6237\u8bfb\u5165\u5b57\u7b26\u4e32\u201cls\u201d\u3002shell\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b&#xff0c;\u7136\u540e\u5728\u90a3\u4e2a\u8fdb\u7a0b\u4e2d\u8fd0\u884cls\u7a0b\u5e8f\u5e76\u7b49\u5f85\u90a3\u4e2a\u8fdb\u7a0b\u7ed3\u675f\u3002\u00a0 \u7136\u540eshell\u8bfb\u53d6\u65b0\u7684\u4e00\u884c\u8f93\u5165&#xff0c;\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b&#xff0c;\u5728\u8fd9\u4e2a\u8fdb\u7a0b\u4e2d\u8fd0\u884c\u7a0b\u5e8f\u5e76\u7b49\u5f85\u8fd9\u4e2a\u8fdb\u7a0b\u7ed3\u675f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"651\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250426053027-680c6f73ad510.png\" width=\"1992\" \/><\/p>\n<p>\u6240\u4ee5&#xff0c;\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49shell\u9700\u8981\u5faa\u73af\u4ee5\u4e0b\u8fc7\u7a0b&#xff1a;<\/p>\n<p>&#xff08;1&#xff09;\u83b7\u53d6\u547d\u4ee4\u884c<\/p>\n<p>&#xff08;2&#xff09;\u89e3\u6790\u547d\u4ee4\u884c<\/p>\n<p>&#xff08;3&#xff09;\u5efa\u7acb\u4e00\u4e2a\u5b50\u8fdb\u7a0b&#xff08;fork&#xff09;<\/p>\n<p>&#xff08;4&#xff09;\u66ff\u6362\u5b50\u8fdb\u7a0b&#xff08;execvp&#xff09;<\/p>\n<p>&#xff08;5&#xff09;\u7236\u8fdb\u7a0b\u7b49\u5f85\u5b50\u8fdb\u7a0b\u9000\u51fa&#xff08;wait&#xff09;<\/p>\n<h3 id=\"2%E3%80%81%E5%AE%9E%E7%8E%B0%E4%BB%A3%E7%A0%81\">2\u3001\u5b9e\u73b0\u4ee3\u7801<\/h3>\n<h4 id=\"%EF%BC%881%EF%BC%89%E8%8E%B7%E5%8F%96%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F\" style=\"background-color:transparent\">&#xff08;1&#xff09;\u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/h4>\n<p>\u8981\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49 Shell&#xff0c;\u73af\u5883\u53d8\u91cf\u7684\u5904\u7406\u662f\u5173\u952e\u90e8\u5206\u3002\u6b63\u5e38\u60c5\u51b5shell\u542f\u52a8\u8981\u4ece\u7cfb\u7edf\u4e2d\u83b7\u53d6\u73af\u5883\u53d8\u91cf&#xff0c;\u800c\u6211\u4eec\u81ea\u5b9a\u4e49shell\u7684\u73af\u5883\u53d8\u91cf\u4fe1\u606f\u8981\u4ece\u7236shell\u4e2d\u83b7\u53d6\u3002<\/p>\n<p>\u624b\u52a8\u83b7\u53d6&#xff1a;\u5728 C \u4e2d\u53ef\u4ee5\u901a\u8fc7 extern char **environ \u8bbf\u95ee\u7ee7\u627f\u7684\u73af\u5883\u53d8\u91cf\u3002\u5c06\u7236Shell\u7684\u73af\u5883\u53d8\u91cf\u9010\u6761\u590d\u5236\u5230g_env\u4e2d&#xff0c;\u7136\u540e\u5c06g_env\u4e2d\u7684\u53d8\u91cf\u6dfb\u52a0\u5230\u5f53\u524d\u8fdb\u7a0b\u7684\u73af\u5883\u53d8\u91cf\u8868\u3002\u3010\u6ce8\u610f&#xff1a;&#xff08;\u8fd9\u91cc\u4f7f\u7528\u4e86putenv() \u51fd\u6570&#xff09;\u6b64\u540eg_env[i]\u7684\u5185\u5b58\u7531\u7cfb\u7edf\u7ba1\u7406&#xff0c;\u4e0d\u80fd\u518d\u624b\u52a8free&#xff01;Unix\/Linux\u7cfb\u7edf\u63d0\u4f9b\u7684\u5168\u5c40\u53d8\u91cf&#xff0c;\u6307\u5411\u5f53\u524d\u8fdb\u7a0b\u7684\u73af\u5883\u53d8\u91cf\u8868&#xff0c;\u3011<\/p>\n<p>\/\/ \u73af\u5883\u53d8\u91cf\u8868<br \/>\n#define MAX_ENVS 1024<br \/>\nchar *g_env[MAX_ENVS];<br \/>\nint g_envs &#061; 0;<\/p>\n<p>\/\/ \u4ece\u7236shell1\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u8868&#xff0c;\u672c\u6765\u662f\u8981\u4ece\u914d\u7f6e\u6587\u4ef6\u83b7\u53d6\u7684<br \/>\nvoid InitEnv()<br \/>\n{<br \/>\n    extern char **environ;<br \/>\n    memset(g_env, 0, sizeof(g_env)); \/\/ \u521d\u59cb\u5316<br \/>\n    g_envs &#061; 0;<\/p>\n<p>    \/\/ 1.\u83b7\u53d6\u73af\u5883\u53d8\u91cf<br \/>\n    for(int i &#061; 0; environ[i]; i&#043;&#043;)<br \/>\n    {<br \/>\n        \/\/ (1)\u7533\u8bf7\u7a7a\u95f4<br \/>\n        g_env[i] &#061; (char*)malloc(strlen(environ[i])&#043;1);<br \/>\n        \/\/ (2)\u62f7\u8d1d\u5230\u6211\u4eec\u7684\u73af\u5883\u53d8\u91cf\u8868<br \/>\n        strcpy(g_env[i], environ[i]);<br \/>\n        g_envs&#043;&#043;;<br \/>\n    }<br \/>\n    \/\/ \u6d4b\u8bd5&#xff1a;\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u73af\u5883\u53d8\u91cf<br \/>\n    g_env[g_envs&#043;&#043;] &#061; (char*)&#034;HAHA&#061;for_test&#034;;<br \/>\n    g_env[g_envs] &#061; NULL; \/\/ \u8868\u7684\u6700\u540e\u4e00\u4e2a\u6210\u5458\u5fc5\u987b\u662fNULL<\/p>\n<p>    \/\/ 2.\u83b7\u53d6\u5230\u7684\u73af\u5883\u53d8\u91cf\u5bfc\u5165shell\u7684\u8868\u4e2d<br \/>\n    for(int i &#061; 0; g_env[i]; i&#043;&#043;)<br \/>\n    {<br \/>\n        putenv(g_env[i]);<br \/>\n    }<br \/>\n} <\/p>\n<h4 id=\"%EF%BC%882%EF%BC%89%E8%BE%93%E5%87%BA%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%8F%90%E7%A4%BA%E7%AC%A6\">&#xff08;2&#xff09;\u8f93\u51fa\u547d\u4ee4\u884c\u63d0\u793a\u7b26<\/h4>\n<p>\u63d0\u793a\u7b26\u683c\u5f0f&#xff1a;[\u7528\u6237\u540d&#064;\u4e3b\u673a\u540d:\u5f53\u524d\u76ee\u5f55\u7684\u57fa\u540d]#\u00a0<\/p>\n<p>\u2460 MakeCommandline() \u4e2d\u4f7f\u7528 snprintf() \u683c\u5f0f\u5316\u63d0\u793a\u7b26&#xff1a;\u3010format&#xff1a;\u683c\u5f0f\u5316\u5b57\u7b26\u4e32&#xff0c;\u7528\u4e8e\u6307\u5b9a\u5982\u4f55\u683c\u5f0f\u5316\u540e\u7eed\u7684\u53c2\u6570\u3011\u3010&#8230;&#xff1a;\u53ef\u53d8\u53c2\u6570\u5217\u8868&#xff0c;\u6839\u636e format \u7684\u8981\u6c42\u63d0\u4f9b\u76f8\u5e94\u7684\u503c\u3011<\/p>\n<p>int snprintf(char *str, size_t size, const char *format, &#8230;); <\/p>\n<p>\u2461 PrintCommandline() \u6253\u5370\u547d\u4ee4\u884c\u63d0\u793a\u7b26&#xff1a;fflush\u7528\u6765\u786e\u4fdd\u63d0\u793a\u7b26\u7acb\u5373\u663e\u793a&#xff08;\u5c24\u5176\u5728\u65e0\u6362\u884c\u7b26\u65f6&#xff09;\u3002<\/p>\n<p>#define COMMAND_SIZE 1024<br \/>\n#define HOSTNAME_MAX 1024<br \/>\n#define FORMAT &#034;[%s&#064;%s:%s]# &#034;<\/p>\n<p>\/\/ shell\u5b9a\u4e49\u7684\u5168\u5c40\u53d8\u91cf<\/p>\n<p>char cwd[1019];<br \/>\nchar cwdenv[1024]; <\/p>\n<p>const char *GetUserName()<br \/>\n{<br \/>\n    const char * name &#061; getenv(&#034;USER&#034;);<br \/>\n    return name &#061;&#061; NULL ? &#034;None&#034; : name;<br \/>\n}<\/p>\n<p>const char *GetHostName()<br \/>\n{<br \/>\n    char hostname[HOSTNAME_MAX];<br \/>\n    return (gethostname(hostname, HOSTNAME_MAX) &#061;&#061; -1) ? &#034;None&#034; : hostname;<br \/>\n}<\/p>\n<p>const char *GetPwd()<br \/>\n{<br \/>\n    \/\/const char * pwd &#061; getenv(&#034;PWD&#034;);<br \/>\n    const char *pwd &#061; getcwd(cwd, sizeof(cwd)); \/\/ \u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u83b7\u53d6<br \/>\n    if(pwd !&#061; NULL)<br \/>\n    {   \/\/ \u628a\u81ea\u5df1\u7684\u73af\u5883\u53d8\u91cf\u5bfc\u7ed9\u8fdb\u7a0b<br \/>\n        snprintf(cwdenv, sizeof(cwdenv), &#034;PWD&#061;%s&#034;, cwd);<br \/>\n        putenv(cwdenv);<br \/>\n    }<br \/>\n    return pwd &#061;&#061; NULL ? &#034;None&#034; : pwd;<br \/>\n}<\/p>\n<p>\/\/ \u5bf9\u8def\u5f84\u505a\u5305\u88c5&#xff0c;\u53ea\u663e\u793a\u5f53\u524d\u4f4d\u7f6e<br \/>\nstd::string DirName(const char *pwd)<br \/>\n{<br \/>\n#define SLASH &#034;\/&#034;<br \/>\n    std::string dir &#061; pwd;<br \/>\n    if(dir &#061;&#061; SLASH) return SLASH;<br \/>\n    auto pos &#061; dir.rfind(SLASH);<br \/>\n    if(pos &#061;&#061; std::string::npos) return &#034;BUG&#034;;<br \/>\n    return dir.substr(pos&#043;1);<br \/>\n}<\/p>\n<p>\/\/ \u5236\u4f5c\u547d\u4ee4\u884c\u63d0\u793a\u7b26<br \/>\nvoid MakeCommandline(char cmd_prompt[], int size)<br \/>\n{<br \/>\n    \/\/ \u8bbe\u7f6e\u5f0f\u5316\u8f93\u5165<br \/>\n    snprintf(cmd_prompt, size, FORMAT, GetUserName(), GetHostName(), DirName(GetPwd()).c_str());<br \/>\n    \/\/snprintf(cmd_prompt, size, FORMAT, GetUserName(), GetHostName(), GetPwd());<br \/>\n}<\/p>\n<p>\/\/ \u6253\u5370\u547d\u4ee4\u884c\u63d0\u793a\u7b26<br \/>\nvoid PrintCommandline()<br \/>\n{<br \/>\n    char prompt[COMMAND_SIZE];<br \/>\n    MakeCommandline(prompt, COMMAND_SIZE);<br \/>\n    printf(&#034;%s&#034;, prompt);<br \/>\n    fflush(stdout);<br \/>\n} <\/p>\n<h4 id=\"%EF%BC%883%EF%BC%89%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E8%BE%93%E5%85%A5%E7%9A%84%E5%91%BD%E4%BB%A4\">&#xff08;3&#xff09;\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u547d\u4ee4<\/h4>\n<p>GetCommandline() \u4ece\u6807\u51c6\u8f93\u5165\u8bfb\u53d6\u4e00\u884c\u7528\u6237\u8f93\u5165\u7684\u547d\u4ee4\u3002<\/p>\n<p>fgets() \u662f\u4e00\u4e2a\u5728 C \u8bed\u8a00\u4e2d\u7528\u4e8e\u4ece\u6587\u4ef6\u6d41\u4e2d\u8bfb\u53d6\u5b57\u7b26\u4e32\u7684\u51fd\u6570&#xff0c;\u5c5e\u4e8e\u6807\u51c6\u5e93 &lt;stdio.h&gt; \u4e2d\u7684\u51fd\u6570\u3002\u5b83\u80fd\u591f\u4ece\u6307\u5b9a\u7684\u6587\u4ef6\u6d41\u4e2d\u8bfb\u53d6\u4e00\u884c\u6570\u636e&#xff0c;\u5e76\u5c06\u5176\u5b58\u50a8\u5230\u76ee\u6807\u7f13\u51b2\u533a\u4e2d&#xff0c;\u540c\u65f6\u53ef\u4ee5\u9650\u5236\u8bfb\u53d6\u7684\u6700\u5927\u5b57\u7b26\u6570&#xff0c;\u4ece\u800c\u907f\u514d\u7f13\u51b2\u533a\u6ea2\u51fa\u3002<\/p>\n<p>char* fgets(char* str, int size, FILE* stream); <\/p>\n<p>\u4f46 fgets() \u4f1a\u4fdd\u7559\u8f93\u5165\u4e2d\u7684\u6362\u884c\u7b26&#xff0c;\u6240\u4ee5\u8c03\u7528\u540e\u8981\u6e05\u7406\u6362\u884c\u7b26\u3002<\/p>\n<p>\/\/ \u83b7\u53d6\u7528\u6237\u8f93\u5165\u547d\u4ee4<br \/>\nbool GetCommandline(char *out, int size)<br \/>\n{<br \/>\n    \/\/ \u4ece\u6807\u51c6\u8f93\u5165\u6d41\u83b7\u53d6\u547d\u4ee4&#xff0c;\u5176\u5b9e\u5c31\u662f\u5b57\u7b26\u4e32<br \/>\n    char* c &#061; fgets(out, size, stdin);<\/p>\n<p>    if(c &#061;&#061; NULL) return 1;<br \/>\n    out[strlen(out)-1] &#061; &#039;\\\\0&#039;; \/\/ \u6e05\u7406\\\\n, \u81f3\u5c11\u4f1a\u6309\u4e00\u6b21\u56de\u8f66&#xff0c;\u6240\u4ee5\u4e0d\u4f1a\u51fa\u9519<\/p>\n<p>    if(strlen(out) &#061;&#061; 0) return false;\/\/ \u4ec0\u4e48\u90fd\u6ca1\u8f93\u5165<br \/>\n    return true;<br \/>\n} <\/p>\n<h4 id=\"%EF%BC%884%EF%BC%89%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%88%86%E6%9E%90\">&#xff08;4&#xff09;\u547d\u4ee4\u884c\u5206\u6790<\/h4>\n<p>\u51fd\u6570\u5b9e\u73b0&#xff1a;\u547d\u4ee4\u884c\u5b57\u7b26\u4e32\u7684\u89e3\u6790&#xff0c;\u5c06\u7528\u6237\u8f93\u5165\u7684\u547d\u4ee4\u884c&#xff08;\u5982 &#034;ls -a -l&#034;&#xff09;\u62c6\u5206\u4e3a \u53c2\u6570\u6570\u7ec4 g_argv&#xff0c;\u5e76\u8bb0\u5f55\u53c2\u6570\u4e2a\u6570 g_argc\u3002<\/p>\n<p>strtok() \u662f\u4e00\u4e2a\u5728 C \u8bed\u8a00\u4e2d\u7528\u4e8e\u5b57\u7b26\u4e32\u5206\u5272\u7684\u51fd\u6570&#xff0c;\u5c5e\u4e8e\u6807\u51c6\u5e93 &lt;string.h&gt; \u4e2d\u7684\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u6210\u591a\u4e2a\u5b50\u5b57\u7b26\u4e32&#xff0c;\u5e76\u4e14\u5728\u591a\u6b21\u8c03\u7528\u4e2d\u9010\u4e2a\u8fd4\u56de\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n<p>char* strtok(char* str, const char* delim); <\/p>\n<p><span style=\"color:#1a439c\"><span style=\"background-color:#eaf4fc\">\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6&#xff0c;str \u662f\u6307\u5411\u8981\u5206\u5272\u7684\u539f\u59cb\u5b57\u7b26\u4e32\u7684\u6307\u9488\u3002 \u5728\u540e\u7eed\u8c03\u7528\u4e2d&#xff0c;str \u5e94\u8be5\u4f20\u5165 NULL&#xff0c;\u8868\u793a\u7ee7\u7eed\u5206\u5272\u4e0a\u4e00\u6b21\u8c03\u7528\u540e\u5269\u4f59\u7684\u90e8\u5206\u3002<\/span><\/span><\/p>\n<p>\/\/ \u5168\u5c40\u7684\u547d\u4ee4\u884c\u53c2\u6570\u8868<br \/>\n#define MAXARGC 128<br \/>\nchar *g_argv[MAXARGC];<br \/>\nint g_argc &#061; 0;<\/p>\n<p>\/\/ 3. \u547d\u4ee4\u884c\u5206\u6790<br \/>\nbool CommandParse(char *commandline)<br \/>\n{<br \/>\n    \/\/ &#034;ls -a -l&#034; -&gt; &#034;ls&#034; &#034;-a&#034; &#034;-l&#034;<br \/>\n    \/\/ \u5b57\u7b26\u4e32\u5207\u5272 strtok()\u51fd\u6570<br \/>\n#define SEP &#034; &#034;<br \/>\n    g_argc &#061; 0;<br \/>\n    \/\/ \u7b2c\u4e00\u6b21\u8c03\u7528&#xff1a;\u4f20\u5165\u8981\u5206\u5272\u7684\u5b57\u7b26\u4e32\u548c\u5206\u9694\u7b26<br \/>\n    g_argv[g_argc&#043;&#043;] &#061; strtok(commandline, SEP);<br \/>\n    \/\/ \u540e\u7eed\u8c03\u7528&#xff1a;\u4f20\u5165 NULL \u548c\u5206\u9694\u7b26&#xff0c;\u7ee7\u7eed\u5206\u5272\u540c\u4e00\u5b57\u7b26\u4e32<br \/>\n    while(g_argv[g_argc&#043;&#043;] &#061; strtok(NULL, SEP));<br \/>\n    g_argc&#8211;; \/\/ \u4fee\u6b63\u53c2\u6570\u8ba1\u6570&#xff0c;\u56e0\u4e3awhile \u5faa\u73af\u4e2d g_argc&#043;&#043; \u5728\u8d4b\u503c\u540e\u6267\u884c\u7684<br \/>\n    return g_argc &gt; 0 ? true : false;<br \/>\n} <\/p>\n<h4 id=\"%EF%BC%885%EF%BC%89%E6%A3%80%E6%B5%8B%E5%B9%B6%E6%89%A7%E8%A1%8C%E5%86%85%E9%94%AE%E5%91%BD%E4%BB%A4\">&#xff08;5&#xff09;\u68c0\u6d4b\u5e76\u6267\u884c\u5185\u952e\u547d\u4ee4<\/h4>\n<p>CheckAndExecBuiltin() \u68c0\u67e5\u662f\u5426\u4e3a\u5185\u5efa\u547d\u4ee4\u3002CD()\u00a0\u5b9e\u73b0\u76ee\u5f55\u5207\u6362&#xff08;cd \u547d\u4ee4&#xff09;&#xff0c;Echo()\u00a0\u5b9e\u73b0 echo \u547d\u4ee4\u3002<\/p>\n<p>chdir() \u662f\u4e00\u4e2a\u5728 C \u8bed\u8a00\u4e2d\u7528\u4e8e\u66f4\u6539\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u51fd\u6570&#xff0c;\u5c5e\u4e8e\u6807\u51c6\u5e93 &lt;unistd.h&gt; \u4e2d\u7684\u51fd\u6570&#xff08;\u5728 POSIX \u7cfb\u7edf\u4e2d&#xff09;\u3002\u5b83\u5141\u8bb8\u7a0b\u5e8f\u52a8\u6001\u5730\u6539\u53d8\u5f53\u524d\u8fdb\u7a0b\u7684\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<p>\/\/ \u6700\u540e\u4e00\u4e2a\u7a0b\u5e8f\u9000\u51fa\u7801<br \/>\nint lastcode &#061; 0;<\/p>\n<p>\/\/ \u5168\u5c40\u7684\u547d\u4ee4\u884c\u53c2\u6570\u8868<br \/>\n#define MAXARGC 128<br \/>\nchar *g_argv[MAXARGC];<br \/>\nint g_argc &#061; 0;<\/p>\n<p>\/\/ \u5185\u952e\u547d\u4ee4<br \/>\nbool CD()<br \/>\n{<\/p>\n<p>    if(g_argc &#061;&#061; 1) \/\/ \u53ea\u6709\u4e00\u4e2acd\u65f6<br \/>\n    {<br \/>\n        std::string home &#061; GetHome();<br \/>\n         if(home.empty()) return true;<br \/>\n        chdir(home.c_str()); \/\/ \u5207\u6362\u8def\u5f84<br \/>\n     }<br \/>\n    else<br \/>\n    {<br \/>\n        std::string where &#061; g_argv[1]; \/\/ \u76ee\u6807\u8def\u5f84<br \/>\n        if(where &#061;&#061; &#034;-&#034;)<br \/>\n        {}<br \/>\n        else if(where &#061;&#061; &#034;~&#034;)<br \/>\n        {}<br \/>\n        else<br \/>\n        {<br \/>\n            chdir(where.c_str());<br \/>\n        }<br \/>\n    }<br \/>\n    return true;<br \/>\n}<\/p>\n<p>bool Echo()<br \/>\n{<br \/>\n    if(g_argc &#061;&#061; 2)<br \/>\n    {<br \/>\n        \/\/<br \/>\n        std::string opt &#061; g_argv[1];<br \/>\n        if(opt &#061;&#061; &#034;$?&#034;) \/\/ eg1: echo $? \u6253\u5370\u9000\u51fa\u7801<br \/>\n        {<br \/>\n            std::cout &lt;&lt; lastcode &lt;&lt; std::endl;<br \/>\n            lastcode &#061; 0;<br \/>\n            return true;<br \/>\n        }<br \/>\n        if(opt[0] &#061;&#061; &#039;$&#039;) \/\/ eg2: echo $PATH \u6253\u5370\u73af\u5883\u53d8\u91cf<br \/>\n        {<br \/>\n            std::string env_name &#061; opt.substr(1);<br \/>\n            const char * env_value &#061; getenv(env_name.c_str()); \/\/ \u83b7\u53d6\u73af\u5883\u53d8\u91cf\u7684\u503c<br \/>\n            if(env_value)<br \/>\n                std::cout &lt;&lt; env_value &lt;&lt; std::endl;<br \/>\n        }<br \/>\n    }<br \/>\n    return false;<br \/>\n}<\/p>\n<p>\/\/ 4. \u68c0\u6d4b\u5e76\u6267\u884c\u5185\u952e\u547d\u4ee4<br \/>\nbool CheckAndExecBuiltin()<br \/>\n{<br \/>\n    std::string cmd &#061; g_argv[0];<br \/>\n    if(cmd &#061;&#061; &#034;cd&#034;)<br \/>\n    {<br \/>\n       return CD();<br \/>\n    }<br \/>\n    else if(cmd &#061;&#061; &#034;echo&#034;)<br \/>\n    {<br \/>\n       return Echo();<br \/>\n    }<br \/>\n    return false;<br \/>\n} <\/p>\n<h4 id=\"%EF%BC%886%EF%BC%89%E6%89%A7%E8%A1%8C%E5%91%BD%E4%BB%A4\">&#xff08;6&#xff09;\u6267\u884c\u547d\u4ee4<\/h4>\n<p>Execute() \u5b9e\u73b0\u4e86 Shell \u4e2d\u5916\u90e8\u547d\u4ee4\u7684\u6267\u884c&#xff0c;\u6838\u5fc3\u662f\u901a\u8fc7 fork() \u521b\u5efa\u5b50\u8fdb\u7a0b&#xff0c;\u5e76\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8c03\u7528 execvp \u6267\u884c\u76ee\u6807\u547d\u4ee4&#xff08;\u81ea\u5b9a\u4e49shell&#xff09;&#xff0c;\u7236\u8fdb\u7a0b\u5219\u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ed3\u675f\u5e76\u8bb0\u5f55\u5176\u9000\u51fa\u72b6\u6001\u3002<\/p>\n<p>\/\/ 5.\u6267\u884c\u547d\u4ee4<br \/>\nint Execute()<br \/>\n{<br \/>\n    pid_t id &#061; fork();<br \/>\n    if(id &#061;&#061; 0)<br \/>\n    {<br \/>\n        \/\/ child, \u7a0b\u5e8f\u66ff\u6362<br \/>\n        execvp(g_argv[0], g_argv);<br \/>\n        exit(1);<br \/>\n    }<br \/>\n    \/\/ father<br \/>\n    int status &#061; 0;<br \/>\n    pid_t rid &#061; waitpid(id, &amp;status, 0);<br \/>\n    if(rid &gt; 0)<br \/>\n    {<br \/>\n        lastcode &#061; WEXITSTATUS(status);<br \/>\n    }<br \/>\n    return 1;<br \/>\n} <\/p>\n<h4 id=\"%EF%BC%887%EF%BC%89%E5%AE%8C%E6%95%B4%E4%BB%A3%E7%A0%81\" style=\"background-color:transparent\">&#xff08;7&#xff09;\u5b8c\u6574\u4ee3\u7801<\/h4>\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;unistd.h&gt;<br \/>\n#include&lt;sys\/types.h&gt;<br \/>\n#include&lt;sys\/wait.h&gt;<\/p>\n<p>#define COMMAND_SIZE 1024<br \/>\n#define HOSTNAME_MAX 1024<br \/>\n#define FORMAT &#034;[%s&#064;%s:%s]# &#034;<\/p>\n<p>\/\/ shell\u5b9a\u4e49\u7684\u5168\u5c40\u53d8\u91cf<\/p>\n<p>\/\/ \u5168\u5c40\u7684\u547d\u4ee4\u884c\u53c2\u6570\u8868<br \/>\n#define MAXARGC 128<br \/>\nchar *g_argv[MAXARGC];<br \/>\nint g_argc &#061; 0;<\/p>\n<p>char cwd[1019];<br \/>\nchar cwdenv[1024]; <\/p>\n<p>\/\/ \u73af\u5883\u53d8\u91cf\u8868<br \/>\n#define MAX_ENVS 1024<br \/>\nchar *g_env[MAX_ENVS];<br \/>\nint g_envs &#061; 0;<\/p>\n<p>\/\/ \u6700\u540e\u4e00\u4e2a\u7a0b\u5e8f\u9000\u51fa\u7801<br \/>\nint lastcode &#061; 0;<\/p>\n<p>const char *GetUserName()<br \/>\n{<br \/>\n    const char * name &#061; getenv(&#034;USER&#034;);<br \/>\n    return name &#061;&#061; NULL ? &#034;None&#034; : name;<br \/>\n}<\/p>\n<p>const char *GetHostName()<br \/>\n{<br \/>\n    char hostname[HOSTNAME_MAX];<br \/>\n    return (gethostname(hostname, HOSTNAME_MAX) &#061;&#061; -1) ? &#034;None&#034; : hostname;<br \/>\n}<\/p>\n<p>const char *GetPwd()<br \/>\n{<br \/>\n    \/\/const char * pwd &#061; getenv(&#034;PWD&#034;);<br \/>\n    const char *pwd &#061; getcwd(cwd, sizeof(cwd)); \/\/ \u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u83b7\u53d6<br \/>\n    if(pwd !&#061; NULL)<br \/>\n    {   \/\/ \u628a\u81ea\u5df1\u7684\u73af\u5883\u53d8\u91cf\u5bfc\u7ed9\u8fdb\u7a0b<br \/>\n        snprintf(cwdenv, sizeof(cwdenv), &#034;PWD&#061;%s&#034;, cwd);<br \/>\n        putenv(cwdenv);<br \/>\n    }<br \/>\n    return pwd &#061;&#061; NULL ? &#034;None&#034; : pwd;<br \/>\n}<\/p>\n<p>const char *GetHome()<br \/>\n{<br \/>\n    const char *home &#061; getenv(&#034;HOME&#034;);<br \/>\n    return home &#061;&#061; &#034;&#034; ? NULL : home;<br \/>\n}<\/p>\n<p>\/\/ \u5bf9\u8def\u5f84\u505a\u5305\u88c5<br \/>\nstd::string DirName(const char *pwd)<br \/>\n{<br \/>\n#define SLASH &#034;\/&#034;<br \/>\n    std::string dir &#061; pwd;<br \/>\n    if(dir &#061;&#061; SLASH) return SLASH;<br \/>\n    auto pos &#061; dir.rfind(SLASH);<br \/>\n    if(pos &#061;&#061; std::string::npos) return &#034;BUG&#034;;<br \/>\n    return dir.substr(pos&#043;1);<br \/>\n}<\/p>\n<p>\/\/ \u5236\u4f5c\u547d\u4ee4\u884c\u63d0\u793a\u7b26<br \/>\nvoid MakeCommandline(char cmd_prompt[], int size)<br \/>\n{<br \/>\n    \/\/ \u8bbe\u7f6e\u5f0f\u5316\u8f93\u5165<br \/>\n    snprintf(cmd_prompt, size, FORMAT, GetUserName(), GetHostName(), DirName(GetPwd()).c_str());<br \/>\n    \/\/snprintf(cmd_prompt, size, FORMAT, GetUserName(), GetHostName(), GetPwd());<br \/>\n}<\/p>\n<p>\/\/ \u6253\u5370\u547d\u4ee4\u884c\u63d0\u793a\u7b26<br \/>\nvoid PrintCommandline()<br \/>\n{<br \/>\n    char prompt[COMMAND_SIZE];<br \/>\n    MakeCommandline(prompt, COMMAND_SIZE);<br \/>\n    printf(&#034;%s&#034;, prompt);<br \/>\n    fflush(stdout);<br \/>\n}<\/p>\n<p>\/\/ \u83b7\u53d6\u7528\u6237\u8f93\u5165\u547d\u4ee4<br \/>\nbool GetCommandline(char *out, int size)<br \/>\n{<br \/>\n    \/\/ \u4ece\u6807\u51c6\u8f93\u5165\u6d41\u83b7\u53d6\u547d\u4ee4&#xff0c;\u5176\u5b9e\u5c31\u662f\u5b57\u7b26\u4e32<br \/>\n    char* c &#061; fgets(out, size, stdin);<\/p>\n<p>    if(c &#061;&#061; NULL) return 1;<br \/>\n    out[strlen(out)-1] &#061; &#039;\\\\0&#039;; \/\/ \u6e05\u7406\\\\n, \u81f3\u5c11\u4f1a\u6309\u4e00\u6b21\u56de\u8f66&#xff0c;\u6240\u4ee5\u4e0d\u4f1a\u51fa\u9519<\/p>\n<p>    if(strlen(out) &#061;&#061; 0) return false;\/\/ \u4ec0\u4e48\u90fd\u6ca1\u8f93\u5165<br \/>\n    return true;<br \/>\n}<\/p>\n<p>\/\/ 3. \u547d\u4ee4\u884c\u5206\u6790<br \/>\nbool CommandParse(char *commandline)<br \/>\n{<br \/>\n    \/\/ &#034;ls -a -l&#034; -&gt; &#034;ls&#034; &#034;-a&#034; &#034;-l&#034;<br \/>\n    \/\/ \u5b57\u7b26\u4e32\u5207\u5272 strtok()\u51fd\u6570<br \/>\n#define SEP &#034; &#034;<br \/>\n    g_argc &#061; 0;<br \/>\n    \/\/ \u7b2c\u4e00\u6b21\u8c03\u7528&#xff1a;\u4f20\u5165\u8981\u5206\u5272\u7684\u5b57\u7b26\u4e32\u548c\u5206\u9694\u7b26<br \/>\n    g_argv[g_argc&#043;&#043;] &#061; strtok(commandline, SEP);<br \/>\n    \/\/ \u540e\u7eed\u8c03\u7528&#xff1a;\u4f20\u5165 NULL \u548c\u5206\u9694\u7b26&#xff0c;\u7ee7\u7eed\u5206\u5272\u540c\u4e00\u5b57\u7b26\u4e32<br \/>\n    while(g_argv[g_argc&#043;&#043;] &#061; strtok(NULL, SEP));<br \/>\n    g_argc&#8211;;<br \/>\n    return g_argc &gt; 0 ? true : false;<br \/>\n}<\/p>\n<p>void PrintArray()<br \/>\n{<br \/>\n    for(int i &#061; 0; g_argv[i]; i&#043;&#043;)<br \/>\n    {<br \/>\n        printf(&#034;argv[%d]-&gt;%s\\\\n&#034;, i, g_argv[i]);<br \/>\n    }<br \/>\n    printf(&#034;%d\\\\n&#034;,g_argc);<br \/>\n}<\/p>\n<p>\/\/ \u4ece\u7236shell1\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u8868&#xff0c;\u672c\u6765\u662f\u8981\u4ece\u914d\u7f6e\u6587\u4ef6\u83b7\u53d6\u7684<br \/>\nvoid InitEnv()<br \/>\n{<br \/>\n    extern char **environ;<br \/>\n    memset(g_env, 0, sizeof(g_env)); \/\/ \u521d\u59cb\u5316<br \/>\n    g_envs &#061; 0;<\/p>\n<p>    \/\/ 1.\u83b7\u53d6\u73af\u5883\u53d8\u91cf<br \/>\n    for(int i &#061; 0; environ[i]; i&#043;&#043;)<br \/>\n    {<br \/>\n        \/\/ (1)\u7533\u8bf7\u7a7a\u95f4<br \/>\n        g_env[i] &#061; (char*)malloc(strlen(environ[i])&#043;1);<br \/>\n        \/\/ (2)\u62f7\u8d1d\u5230\u6211\u4eec\u7684\u73af\u5883\u53d8\u91cf\u8868<br \/>\n        strcpy(g_env[i], environ[i]);<br \/>\n        g_envs&#043;&#043;;<br \/>\n    }<br \/>\n    \/\/ \u6d4b\u8bd5&#xff1a;\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u73af\u5883\u53d8\u91cf<br \/>\n    g_env[g_envs&#043;&#043;] &#061; (char*)&#034;HAHA&#061;for_test&#034;;<br \/>\n    g_env[g_envs] &#061; NULL; \/\/ \u8868\u7684\u6700\u540e\u4e00\u4e2a\u6210\u5458\u5fc5\u987b\u662fNULL<\/p>\n<p>    \/\/ 2.\u83b7\u53d6\u5230\u7684\u73af\u5883\u53d8\u91cf\u5bfc\u5165shell\u7684\u8868\u4e2d<br \/>\n    for(int i &#061; 0; g_env[i]; i&#043;&#043;)<br \/>\n    {<br \/>\n        putenv(g_env[i]);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u5185\u952e\u547d\u4ee4<br \/>\nbool CD()<br \/>\n{<\/p>\n<p>    if(g_argc &#061;&#061; 1) \/\/ \u53ea\u6709\u4e00\u4e2acd\u65f6<br \/>\n    {<br \/>\n        std::string home &#061; GetHome();<br \/>\n         if(home.empty()) return true;<br \/>\n        chdir(home.c_str());<br \/>\n     }<br \/>\n    else<br \/>\n    {<br \/>\n        std::string where &#061; g_argv[1];<br \/>\n        if(where &#061;&#061; &#034;-&#034;)<br \/>\n        {}<br \/>\n        else if(where &#061;&#061; &#034;~&#034;)<br \/>\n        {}<br \/>\n        else<br \/>\n        {<br \/>\n            chdir(where.c_str());<br \/>\n        }<br \/>\n    }<br \/>\n    return true;<br \/>\n}<\/p>\n<p>bool Echo()<br \/>\n{<br \/>\n    if(g_argc &#061;&#061; 2)<br \/>\n    {<br \/>\n        \/\/<br \/>\n        std::string opt &#061; g_argv[1];<br \/>\n        if(opt &#061;&#061; &#034;$?&#034;) \/\/ eg1: echo $?<br \/>\n        {<br \/>\n            std::cout &lt;&lt; lastcode &lt;&lt; std::endl;<br \/>\n            lastcode &#061; 0;<br \/>\n            return true;<br \/>\n        }<br \/>\n        if(opt[0] &#061;&#061; &#039;$&#039;) \/\/ eg2: echo $PATH<br \/>\n        {<br \/>\n            std::string env_name &#061; opt.substr(1);<br \/>\n            const char * env_value &#061; getenv(env_name.c_str()); \/\/ \u83b7\u53d6\u73af\u5883\u53d8\u91cf\u7684\u503c<br \/>\n            if(env_value)<br \/>\n                std::cout &lt;&lt; env_value &lt;&lt; std::endl;<br \/>\n        }<br \/>\n    }<br \/>\n    return false;<br \/>\n}<\/p>\n<p>\/\/ 4. \u68c0\u6d4b\u5e76\u6267\u884c\u5185\u952e\u547d\u4ee4<br \/>\nbool CheckAndExecBuiltin()<br \/>\n{<br \/>\n    std::string cmd &#061; g_argv[0];<br \/>\n    if(cmd &#061;&#061; &#034;cd&#034;)<br \/>\n    {<br \/>\n       return CD();<br \/>\n    }<br \/>\n    else if(cmd &#061;&#061; &#034;echo&#034;)<br \/>\n    {<br \/>\n       return Echo();<br \/>\n    }<br \/>\n    return false;<br \/>\n}<\/p>\n<p>\/\/ 5.\u6267\u884c\u547d\u4ee4<br \/>\nint Execute()<br \/>\n{<br \/>\n    pid_t id &#061; fork();<br \/>\n    if(id &#061;&#061; 0)<br \/>\n    {<br \/>\n        \/\/ child, \u7a0b\u5e8f\u66ff\u6362<br \/>\n        execvp(g_argv[0], g_argv);<br \/>\n        exit(1);<br \/>\n    }<br \/>\n    \/\/ father<br \/>\n    int status &#061; 0;<br \/>\n    pid_t rid &#061; waitpid(id, &amp;status, 0);<br \/>\n    if(rid &gt; 0)<br \/>\n    {<br \/>\n        lastcode &#061; WEXITSTATUS(status);<br \/>\n    }<br \/>\n    return 1;<br \/>\n}<br \/>\n\/\/ \u91ca\u653e\u901a\u8fc7malloc\u5206\u914d\u7684\u73af\u5883\u53d8\u91cf\u5185\u5b58<br \/>\nvoid FreeEnvMemory1()<br \/>\n{<br \/>\n    for(int i &#061; 0; i &lt; g_envs; i&#043;&#043;) {<br \/>\n        if(g_env[i] !&#061; NULL) {<br \/>\n            free(g_env[i]);  \/\/ \u91ca\u653e\u6bcf\u4e2a\u73af\u5883\u53d8\u91cf\u5b57\u7b26\u4e32<br \/>\n            g_env[i] &#061; NULL; \/\/ \u7f6e\u7a7a\u6307\u9488<br \/>\n        }<br \/>\n    }<br \/>\n    g_envs &#061; 0;  \/\/ \u91cd\u7f6e\u8ba1\u6570\u5668<br \/>\n}<\/p>\n<p>void FreeEnvMemory() {<br \/>\n    if (!g_used_putenv) {<br \/>\n        \/\/ \u672a\u8c03\u7528 putenv&#xff0c;\u53ef\u4ee5\u5b89\u5168\u91ca\u653e<br \/>\n        for (int i &#061; 0; i &lt; g_envs; i&#043;&#043;) {<br \/>\n            free(g_env[i]);<br \/>\n            g_env[i] &#061; NULL;<br \/>\n        }<br \/>\n    } else {<br \/>\n        \/\/ \u8c03\u7528\u8fc7 putenv&#xff0c;\u53ea\u80fd\u91ca\u653e\u6570\u7ec4\u6307\u9488&#xff08;\u4e0d\u91ca\u653e\u5b57\u7b26\u4e32\u5185\u5b58&#xff09;<br \/>\n        for (int i &#061; 0; i &lt; g_envs; i&#043;&#043;) {<br \/>\n            g_env[i] &#061; NULL;  \/\/ \u4ec5\u7f6e\u7a7a\u6307\u9488<br \/>\n        }<br \/>\n    }<br \/>\n    g_envs &#061; 0;<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    \/\/ shell\u542f\u52a8\u8981\u4ece\u7cfb\u7edf\u4e2d\u83b7\u53d6\u73af\u5883\u53d8\u91cf&#xff0c;\u800c\u6211\u4eec\u7684\u73af\u5883\u53d8\u91cf\u4fe1\u606f\u8981\u4ece\u7236shell\u4e2d\u83b7\u53d6<br \/>\n    InitEnv();<br \/>\n    \/\/ \u5f53\u7136\u662f\u5faa\u73af\u5566&#xff01;<br \/>\n    while(true)<br \/>\n    {<br \/>\n        \/\/ 1\u3001\u8f93\u51fa\u547d\u4ee4\u884c\u63d0\u793a\u7b26<br \/>\n        PrintCommandline();<\/p>\n<p>        \/\/ 2. \u83b7\u53d6\u8f93\u5165\u7684\u547d\u4ee4<br \/>\n        char commandline[COMMAND_SIZE];<br \/>\n        if(!GetCommandline(commandline, COMMAND_SIZE))<br \/>\n            continue;<br \/>\n        \/\/printf(&#034;%s\\\\n&#034;, commandline);<\/p>\n<p>        \/\/ 3. \u547d\u4ee4\u884c\u5206\u6790, \u5b57\u7b26\u4e32\u62c6\u5206\u6210\u591a\u4e2a\u5143\u7d20<br \/>\n        if(!CommandParse(commandline)) continue;<br \/>\n        \/\/PrintArray();<\/p>\n<p>        \/\/ 4.\u68c0\u6d4b\u5e76\u6267\u884c\u5185\u5efa\u547d\u4ee4<br \/>\n        if(CheckAndExecBuiltin()) continue;<\/p>\n<p>        \/\/ 5. \u6267\u884c\u547d\u4ee4<br \/>\n        Execute();<br \/>\n    }<br \/>\n    \/\/ \u91ca\u653e\u7a7a\u95f4<br \/>\n    FreeEnvMemory();<br \/>\n    return 0;<br \/>\n} <\/p>\n<h2 id=\"%C2%A0%E4%B8%89%E3%80%81%E5%87%BD%E6%95%B0%E5%92%8C%E8%BF%9B%E7%A8%8B%E4%B9%8B%E9%97%B4%E7%9A%84%E7%9B%B8%E4%BC%BC%E6%80%A7\" style=\"background-color:transparent\">\u00a0\u4e09\u3001\u51fd\u6570\u548c\u8fdb\u7a0b\u4e4b\u95f4\u7684\u76f8\u4f3c\u6027<\/h2>\n<p>\u4e00\u4e2aC\u7a0b\u5e8f\u6709\u5f88\u591a\u51fd\u6570\u7ec4\u6210\u3002\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570&#xff0c;\u540c\u65f6\u4f20\u9012\u7ed9\u5b83\u4e00\u4e9b\u53c2\u6570\u3002\u88ab\u8c03\u7528\u7684\u51fd\u6570\u6267\u884c\u4e00\u5b9a\u7684\u64cd\u4f5c&#xff0c;\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u6bcf\u4e2a\u51fd\u6570\u90fd\u6709\u4ed6\u7684\u5c40\u90e8\u53d8\u91cf&#xff0c;\u4e0d\u540c\u7684\u51fd\u6570\u901a\u8fc7 call\/return \u7cfb\u7edf\u8fdb\u884c\u901a\u4fe1\u3002\u8fd9\u79cd\u901a\u8fc7\u53c2\u6570\u548c\u8fd4\u56de\u503c\u5728\u62e5\u6709\u79c1\u6709\u6570\u636e\u7684\u51fd\u6570\u95f4\u901a\u4fe1\u7684\u6a21\u5f0f\u662f\u7ed3\u6784\u5316\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u57fa\u7840\u3002Linux \u9f13\u52b1\u5c06\u8fd9\u79cd\u5e94\u7528\u4e8e\u7a0b\u5e8f\u4e4b\u5185\u7684\u6a21\u5f0f\u6269\u5c55\u5230\u7a0b\u5e8f\u4e4b\u95f4\u3002\u5982\u4e0b\u56fe&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"831\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250426053028-680c6f74409a7.png\" width=\"1477\" \/><\/p>\n<p>\u4e00\u4e2aC\u7a0b\u5e8f\u53ef\u4ee5 fork\/exec \u53e6\u4e00\u4e2a\u7a0b\u5e8f&#xff0c;\u5e76\u4f20\u7ed9\u5b83\u4e00\u4e9b\u53c2\u6570\u3002\u8fd9\u4e2a\u88ab\u8c03\u7528\u7684\u7a0b\u5e8f\u6267\u884c\u4e00\u5b9a\u7684\u64cd\u4f5c&#xff0c;\u7136\u540e\u901a\u8fc7 exit(n) \u6765\u8fd4\u56de\u503c\u3002\u8c03\u7528\u5b83\u7684\u8fdb\u7a0b\u53ef\u4ee5\u901a\u8fc7 wait(&amp;ret) \u6765\u83b7\u53d6 exit \u7684\u8fd4\u56de\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e43\u6b21\uff0c\u6536\u85cf52\u6b21\u3002\u672c\u6587\u6df1\u5165\u63a2\u8ba8\u8fdb\u7a0b\u66ff\u6362\u539f\u7406\u4e0e\u5b9e\u73b0\uff0c\u89e3\u6790 exec \u51fd\u6570\u7684\u5173\u952e\u7279\u6027\uff0c\u540c\u65f6\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49 Shell \u547d\u4ee4\u884c\u89e3\u91ca\u5668\uff0c\u63ed\u793a\u51fd\u6570\u8c03\u7528\u4e0e\u8fdb\u7a0b\u901a\u4fe1\u7684\u5185\u5728\u76f8\u4f3c\u6027\uff0c\u52a9\u529b\u8bfb\u8005\u638c\u63e1 Unix \u73af\u5883\u4e0b\u7684\u6838\u5fc3\u7f16\u7a0b\u6280\u5de7\u3002<\/p>\n","protected":false},"author":2,"featured_media":33075,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[58,103,43,44],"topic":[],"class_list":["post-33080","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-linux","tag-ubuntu","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\u8fdb\u7a0b\u66ff\u6362\u4e0e\u81ea\u5b9a\u4e49 Shell\uff1a\u539f\u7406\u4e0e\u5b9e\u6218 - \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\/33080.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010Linux\u3011\u8fdb\u7a0b\u66ff\u6362\u4e0e\u81ea\u5b9a\u4e49 Shell\uff1a\u539f\u7406\u4e0e\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e43\u6b21\uff0c\u6536\u85cf52\u6b21\u3002\u672c\u6587\u6df1\u5165\u63a2\u8ba8\u8fdb\u7a0b\u66ff\u6362\u539f\u7406\u4e0e\u5b9e\u73b0\uff0c\u89e3\u6790 exec \u51fd\u6570\u7684\u5173\u952e\u7279\u6027\uff0c\u540c\u65f6\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49 Shell \u547d\u4ee4\u884c\u89e3\u91ca\u5668\uff0c\u63ed\u793a\u51fd\u6570\u8c03\u7528\u4e0e\u8fdb\u7a0b\u901a\u4fe1\u7684\u5185\u5728\u76f8\u4f3c\u6027\uff0c\u52a9\u529b\u8bfb\u8005\u638c\u63e1 Unix \u73af\u5883\u4e0b\u7684\u6838\u5fc3\u7f16\u7a0b\u6280\u5de7\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/33080.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-26T05:30:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250426053026-680c6f7275656.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=\"9 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/33080.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/33080.html\",\"name\":\"\u3010Linux\u3011\u8fdb\u7a0b\u66ff\u6362\u4e0e\u81ea\u5b9a\u4e49 Shell\uff1a\u539f\u7406\u4e0e\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-26T05:30:29+00:00\",\"dateModified\":\"2025-04-26T05:30:29+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/33080.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/33080.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/33080.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010Linux\u3011\u8fdb\u7a0b\u66ff\u6362\u4e0e\u81ea\u5b9a\u4e49 Shell\uff1a\u539f\u7406\u4e0e\u5b9e\u6218\"}]},{\"@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\u8fdb\u7a0b\u66ff\u6362\u4e0e\u81ea\u5b9a\u4e49 Shell\uff1a\u539f\u7406\u4e0e\u5b9e\u6218 - \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\/33080.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010Linux\u3011\u8fdb\u7a0b\u66ff\u6362\u4e0e\u81ea\u5b9a\u4e49 Shell\uff1a\u539f\u7406\u4e0e\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e43\u6b21\uff0c\u6536\u85cf52\u6b21\u3002\u672c\u6587\u6df1\u5165\u63a2\u8ba8\u8fdb\u7a0b\u66ff\u6362\u539f\u7406\u4e0e\u5b9e\u73b0\uff0c\u89e3\u6790 exec \u51fd\u6570\u7684\u5173\u952e\u7279\u6027\uff0c\u540c\u65f6\u8bbe\u8ba1\u5e76\u5b9e\u73b0\u4e00\u4e2a\u81ea\u5b9a\u4e49 Shell \u547d\u4ee4\u884c\u89e3\u91ca\u5668\uff0c\u63ed\u793a\u51fd\u6570\u8c03\u7528\u4e0e\u8fdb\u7a0b\u901a\u4fe1\u7684\u5185\u5728\u76f8\u4f3c\u6027\uff0c\u52a9\u529b\u8bfb\u8005\u638c\u63e1 Unix \u73af\u5883\u4e0b\u7684\u6838\u5fc3\u7f16\u7a0b\u6280\u5de7\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/33080.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-26T05:30:29+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250426053026-680c6f7275656.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"9 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/33080.html","url":"https:\/\/www.wsisp.com\/helps\/33080.html","name":"\u3010Linux\u3011\u8fdb\u7a0b\u66ff\u6362\u4e0e\u81ea\u5b9a\u4e49 Shell\uff1a\u539f\u7406\u4e0e\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-26T05:30:29+00:00","dateModified":"2025-04-26T05:30:29+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/33080.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/33080.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/33080.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010Linux\u3011\u8fdb\u7a0b\u66ff\u6362\u4e0e\u81ea\u5b9a\u4e49 Shell\uff1a\u539f\u7406\u4e0e\u5b9e\u6218"}]},{"@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\/33080","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=33080"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/33080\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/33075"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=33080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=33080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=33080"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=33080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}