{"id":8789,"date":"2023-05-30T17:56:19","date_gmt":"2023-05-30T09:56:19","guid":{"rendered":"\/?p=8789"},"modified":"2023-05-30T17:56:19","modified_gmt":"2023-05-30T09:56:19","slug":"08-shell%e8%84%9a%e6%9c%ac%e7%bc%96%e7%a8%8b%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"\/?p=8789","title":{"rendered":"08-shell\u811a\u672c\u7f16\u7a0b\u57fa\u7840"},"content":{"rendered":"<h2>1.bash\u7279\u6027\u53cabash\u811a\u672c\u7f16\u7a0b\u521d\u6b65<\/h2>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230530102842433.png\" alt=\"image-20230530102842433\" \/> <\/p>\n<p>\u7ec8\u7aef\uff1a\u9644\u7740\u5728\u7ec8\u7aef\u7684\u63a5\u53e3\u7a0b\u5e8f<\/p>\n<ul>\n<li>GUI: KDE, GNome, Xfce<\/li>\n<li>CLI: \/etc\/shells<\/li>\n<\/ul>\n<p>bash\u7684\u7279\u6027\uff1a<\/p>\n<ul>\n<li>\n<p>\u547d\u4ee4\u884c\u5c55\u5f00\uff1a ~\uff0c {}<\/p>\n<\/li>\n<li>\n<p>\u547d\u4ee4\u522b\u540d\uff1aalias, unlias<\/p>\n<\/li>\n<li>\n<p>\u547d\u4ee4\u5386\u53f2\uff1ahistory<\/p>\n<\/li>\n<li>\n<p>\u6587\u4ef6\u540d\u901a\u914d\uff1aglob <\/p>\n<\/li>\n<li>\n<p>\u5feb\u6377\u952e\uff1a Ctrl + a, e, u, k, l<\/p>\n<\/li>\n<li>\n<p>\u547d\u4ee4\u8865\u5168\uff1a $PATH<\/p>\n<\/li>\n<li>\n<p>\u8def\u5f84\u8865\u5168<\/p>\n<\/li>\n<\/ul>\n<h3>1.bash\u7279\u6027\u4e4b\uff1a\u547d\u4ee4\u54c8\u5e0chash<\/h3>\n<p>\u7f13\u5b58\u6b64\u524d\u547d\u4ee4\u67e5\u627e\u7684\u7ed3\u679c\uff1a key-value<\/p>\n<p>key: \u641c\u7d22\u952e<\/p>\n<p>value\uff1a\u503c<\/p>\n<p><strong>hash<\/strong>\u547d\u4ee4\uff1a<\/p>\n<p>hash:\u5217\u51fa<\/p>\n<p>hash -d COMMAND: \u5220\u9664<\/p>\n<p>hash -r : \u6e05\u7a7a<\/p>\n<pre><code>[lemon@VM-4-10-centos ~]$ help hash\nhash: hash [-lr] [-p \u8def\u5f84\u540d] [-dt] [\u540d\u79f0 ...]\n    \u8bb0\u4f4f\u6216\u663e\u793a\u7a0b\u5e8f\u4f4d\u7f6e\u3002\n\n    \u786e\u5b9a\u5e76\u8bb0\u4f4f\u6bcf\u4e00\u4e2a\u7ed9\u5b9a NAME \u540d\u79f0\u7684\u547d\u4ee4\u7684\u5b8c\u6574\u8def\u5f84\u3002\u5982\u679c\n    \u4e0d\u63d0\u4f9b\u53c2\u6570\uff0c\u5219\u663e\u793a\u5df2\u7ecf\u8bb0\u4f4f\u7684\u547d\u4ee4\u7684\u4fe1\u606f\u3002\n\n    \u9009\u9879\uff1a\n      -d                \u5fd8\u8bb0\u6bcf\u4e00\u4e2a\u5df2\u7ecf\u8bb0\u4f4f\u7684\u547d\u4ee4\u7684\u4f4d\u7f6e # \u5220\u9664\u6307\u5b9a\u503c\n      -l                \u4ee5\u53ef\u4f5c\u4e3a\u8f93\u5165\u91cd\u7528\u7684\u683c\u5f0f\u663e\u793a\n      -p pathname       \u4f7f\u7528 pathname \u8def\u5f84\u4f5c\u4e3a NAME \u547d\u4ee4\u7684\u5168\u8def\u5f84\n      -r                \u5fd8\u8bb0\u6240\u6709\u8bb0\u4f4f\u7684\u4f4d\u7f6e  # \u6e05\u7a7a\u54c8\u5e0c\u8868\n      -t                \u6253\u5370\u8bb0\u4f4f\u7684\u6bcf\u4e00\u4e2a NAME \u540d\u79f0\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u6307\u5b9a\u4e86\u591a\u4e2a\n                NAME \u540d\u79f0\uff0c\u5219\u6bcf\u4e2a\u4f4d\u7f6e\u524d\u9762\u4f1a\u52a0\u4e0a\u76f8\u5e94\u7684 NAME \u540d\u79f0\n\n    \u53c2\u6570\uff1a\n      NAME              \u6bcf\u4e2a NAME \u540d\u79f0\u4f1a\u5728 $PATH \u8def\u5f84\u53d8\u91cf\u4e2d\u88ab\u641c\u7d22\uff0c\u5e76\n                \u4e14\u6dfb\u52a0\u5230\u8bb0\u4f4f\u7684\u547d\u4ee4\u7684\u5217\u8868\u4e2d\u3002\n\n    \u9000\u51fa\u72b6\u6001\uff1a\n    \u8fd4\u56de\u6210\u529f\uff0c\u9664\u975e NAME \u547d\u4ee4\u6ca1\u6709\u627e\u5230\u6216\u8005\u4f7f\u7528\u4e86\u65e0\u6548\u7684\u9009\u9879\u3002<\/code><\/pre>\n<p>\u6d4b\u8bd5\uff1a<\/p>\n<pre><code class=\"language-bash\">[rhino@rhino010 lxc]$ rm -rf test\n[rhino@rhino010 lxc]$ mkdir test\n[rhino@rhino010 lxc]$ hash\nhits    command\n   1    \/usr\/bin\/tty\n   1    \/usr\/bin\/install\n   1    \/usr\/sbin\/ifconfig\n   2    \/usr\/bin\/rm\n   1    \/usr\/bin\/who\n   3    \/usr\/bin\/mkdir\n   1    \/usr\/bin\/ls\n[rhino@rhino010 lxc]$ hash -d ls\n[rhino@rhino010 lxc]$ hash\nhits    command\n   1    \/usr\/bin\/tty\n   1    \/usr\/bin\/install\n   1    \/usr\/sbin\/ifconfig\n   2    \/usr\/bin\/rm\n   1    \/usr\/bin\/who\n   3    \/usr\/bin\/mkdir\n[rhino@rhino010 lxc]$ hash -r\n[rhino@rhino010 lxc]$ hash\nhash: hash table empty\n<\/code><\/pre>\n<h3>2.bash\u7279\u6027\u4e4b\uff1a\u53d8\u91cf<\/h3>\n<ul>\n<li>\n<p>\u7a0b\u5e8f\uff1a\u6307\u4ee4+\u6570\u636e<\/p>\n<ul>\n<li>\n<p>\u6307\u4ee4\uff1a\u7531\u7a0b\u5e8f\u6587\u4ef6\u63d0\u4f9b<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\uff1aIO\u8bbe\u5907(\u901a\u8fc7\u952e\u76d8\u8f93\u5165\u7b49)\u3001\u6587\u4ef6\u3001\u7ba1\u9053<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7a0b\u5e8f\uff1a\u7b97\u6cd5+\u6570\u636e\u7ed3\u6784<\/p>\n<\/li>\n<li>\n<p>\u53d8\u91cf\u8d4b\u503c\uff1aname=value<\/p>\n<\/li>\n<li>\n<p>\u53d8\u91cf\u7c7b\u578b(\u6570\u636e\u7c7b\u578b)\uff1a\u5b58\u50a8\u683c\u5f0f\u3001\u8868\u793a\u6570\u636e\u8303\u56f4\u3001\u53c2\u4e0e\u7684\u8fd0\u7b97<\/p>\n<ul>\n<li>\u7f16\u7a0b\u8bed\u8a00\uff1a<\/li>\n<li>\u5f3a\u7c7b\u578b\u53d8\u91cf: C\u8bed\u8a00\u7b49<\/li>\n<li>\u5f31\u7c7b\u578b\u53d8\u91cf\uff1abash\n<ul>\n<li>bash\u628a\u6240\u6709\u53d8\u91cf\u7edf\u7edf\u89c6\u4f5c\u5b57\u7b26\u578b<\/li>\n<li>bash\u4e2d\u7684\u53d8\u91cf\u65e0\u9700\u4e8b\u5148\u58f0\u660e,\u76f8\u5f53\u4e8e\u628a\u58f0\u660e\u548c\u590d\u5236\u8fc7\u7a0b\u540c\u65f6\u5b9e\u73b0\uff08python\u4e5f\u65e0\u9700\u58f0\u660e\uff0c\u4f46\u8fd8\u662f\u7b97\u5f3a\u7c7b\u578b\u53d8\u91cf\uff0c\u6bd5\u7adf\u4e0d\u80fd\u6267\u884c<code>1+&#039;2&#039;<\/code>\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u53d8\u91cf\u66ff\u6362\uff1a\u628a\u53d8\u91cf\u540d\u51fa\u73b0\u7684\u4f4d\u7f6e\u8f6c\u6362\u4e3a\u5176\u6240\u6307\u5411\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\u6570\u636e\u7684\u8fc7\u7a0b<\/p>\n<\/li>\n<li>\n<p>\u53d8\u91cf\u5f15\u7528\uff1a <code>${var_name}, $var_name<\/code><\/p>\n<\/li>\n<li>\n<p>\u53d8\u91cf\u540d\u547d\u540d\u65b9\u5f0f\uff1a\u6240\u6709\u7f16\u7a0b\u8bed\u8a00\u53d8\u91cf\u547d\u540d\u57fa\u672c\u4e0a\u89c4\u8303\u4e00\u81f4\uff0c\u53ea\u80fd\u5305\u542b\u6570\u5b57\u3001\u5b57\u6bcd\u3001\u4e0b\u5212\u7ebf\uff0c\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934<\/p>\n<\/li>\n<li>\n<p>bash\u53d8\u91cf\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li>\u672c\u5730\u53d8\u91cf\uff1a\u4f5c\u7528\u57df\u4ec5\u4e3a\u5f53\u524dshell\u8fdb\u7a0b<\/li>\n<li>\u73af\u5883\u53d8\u91cf\uff1a\u4f5c\u7528\u57df\u4e3a\u5f53\u524dshell\u8fdb\u7a0b\u53ca\u5176\u5b50\u8fdb\u7a0b(\u518d\u952e\u5165\u4e00\u6b21<code>bash<\/code>\u5219\u8fdb\u5165\u5b50shell\u8fdb\u7a0b\uff0c\u65b0\u5f00\u7684\u7ec8\u7aef\u662f\u4e0d\u884c\u7684\uff0c\u53ef\u4ee5<code>who<\/code>\u67e5\u770b\u7ec8\u7aef\u60c5\u51b5)<\/li>\n<li>\u5c40\u90e8\u53d8\u91cf\uff1a\u4f5c\u7528\u57df\u4ec5\u4e3a\u67d0\u4ee3\u7801\u7247\u6bb5\uff08\u51fd\u6570\u4e0a\u4e0b\u6587\uff09<\/li>\n<li>\u672a\u77e5\u53c2\u6570\u53d8\u91cf\uff1a\u5c31\u662f\u5411\u6267\u884c\u811a\u672cshell\u8fdb\u7a0b\u4f20\u9012\u7684\u53c2\u6570<\/li>\n<li>\u7279\u6b8a\u53d8\u91cf\uff1a\u901a\u5e38\u662fshell\u5185\u7f6e\u7684\u6709\u7279\u6b8a\u516c\u7528\u7684\u53d8\u91cf<\/li>\n<li><code>$?<\/code>\u4fdd\u5b58\u4e0a\u4e00\u4e2a\u547d\u4ee4\u7684\u6267\u884c\u72b6\u6001\u7ed3\u679c\uff080\u8868\u793a\u6210\u529f\uff0c1-255\u8868\u793a\u5931\u8d25\uff09<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>bash\u53d8\u91cf\u8d4b\u503c\u65b9\u5f0f\uff1a<\/p>\n<ul>\n<li>\n<p>\u672c\u5730\u53d8\u91cf\u8d4b\u503c\uff1a<code>name=value<\/code><\/p>\n<\/li>\n<li>\n<p>\u53d8\u91cf\u5f15\u7528\uff1a<code>${name}, $name<\/code><\/p>\n<ul>\n<li><code>&quot;&quot;<\/code>:\u53d8\u91cf\u540d\u4f1a\u66ff\u6362\u4e3a\u5176\u503c<\/li>\n<li><code>&#039;&#039;<\/code>:\u53d8\u91cf\u540d\u4e0d\u4f1a\u66ff\u6362\u4e3a\u5176\u503c<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u67e5\u770b\u53d8\u91cf\uff1a<code>set<\/code><\/p>\n<\/li>\n<li>\n<p>\u64a4\u9500\u53d8\u91cf\uff1a<code>unset name<\/code>  \u6ce8\u610f\uff0c\u6b64\u5904\u975e\u53d8\u91cf\u5f15\u7528<\/p>\n<\/li>\n<li>\n<p>\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<\/li>\n<li>\n<p>\u53d8\u91cf\u8d4b\u503c\uff08\u56db\u79cd\u65b9\u5f0f\uff09\uff1a<\/p>\n<ul>\n<li>\n<p><code>export name=value<\/code><\/p>\n<\/li>\n<li>\n<pre><code class=\"language-bash\">name = value\nexport name<\/code><\/pre>\n<\/li>\n<li>\n<p><code>declare -x name=value<\/code><\/p>\n<\/li>\n<li>\n<pre><code>name=value\ndeclare -x name<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u53d8\u91cf\u5f15\u7528:<code>${name}, $name<\/code>,\u6ce8\u610f\uff0cbash\u5185\u5d4c\u4e86\u8bb8\u591a\u73af\u5883\u53d8\u91cf(\u901a\u5e38\u4e3a\u5168\u5927\u5199\u5b57\u7b26\uff0c\u5982<code>PATH, HISTFILE,HISTZISE,HISTFILESIZE,HISTCONTROL,SHELL,HOME,UID,OWD,OLDPWD<\/code>)\uff0c\u7528\u4e8e\u5b9a\u4e49bash\u7684\u5de5\u4f5c\u73af\u5883<\/p>\n<\/li>\n<li>\n<p>\u67e5\u770b\u73af\u5883\u53d8\u91cf<\/p>\n<ul>\n<li><code>printenv<\/code><\/li>\n<li><code>declare -x<\/code><\/li>\n<li><code>env<\/code><\/li>\n<li><code>export<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u64a4\u9500\u73af\u5883\u53d8\u91cf\uff1a<code>unset name<\/code><\/p>\n<\/li>\n<li>\n<p>\u53ea\u8bfb\u53d8\u91cf\uff1a\u65e0\u6cd5\u91cd\u65b0\u8d4b\u503c\uff0c\u5e76\u4e14\u4e0d\u652f\u6301\u64a4\u9500\uff1a\u5b58\u6d3b\u65f6\u95f4\u4e3a\u5f53\u524dshell\u8fdb\u7a0b\u7684\u751f\u547d\u5468\u671f\uff0c\u5c81shell\u8fdb\u7a0b\u7ec8\u6b62\u800c\u7ec8\u6b62<\/p>\n<\/li>\n<li>\n<p><code>declare -r name<\/code><\/p>\n<\/li>\n<li>\n<p><code>readonly name<\/code><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>3. bash\u7279\u6027\u4e4b\uff1a\u591a\u547d\u4ee4\u6267\u884c<\/h3>\n<ul>\n<li>\n<p><code>~]# COMMAND1; COMMAND2; COMMAND3;<\/code><\/p>\n<\/li>\n<li>\n<p>\u903b\u8f91\u8fd0\u7b97\uff1a<\/p>\n<ul>\n<li>\n<p>\u8fd0\u7b97\u6570\uff1a<\/p>\n<\/li>\n<li>\n<p>\u771f(<code>true, yes, on, 1<\/code>)<\/p>\n<\/li>\n<li>\n<p>\u5047(false, no, off, 0)  <\/p>\n<\/li>\n<li>\n<p>\u4e0e\uff1a<code>&amp;&amp;<\/code>\u53ef\u7528\u4e8e\u4e58\u6cd5<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">1 && 1 = 1\n0 && 1 = 0\n0 && 0 = 0\n1 && 0 = 0<\/code><\/pre>\n<ul>\n<li>\u6216\uff1a<code>||<\/code><\/li>\n<\/ul>\n<pre><code class=\"language-bash\">1 || 1 = 1\n0 || 1 = 1\n0 || 0 = 0\n1 || 0 = 1<\/code><\/pre>\n<ul>\n<li>\u975e\uff1a<code>!<\/code><\/li>\n<\/ul>\n<pre><code class=\"language-bash\">!1 = 0\n!0 = 1<\/code><\/pre>\n<ul>\n<li>\u5f02\u6216\uff1a<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u77ed\u8def\u6cd5\u5219\uff1a<\/p>\n<ul>\n<li><code>~]# COMMAND1 &amp;&amp; COMMAND2<\/code><\/li>\n<\/ul>\n<p>COMMAND1\u4e3a\u5047\uff0c\u5219COMMAND2\u4e0d\u4f1a\u518d\u6267\u884c\uff1b\u5426\u5219\u5fc5\u987b\u6267\u884c<\/p>\n<ul>\n<li><code>~]# COMMAND1 || COMMAND2<\/code><\/li>\n<\/ul>\n<p>COMMAND1\u4e3a\u771f\uff0c\u5219COMMAND2\u4e0d\u4f1a\u518d\u6267\u884c\uff1b\u5426\u5219\u5fc5\u987b\u6267\u884c<\/p>\n<ul>\n<li>\u793a\u4f8b\uff1a\u67e5\u770b\u67d0\u7528\u6237id,\u5982\u679c\u67e5\u770b\u5931\u8d25(\u5373\u4e0d\u5b58\u5728\u8be5\u7528\u6237)\uff0c\u90a3\u4e48\u521b\u5efa\u4e4b<\/li>\n<\/ul>\n<p><code>~]# id $username || useradd $username<\/code><\/p>\n<\/li>\n<\/ul>\n<h2>2.shell\u811a\u672c\u7f16\u7a0b<\/h2>\n<h3>1.\u6982\u8ff0<\/h3>\n<p><strong>\u7f16\u7a0b\u8bed\u8a00\u7684\u5206\u7c7b:\u6839\u636e\u8fd0\u884c\u65b9\u5f0f<\/strong><\/p>\n<ul>\n<li>\u7f16\u8bd1\u8fd0\u884c:\u6e90\u4ee3\u7801 --&gt; \u7f16\u8bd1\u5668 ( \u7f16\u8bd1)-&gt; \u7a0b\u5e8f\u6587\u4ef6;<\/li>\n<li>\u89e3\u91ca\u8fd0\u884c:\u6e90\u4ee3\u7801 -&gt; \u8fd0\u884c\u65f6\u542f\u52a8\u89e3\u91ca\u5668\uff0c\u7531\u89e3\u91ca\u5668\u8fb9\u89e3\u91ca\u8fb9\u8fd0\u884c<\/li>\n<\/ul>\n<p><strong>\u6839\u636e\u5176\u7f16\u7a0b\u8fc7\u7a0b\u4e2d\u529f\u80fd\u7684\u5b9e\u73b0\u662f\u8c03\u7528\u5e93\u8fd8\u662f\u8c03\u7528\u5916\u90e8\u7684\u7a0b\u5e8f\u6587\u4ef6<\/strong>:<\/p>\n<ul>\n<li>shell\u811a\u672c\u7f16\u7a0b:\u5229\u7528\u7cfb\u7edf\u4e0a\u7684\u547d\u4ee4\u53ca\u7f16\u7a0b\u7ec4\u4ef6\u8fdb\u884c\u7f16\u7a0b;<\/li>\n<li>\u5b8c\u6574\u7f16\u7a0b:\u5229\u7528\u5e93\u6216\u7ed9\u7a0b\u7ec4\u4ef6\u8fdb\u884c\u7f16\u7a0b:<\/li>\n<\/ul>\n<p><strong>\u7f16\u7a0b\u6a21\u578b<\/strong>:\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\u8bed\u8a00\uff0c\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00<\/p>\n<ul>\n<li>\n<p>\u8fc7\u7a0b\u5f0f:\u4ee5\u6307\u4ee4\u4e3a\u4e2d\u5fc3\u6765\u7ec4\u7ec7\u4ee3\u7801\uff0c\u6570\u636e\u662f\u670d\u52a1\u4e8e\u4ee3\u7801;<\/p>\n<p>\u987a\u5e8f\u6267\u884c\u3001\u9009\u62e9\u6267\u884c\u3001\u5faa\u73af\u6267\u884c ... ...<\/p>\n<p>\u4ee3\u8868:C\uff0cbash<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u8c61\u5f0f:\u4ee5\u6570\u636e\u4e3a\u4e2d\u5fc3\u6765\u7ec4\u7ec7\u4ee3\u7801\uff0c\u56f4\u7ed5\u6570\u636e\u6765\u7ec4\u7ec7\u6307\u4ee4;<\/p>\n<ul>\n<li>\u7c7b(class):\u5b9e\u4f8b\u5316\u5bf9\u8c61\uff0cmethod;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u4ee3\u8868: Java,C++,Python<br \/>\n<strong>shell\u811a\u672c\u7f16\u7a0b<\/strong>:\u8fc7\u7a0b\u5f0f\u7f16\u7a0b\uff0c\u89e3\u91ca\u8fd0\u884c\uff0c\u4f9d\u8d56\u4e8e\u5916\u90e8\u7a0b\u5e8f\u6587\u4ef6\u8fd0\u884c;<\/p>\n<p><strong>shell\u811a\u672c\u662f\u4ec0\u4e48\uff1a<\/strong>\u547d\u4ee4\u7684\u5806\u780c\uff0c\u81ea\u4e0a\u800c\u4e0b\u6267\u884c\uff1b\u4f46\u7531\u4e8e\u5f88\u591a\u547d\u4ee4\u4e0d\u5177\u6709\u5e42\u7b49\u6027\uff0c\u9700\u8981\u7528\u7a0b\u5e8f\u903b\u8f91\u6765\u5224\u65ad\u8fd0\u884c\u6761\u4ef6\u662f\u5426\u6ee1\u8db3\uff0c\u4ee5\u907f\u514d\u5176\u8fd0\u884c\u4e2d\u53d1\u751f\u9519\u8bef\u3002<\/p>\n<h3>2.\u5982\u4f55\u7f16\u5199<\/h3>\n<p>\u811a\u672c\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\uff0c\u9876\u683c\uff1a\u7ed9\u51fashebang,\u89e3\u91ca\u5668\u8def\u5f84\uff0c\u7528\u4e8e\u6307\u660e\u89e3\u91ca\u6267\u884c\u5f53\u524d\u811a\u672c\u7684\u89e3\u91ca\u5668\u7a0b\u5e8f\u6587\u4ef6<\/p>\n<p>\u5e38\u89c1\u7684\u89e3\u91ca\u5668\uff1a<\/p>\n<ul>\n<li><code>#!\/bin\/bash<\/code><\/li>\n<li><code>#!\/usr\/bin\/python<\/code><\/li>\n<li><code>#!\/usr\/bin\/perl<\/code><\/li>\n<\/ul>\n<p><strong>\u6587\u672c\u7f16\u8f91\u5668<\/strong>\uff1anano<\/p>\n<p>\u884c\u7f16\u8f91\u5668\uff1ased<\/p>\n<p>\u5168\u5c4f\u5e55\u7f16\u8f91\u5668\uff1anano, vi, vim<\/p>\n<p><strong>\u5c1d\u8bd5\u7528nano\u7f16\u5199\u4e00\u4e2ashell\u811a\u672c\uff1a<\/strong><\/p>\n<pre><code class=\"language-bash\">nano myfirst.sh<\/code><\/pre>\n<p>\u770b\u89c1\u5927\u6982\u957f\u8fd9\u6837\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230530160415602.png\" alt=\"image-20230530160415602\" \/><\/p>\n<p>\u4f7f\u7528<code>Ctrl+X<\/code>\u9000\u51fa\uff0c<code>Ctrl+O<\/code>\u5199\u5165<\/p>\n<p>\u6267\u884c\uff1a<\/p>\n<pre><code class=\"language-bash\">[root@VM-4-10-centos ~]# sh myfirst.sh\n\u66f4\u6539\u7528\u6237 user3 \u7684\u5bc6\u7801 \u3002\npasswd\uff1a\u6240\u6709\u7684\u8eab\u4efd\u9a8c\u8bc1\u4ee4\u724c\u5df2\u7ecf\u6210\u529f\u66f4\u65b0\u3002\n\/tmp\/test.uFOOSC\n\n# \u6216\u8005\u4f7f\u7528\u8def\u5f84\uff0c.\/myfirst.sh\uff0c \/root\/myfirst.sh\uff0c bash myfirst.sh<\/code><\/pre>\n<p>\u521a\u521a\u7684shell\u811a\u672c\u4e2d\uff0c\u5982\u679c\u518d\u6b21\u6267\u884c\uff0c\u521b\u5efa\u4f1a\u5931\u8d25\uff0c\u4f46\u662f\u540e\u4e24\u6b65\u5e76\u4e0d\u4f1a\u7ec8\u6b62<\/p>\n<p>\u5982\u679c\u60f3\u8981\u5224\u65ad\u7528\u6237\u5b58\u5728\uff0c\u82e5\u5b58\u5728\u5219\u4e0d\u518d\u521b\u5efa\uff0c\u53ea\u6267\u884c\u540e\u7eed\u7684\u6539\u5bc6\u7801\u7b49\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>||\u4e0e<\/code>\uff0cCOMMAND1\u6210\u529f\u5219\u4e0d\u518d\u6267\u884cCOMMAND2<\/p>\n<pre><code class=\"language-shell\">#!\/bin\/bash\n# \u6dfb\u52a0\u7528\u6237\u5e76\u8bbe\u7f6e\u5bc6\u7801\nid user3 || useradd user3\necho &quot;user3&quot; | passwd --stdin user3\n\nmktemp -d \/tmp\/test.XXXXXX<\/code><\/pre>\n<pre><code class=\"language-shell\">[root@VM-4-10-centos ~]# sh myfirst.sh\nuid=1005(user3) gid=1005(user3) \u7ec4=1005(user3)\n\u66f4\u6539\u7528\u6237 user3 \u7684\u5bc6\u7801 \u3002\npasswd\uff1a\u6240\u6709\u7684\u8eab\u4efd\u9a8c\u8bc1\u4ee4\u724c\u5df2\u7ecf\u6210\u529f\u66f4\u65b0\u3002\n\/tmp\/test.t6oD2U<\/code><\/pre>\n<h3>3.\u6267\u884c\u811a\u672c\u7684\u65b9\u6cd5<\/h3>\n<ol>\n<li>\n<p>\u8d4b\u4e88\u6267\u884c\u6743\u9650\uff0c\u5e76\u76f4\u63a5\u8fd0\u884c\u6b64\u7a0b\u5e8f\u6587\u4ef6:<\/p>\n<pre><code class=\"language-shell\">chmod +x \/PATH\/TO\/SCRIPT_FILE\n\/PATH\/TO\/SCRIPT_FILE<\/code><\/pre>\n<\/li>\n<li>\n<p>\u76f4\u63a5\u8fd0\u884c\u89e3\u91ca\u5668\uff0c\u5c06\u811a\u672c\u4ee5\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u9012\u7ed9\u89e3\u91ca\u5668\u7a0b\u5e8f<\/p>\n<pre><code class=\"language-shell\">bash \/PATH\/TO\/SCRIPT_FILE\nsh \/PATH\/TO\/SCRIPT_FILE<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>4.\u7b80\u5355\u7ec3\u4e60<\/h3>\n<p><strong><em>\u7ec3\u4e601\uff1a\u663e\u793a\/etc\u76ee\u5f55\u4e0b\u6240\u6709\u4ee5\u5927\u5199P\u6216\u8005\u5c0f\u5199p\u5f00\u5934\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u672c\u8eab;<\/em><\/strong><\/p>\n<pre><code class=\"language-shell\">#!\/bin\/bash\nll \/etc\/P* # \u5927\u5199\nll \/etc\/p* # \u5c0f\u5199\nll \/etc\/[Pp]* # \u4e0d\u533a\u5206\u5927\u5c0f\u5199<\/code><\/pre>\n<p><strong><em>\u7ec3\u4e602\uff1a\u663e\u793a\/var\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u6216\u8005\u76ee\u5f55\u672c\u8eab\uff0c\u5e76\u5c06\u663e\u793a\u7ed3\u679c\u4e2d\u7684\u5c0f\u5199\u5b57\u6bcd\u8f6c\u6362\u4e3a\u5927\u5199\u540e\u663e\u793a;<\/em><\/strong><\/p>\n<pre><code class=\"language-shell\">#!\/bin\/bash\nls -d \/var\/* | tr &#039;z-z&#039; &#039;A-Z&#039;<\/code><\/pre>\n<p><strong><em>\u7ec3\u4e603\uff1a\u521b\u5efa\u4e34\u65f6\u6587\u4ef6\/tem\/myfile.XXXX;<\/em><\/strong><\/p>\n<pre><code class=\"language-shell\">#!\/bin\/bash\nmkdir \/tem\/myfile.XXXX<\/code><\/pre>\n<p><strong><em>\u7efc\u5408<\/em><\/strong><\/p>\n<pre><code class=\"language-shell\">#!\/bin\/bash\necho &quot;Show some under \/etc&quot;\nls -d \/etc\/[pP]*\necho &quot;Traslate lower to upper&quot;\nls -d \/var\/*tr &#039;a-z&#039;&#039;A-Z&#039;\necho &quot;Create a temp file&quot;\nmktemp \/tmp\/myfile.XXXX<\/code><\/pre>\n<h3>5.bash\u7684\u914d\u7f6e\u6587\u4ef6<\/h3>\n<h4>1. \u914d\u7f6e\u6587\u4ef6\u7c7b\u578b<\/h4>\n<p><strong>\u914d\u7f6e\u6587\u4ef6\u4e3b\u8981\u5206\u4e3a\u4e24\u7c7b\uff1a<\/strong><\/p>\n<p><strong>profile\u7c7b\uff1a<\/strong>\u4e3a\u4ea4\u4e92\u5f0f\u767b\u5f55\u7684shelli\u8fdb\u7a0b\u63d0\u4f9b\u914d\u7f6e<\/p>\n<p><strong>bashrc\u7c7b\uff1a<\/strong>\u4e3a\u975e\u4ea4\u4e92\u5f0f\u767b\u5f55\u7684shelli\u8fdb\u7a0b\u63d0\u4f9b\u914d\u7f6e<\/p>\n<p><strong>\u767b\u5f55\u7c7b\u578b\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u4ea4\u4e92\u5f0f\u767b\u5f55shelli\u8fdb\u7a0b\uff1a<br \/>\n\u76f4\u63a5\u901a\u8fc7\u67d0\u7ec8\u7aef\u8f93\u5165\u8d26\u53f7\u548c\u5bc6\u7801\u540e\u767b\u5f55\u6253\u5f00\u7684shelli\u8fdb\u7a0b\uff1a<br \/>\n\u4f7f\u7528su\u547d\u4ee4\uff1a<code>Su - USERNAME<\/code>,\u6216\u8005\u4f7f\u7528<code>su -I USERNAME<\/code>\u6267\u884c\u7684\u767b\u5f55\u5207\u6362\uff1a<\/p>\n<\/li>\n<li>\n<p>\u975e\u4ea4\u4e92\u5f0f\u767b\u5f55shel\u8fdb\u7a0b<br \/>\n<code>su USERNAME<\/code>\u6267\u884c\u7684\u767b\u5f55\u5207\u6362\uff1a<br \/>\n\u56fe\u5f62\u754c\u9762\u4e0b\u6253\u5f00\u7684\u7ec8\u7aef\uff1a<br \/>\n\u8fd0\u884c\u811a\u672c<\/p>\n<\/li>\n<\/ul>\n<p><strong>profile\u7c7b\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u5168\u5c40\uff1a\u5bf9\u6240\u6709\u7528\u6237\u751f\u6548<\/p>\n<pre><code class=\"language-shell\">\/etc\/profile\n\/etc\/profild.d\/*.sh<\/code><\/pre>\n<\/li>\n<li>\n<p>\u7528\u6237\u4e2a\u4eba\uff1a\u4ec5\u5bf9\u5f53\u524d\u7528\u6237\u6709\u6548<\/p>\n<p><code>~\/.bash_profile<\/code><\/p>\n<\/li>\n<li>\n<p>\u529f\u7528\uff1a<\/p>\n<ol>\n<li>\u7528\u4e8e\u5b9a\u4e49\u73af\u5883\u53d8\u91cf<\/li>\n<li>\u8fd0\u884c\u547d\u4ee4\u6216\u811a\u672c<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p><strong>bashrc\u7c7b\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u5168\u5c40\uff1a<\/p>\n<p><code>\/etc\/bashrc<\/code><\/p>\n<\/li>\n<li>\n<p>\u7528\u6237\u4e2a\u4eba\uff1a<\/p>\n<p><code>~\/.bashrc<\/code><\/p>\n<\/li>\n<li>\n<p>\u529f\u7528\uff1a<\/p>\n<ol>\n<li>\u5b9a\u4e49\u672c\u5730\u53d8\u91cf<\/li>\n<li>\u5b9a\u4e49\u547d\u4ee4\u522b\u540d<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u4ec5\u7ba1\u7406\u5458\u53ef\u4fee\u6539\u5168\u5c40\u914d\u7f6e\u6587\u4ef6<\/p>\n<h4>2.<strong>shell\u8bfb\u53d6\u914d\u7f6e\u987a\u5e8f<\/strong><\/h4>\n<ul>\n<li>\u4ea4\u4e92\u5f0f\u767b\u5f55shell\u8fdb\u7a0b\uff1a<br \/>\n<code>\/etc\/profile--&gt;\/etc\/profile.d\/*--&gt;~\/.bash_profile -&gt;~\/.bashrc--&gt;\/etc\/bashrc<\/code><\/li>\n<li>\u975e\u4ea4\u4e92\u5f0f\u767b\u5f55shell:\u8fdb\u7a0b\uff1a<br \/>\n<code>-\/.bashrc--&gt;\/etc\/bashrc--&gt;\/etc\/profile.d\/*<\/code><\/li>\n<\/ul>\n<p>\u547d\u4ee4\u884c\u4e2d\u5b9a\u4e49\u7684\u7279\u6027\uff0c\u4f8b\u5982\u53d8\u91cf\u548c\u522b\u540d\u4f5c\u7528\u57df\u4e3a\u5f53\u524dshl\u8fdb\u7a0b\u7684\u751f\u547d\u5468\u671f;<br \/>\n\u914d\u7f6e\u6587\u4ef6\u5b9a\u4e49\u7684\u7279\u6027\uff0c\u53ea\u5bf9\u968f\u540e\u65b0\u542f\u52a8\u7684shell\u8fdb\u7a0b\u6709\u6548\uff1a<br \/>\n\u8ba9\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u5b9a\u4e49\u7684\u7279\u6027\u7acb\u5373\u751f\u6548\uff1a<\/p>\n<ol>\n<li>\n<p>\u901a\u8fc7\u547d\u4ee4\u884c\u91cd\u590d\u5b9a\u4e49\u4e00\u6b21\uff1a<\/p>\n<\/li>\n<li>\n<p>\u8ba9shelli\u8fdb\u7a0b\u91cd\u8bfb\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-shell\">source \/PATH\/FROM\/CONF_FILE\n\/PATH\/FROM\/CONF_FILE<\/code><\/pre>\n<\/li>\n<\/ol>\n<h4>3.\u5b9e\u9a8c<\/h4>\n<p><strong>\u522b\u540d\u6d4b\u8bd5\uff1a\u5c06clear\u5199\u5230\u914d\u7f6e\u6587\u4ef6\u4e2d\u522b\u540d\u4e3acls<\/strong><\/p>\n<p>\u76f4\u63a5\u6572cls\u662f\u65e0\u8be5\u547d\u4ee4\u7684ll,\u5c06clear\u522b\u540d\u6210cls<\/p>\n<pre><code>[lemon@VM-4-10-centos ~]$ cls\n-bash: cls: \u672a\u627e\u5230\u547d\u4ee4\n[lemon@VM-4-10-centos ~]$ vim .bashrc<\/code><\/pre>\n<p>\u6dfb\u52a0<code>alias cls=clear<\/code>:<\/p>\n<pre><code>[lemon@VM-4-10-centos ~]$ cat .bashrc\n# .bashrc\n\n# Source global definitions\nif [ -f \/etc\/bashrc ]; then\n        . \/etc\/bashrc\nfi\nVIRTUALENVWRAPPER_PYTHON=\/usr\/bin\/python3\nexport WORKON_HOME=$HOME\/.virtualenvs\nsource \/home\/lemon\/.local\/bin\/virtualenvwrapper.sh\n\nalias cls=clear\n# Uncomment the following line if you don&#039;t like systemctl&#039;s auto-paging feature:\n# export SYSTEMD_PAGER=\n\n# User specific aliases and functions<\/code><\/pre>\n<p>\u518d\u6267\u884ccls\uff0c\u5c31OK\u4e86\u3002\u5b9e\u6d4b\u6709\u6548\uff0c\u4f46\u662froot\u662f\u6267\u884c\u4e0d\u4e86\u7684\uff0c\u5168\u5c40\u7684\u8bdd\u53bb\u4fee\u6539<code>\/etc\/profile<\/code><\/p>\n<p>\u6bd4\u5982\u5199\u4e2a\u6b22\u8fce\u767b\u9646\u7684\u5168\u5c40\u53d8\u91cf\u811a\u672c\uff1a<\/p>\n<pre><code class=\"language-shell\">[root@VM-4-10-centos ~]# vim \/etc\/profile.d\/welcome.sh\n[root@VM-4-10-centos ~]# cat \/etc\/profile.d\/welcome.sh\necho &quot;\u5929\u4e0a\u4eba\u95f4\u7406\u7597\u4f1a\u6240\u5168\u4f53\u4f73\u4e3d\uff0c\u6b22\u8fce\u8d35\u5bbe\u5149\u4e34\uff01&quot;<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20230530174614977.png\" alt=\"image-20230530174614977\" \/> <\/p>\n<p>\u4e5f\u53ef\u4ee5\u7528\u91cd\u5b9a\u5411\u5199\uff1a<code>echo &#039;echo &quot;welcome!&quot;&#039; &gt; \/etc\/profile.d\/welcome.sh<\/code><\/p>\n<hr \/>\n<p><strong>export<\/strong>\uff1a\u5bfc\u51fa\u73af\u5883\u53d8\u91cf<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.bash\u7279\u6027\u53cabash\u811a\u672c\u7f16\u7a0b\u521d\u6b65 \u7ec8\u7aef\uff1a\u9644\u7740\u5728\u7ec8\u7aef\u7684\u63a5\u53e3\u7a0b\u5e8f GUI: KDE, GNome, Xfce CLI: \/etc\/sh   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[131],"tags":[15,138],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8789"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8789"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8789\/revisions"}],"predecessor-version":[{"id":8790,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8789\/revisions\/8790"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8789"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}