{"id":403,"date":"2021-07-28T17:34:33","date_gmt":"2021-07-28T09:34:33","guid":{"rendered":"https:\/\/egonlin.com\/?p=403"},"modified":"2021-07-30T10:15:49","modified_gmt":"2021-07-30T02:15:49","slug":"%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=403","title":{"rendered":"\u51fd\u6570"},"content":{"rendered":"<h1>\u51fd\u6570<\/h1>\n<h2>\u4e00 \u51fd\u6570\u4ecb\u7ecd<\/h2>\n<h3>\u4ec0\u4e48\u662f\u51fd\u6570\uff1f\uff1f\uff1f<\/h3>\n<pre><code class=\"language-python\">\u51fd\u6570\u5c31\u662f\u7528\u6765\u76db\u653e\u4e00\u7ec4\u4ee3\u7801\u7684\u5bb9\u5668\uff0c\u51fd\u6570\u5185\u7684\u4e00\u7ec4\u4ee3\u7801\u5b8c\u6210\u4e00\u4e2a\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u79f0\u4e4b\u4e3a\u4e00\u7ec4\u4ee3\u7801\u5757\uff0c\u8c03\u7528\u51fd\u6570\u4fbf\u53ef\u89e6\u53d1\u51fd\u6570\u5185\u4ee3\u7801\u5757\u7684\u8fd0\u884c\uff0c\u8fd9\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u590d\u7528\uff0c\u6240\u4ee5\u51fd\u6570\u53c8\u53ef\u4ee5\u79f0\u4e4b\u4e3a\u4e00\u4e2a\u5de5\u5177<\/code><\/pre>\n<h3>\u4e3a\u4f55\u8981\u7528\u51fd\u6570<\/h3>\n<pre><code class=\"language-python\">#1\u3001\u51cf\u5c11\u4ee3\u7801\u5197\u4f59\n#2\u3001\u63d0\u5347\u4ee3\u7801\u7684\u7ec4\u7ec7\u7ed3\u6784\u6027\u3001\u53ef\u8bfb\u6027\n#3\u3001\u589e\u5f3a\u6269\u5c55\u6027<\/code><\/pre>\n<h2>\u4e8c \u51fd\u6570\u7684\u57fa\u672c\u4f7f\u7528<\/h2>\n<pre><code class=\"language-python\">\u5177\u5907\u67d0\u4e00\u529f\u80fd\u7684\u5de5\u5177=&gt;\u51fd\u6570\n\u4e8b\u5148\u51c6\u5907\u597d\u54e6\u5de5\u5177=&gt;\u51fd\u6570\u7684\u5b9a\u4e49\n\u9047\u5230\u5e94\u7528\u573a\u666f\uff0c\u62ff\u6765\u5c31\u7528=&gt;\u51fd\u6570\u7684\u8c03\u7528\n\n\u6240\u4ee5\u51fd\u6570\u7684\u4f7f\u7528\u539f\u5219\uff1a\u5148\u5b9a\u4e49\uff0c\u540e\u8c03\u7528<\/code><\/pre>\n<h3>\u5b9a\u4e49\u51fd\u6570<\/h3>\n<pre><code class=\"language-python\">#\u8bed\u6cd5\uff1a\n[ function ] funname [()]\n{\n    \u547d\u4ee41;\n    \u547d\u4ee42;\n    \u547d\u4ee43;\n    ...\n    [return int;]\n}\n\n# \u793a\u4f8b1\uff1a\u5b8c\u6574\u5199\u6cd5\nfunction \u51fd\u6570\u540d() {\n    \u51fd\u6570\u8981\u5b9e\u73b0\u7684\u529f\u80fd\u4ee3\u7801\n}\n\n# \u793a\u4f8b2\uff1a\u7701\u7565\u5173\u952e\u5b57(),\u6ce8\u610f\u6b64\u65f6\u4e0d\u80fd\u7701\u7565\u5173\u952e\u5b57function\nfunction \u51fd\u6570\u540d {\n    \u51fd\u6570\u8981\u5b9e\u73b0\u7684\u529f\u80fd\u4ee3\u7801\n}\n\n# \u793a\u4f8b3\uff1a\u7701\u7565\u5173\u952e\u5b57function\n\u51fd\u6570\u540d() {\n    \u51fd\u6570\u8981\u5b9e\u73b0\u7684\u529f\u80fd\u4ee3\u7801\n}<\/code><\/pre>\n<h3>\u8c03\u7528\u51fd\u6570<\/h3>\n<pre><code class=\"language-python\"># \u8bed\u6cd5\uff1a\n\u51fd\u6570\u540d  # \u65e0\u53c2\u8c03\u7528\n\u51fd\u6570\u540d \u53c2\u65701 \u53c2\u65702  # \u6709\u53c2\u8c03\u7528\n\n# \u793a\u4f8b\nfunction test1(){\n    echo &quot;\u6267\u884c\u7b2c\u4e00\u4e2a\u51fd\u6570&quot;\n}\n\nfunction test2 {\n    echo &quot;\u6267\u884c\u7b2c\u4e8c\u4e2a\u51fd\u6570&quot;\n}\n\ntest3(){\n    echo &quot;\u6267\u884c\u7b2c\u4e09\u4e2a\u51fd\u6570&quot;\n}\n\n# \u8c03\u7528\u51fd\u6570\uff1a\u76f4\u63a5\u5f15\u7528\u51fd\u6570\u540d\u5b57\u5373\u8c03\u7528\u51fd\u6570\uff0c\u4f1a\u89e6\u53d1\u51fd\u6570\u5185\u4ee3\u7801\u7684\u8fd0\u884c\ntest1\ntest2\ntest3\n\n# ps\uff1a\u5173\u4e8e\u6709\u53c2\u8c03\u7528\u89c1\u4e0b\u4e00\u5c0f\u8282<\/code><\/pre>\n<h2>\u4e09 \u51fd\u6570\u53c2\u6570<\/h2>\n<p>\u5982\u679c\u628a\u51fd\u6570\u5f53\u6210\u4e00\u5ea7\u5de5\u5382\uff0c\u51fd\u6570\u7684\u53c2\u6570\u5c31\u662f\u4e3a\u5de5\u5382\u8fd0\u9001\u7684\u539f\u6750\u6599<\/p>\n<ul>\n<li>\n<p>\u8c03\u7528\u51fd\u6570\u65f6\u53ef\u4ee5\u5411\u5176\u4f20\u9012\u53c2\u6570<\/p>\n<pre><code class=\"language-python\"># \u8c03\u7528\u51fd\u6570test1\uff0c\u5728\u5176\u540e\u4ee5\u7a7a\u683c\u4e3a\u5206\u9694\u7b26\u4f9d\u6b21\u7f57\u5217\u53c2\u6570 \ntest1 111 222 333 444 555<\/code><\/pre>\n<\/li>\n<li>\n<p>\u5728\u51fd\u6570\u4f53\u5185\u90e8\uff0c\u901a\u8fc7 <code>$n<\/code> \u7684\u5f62\u5f0f\u6765\u83b7\u53d6\u53c2\u6570\u7684\u503c\uff0c\u4f8b\u5982\uff0c<code>$1<\/code>\u8868\u793a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c<code>$2<\/code>\u8868\u793a\u7b2c\u4e8c\u4e2a\u53c2\u6570&#8230;\u5f53n&gt;=10\u65f6\uff0c\u9700\u8981\u4f7f\u7528<code>${n}<\/code>\u6765\u83b7\u53d6\u53c2\u6570<\/p>\n<pre><code class=\"language-python\">[root@egon ~]# cat b.sh \nfunction test1(){\n  echo \"...start...\"\n  echo $1\n  echo $2\n  echo $3\n  echo \"...end...\"\n}\n\ntest1 111 222 333 444 555  # \u4e3a\u51fd\u6570\u4f53\u4f20\u53c2\n\n[root@egon ~]# .\/b.sh \n...start...\n111\n222\n333\n...end...<\/code><\/pre>\n<p>ps\uff1a\u5728\u811a\u672c\u5185\u83b7\u53d6\u811a\u672c\u8c03\u7528\u8005\u5728\u547d\u4ee4\u884c\u91cc\u4e3a\u811a\u672c\u4f20\u5165\u7684\u53c2\u6570\uff0c\u540c\u6837\u4f7f\u7528\u7684\u662f$n\uff0c\u4e0d\u8981\u641e\u6df7<\/p>\n<pre><code class=\"language-python\">[root@egon ~]# cat b.sh \nfunction test1(){\n  echo \"...start...\"\n  echo \"\u8fd9\u662f\u51fd\u6570\u5185:$1\"\n  echo \"\u8fd9\u662f\u51fd\u6570\u5185:$2\"\n  echo \"\u8fd9\u662f\u51fd\u6570\u5185:$3\"\n  echo \"...end...\"\n}\n\n# test1 111 222 333 444 555\ntest1\n\necho \"\u8fd9\u662f\u811a\u672c\u7ea7\u7684\u53c2\u6570$1\"\necho \"\u8fd9\u662f\u811a\u672c\u7ea7\u7684\u53c2\u6570$2\"\necho \"\u8fd9\u662f\u811a\u672c\u7ea7\u7684\u53c2\u6570$3\"\n\n[root@egon ~]# \n[root@egon ~]# \n[root@egon ~]# .\/b.sh xxx yyy zzz mmm nnn\n...start...\n\u8fd9\u662f\u51fd\u6570\u5185:\n\u8fd9\u662f\u51fd\u6570\u5185:\n\u8fd9\u662f\u51fd\u6570\u5185:\n...end...\n\u8fd9\u662f\u811a\u672c\u7ea7\u7684\u53c2\u6570xxx\n\u8fd9\u662f\u811a\u672c\u7ea7\u7684\u53c2\u6570yyy\n\u8fd9\u662f\u811a\u672c\u7ea7\u7684\u53c2\u6570zzz\n[root@egon ~]# <\/code><\/pre>\n<\/li>\n<\/ul>\n<p><strong>\u6e29\u6545\u77e5\u65b0<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c2\u6570\u5904\u7406<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>$#<\/td>\n<td>\u4f20\u9012\u5230\u811a\u672c\u6216\u51fd\u6570\u7684\u53c2\u6570\u4e2a\u6570<\/td>\n<\/tr>\n<tr>\n<td>$*<\/td>\n<td>\u6240\u6709\u53c2\u6570<\/td>\n<\/tr>\n<tr>\n<td>$@<\/td>\n<td>\u6240\u6709\u53c2\u6570\uff0c\u4e0e$*\u7c7b\u4f3c<\/td>\n<\/tr>\n<tr>\n<td>$$<\/td>\n<td>\u5f53\u524d\u811a\u672c\u8fdb\u7a0b\u7684ID\u53f7<\/td>\n<\/tr>\n<tr>\n<td>$?<\/td>\n<td>\u83b7\u53d6\u4e0a\u4e00\u6761\u547d\u4ee4\u6267\u884c\u5b8c\u6bd5\u540e\u7684\u9000\u51fa\u72b6\u6001\u30020\u8868\u793a\u6b63\u786e\uff0c\u975e0\u4ee3\u8868\u9519\u8bef\uff0c\u5982\u679c\u6267\u884c\u7684\u662f\u51fd\u6570\u90a3\u4e48$?\u53d6\u7684\u662f\u51fd\u6570\u4f53\u5185return\u540e\u7684\u503c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>ps\uff1a<\/p>\n<pre><code class=\"language-python\">#1\u3001\u5f53$*\u548c$@\u6ca1\u6709\u88ab\u5f15\u53f7\u5f15\u7528\u8d77\u6765\u7684\u65f6\u5019\uff0c\u5b83\u4eec\u786e\u5b9e\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u90fd\u4f1a\u628a\u4f4d\u7f6e\u53c2\u6570\u5f53\u6210\u4e00\u4e2a\u4e2a\u4f53\u3002\n\n#2\u3001&quot;$*&quot; \u4f1a\u628a\u6240\u6709\u4f4d\u7f6e\u53c2\u6570\u5f53\u6210\u4e00\u4e2a\u6574\u4f53\uff08\u6216\u8005\u8bf4\u5f53\u6210\u4e00\u4e2a\u5355\u8bcd\uff09\uff0c\u5982\u679c\u6ca1\u6709\u4f4d\u7f6e\u53c2\u6570\uff0c\u5219&quot;$*&quot;\u4e3a\u7a7a\uff0c\u5982\u679c\u6709\u4e24\u4e2a\u4f4d\u7f6e\u53c2\u6570\u5e76\u4e14\u5206\u9694\u7b26\u4e3a\u7a7a\u683c\u65f6\uff0c&quot;$*&quot;\u76f8\u5f53\u4e8e&quot;$1 $2&quot;\n\n#3\u3001&quot;$@&quot;  \u4f1a\u628a\u6240\u6709\u4f4d\u7f6e\u53c2\u6570\u5f53\u6210\u4e00\u4e2a\u5355\u72ec\u7684\u5b57\u6bb5\uff0c\u5982\u679c\u6ca1\u6709\u4f4d\u7f6e\u53c2\u6570\uff0c\u5219&quot;$@&quot;\u5c55\u5f00\u4e3a\u7a7a\uff08\u4e0d\u662f\u7a7a\u5b57\u7b26\u4e32\uff0c\u800c\u662f\u7a7a\u5217\u8868\uff09\uff0c\u5982\u679c\u5b58\u5728\u4e00\u4e2a\u4f4d\u7f6e\u53c2\u6570\uff0c\u5219&quot;$@&quot;\u76f8\u5f53\u4e8e&quot;$1&quot;\uff0c\u5982\u679c\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u5219&quot;$@&quot;\u76f8\u5f53\u4e8e&quot;$1&quot;  &quot;$2&quot;\u7b49\u7b49<\/code><\/pre>\n<p>\u793a\u4f8b<\/p>\n<pre><code class=\"language-python\">[root@egon ~]# cat b.sh \necho &quot;=======\u51fd\u6570test1===========&quot;\nfunction test1(){\n    echo &quot;$*&quot;  # 111 222 333 444 555\n    echo &quot;$@&quot;  # 111 222 333 444 555\n    echo $#    # 5\n    echo $$    # 87380\n    echo $?    # 0\n}\n\ntest1 111 222 333 444 555\n\necho &quot;=======\u51fd\u6570test2===========&quot;\nfunction test2(){\n    for i in &quot;$*&quot;  # \u6ce8\u610f:$*\u4e0d\u52a0\u5f15\u53f7\u7ed3\u679c\u4e0e$@\u4e00\u6a21\u4e00\u6837\n    do\n        echo $i\n    done\n}\n\ntest2 111 222 333 &quot;444 555&quot;  # \u6ce8\u610f&quot;444 555&quot;\u88ab\u5f15\u53f7\u5f15\u6210\u4e86\u4e00\u4e2a\u53c2\u6570\n# \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a111 222 333 444 555\n\necho &quot;=======\u51fd\u6570test3===========&quot;\nfunction test3(){\n    for i in &quot;$@&quot;  # \u6ce8\u610f:$*\u4e0d\u52a0\u5f15\u53f7\u7ed3\u679c\u4e0e$@\u4e00\u6a21\u4e00\u6837\n    do\n        echo $i\n    done\n}\n\ntest3 111 222 333 &quot;444 555&quot;  # \u6ce8\u610f&quot;444 555&quot;\u88ab\u5f15\u53f7\u5f15\u6210\u4e86\u4e00\u4e2a\u53c2\u6570\n# \u8fd0\u884c\u7ed3\u679c\u4e3a\uff1a\n# 111\n# 222\n# 333\n# 444 555<\/code><\/pre>\n<h2>\u56db \u51fd\u6570\u7684\u8fd4\u56de\u503c<\/h2>\n<p>\u5982\u679c\u628a\u51fd\u6570\u5f53\u6210\u4e00\u5ea7\u5de5\u5382\uff0c\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5c31\u662f\u5de5\u5382\u7684\u4ea7\u54c1\uff0c\u5728\u51fd\u6570\u5185\u4f7f\u7528return\u5173\u952e\u5b57\u8fd4\u56de\u503c\uff0c\u51fd\u6570\u5185\u53ef\u4ee5\u6709\u591a\u4e2areturn\uff0c\u4f46\u53ea\u8981\u6267\u884c\u4e00\u4e2a\uff0c\u6574\u4e2a\u51fd\u6570\u5c31\u4f1a\u7acb\u523b\u7ed3\u675f<\/p>\n<pre><code class=\"language-python\">[root@egon ~]# cat b.sh \nfunction test1(){\n    echo 111\n    return\n    echo 222\n    return \n    echo 333\n}\n\ntest1\n\n[root@egon ~]# chmod +x bbbb.sh \n[root@egon ~]# .\/b.sh \n111<\/code><\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662fshell\u8bed\u8a00\u7684\u51fd\u6570\u4e2d,\u901a\u5e38\u7528return\u8fd4\u56de\u51fd\u6570\u8fd0\u884c\u662f\u5426\u6210\u529f\u7684\u72b6\u6001\uff0c0\u4ee3\u8868\u6210\u529f\uff0c\u975e\u96f6\u4ee3\u8868\u5931\u8d25\uff0c\u9700\u8981\u7528$?\u83b7\u53d6\u51fd\u6570\u7684\u8fd4\u56de\u503c<\/p>\n<ul>\n<li>\n<p>1\u3001\u5982\u679c\u51fd\u6570\u5185\u6ca1\u6709return\uff0c\u90a3\u4e48\u5c06\u4ee5\u6700\u540e\u4e00\u6761\u547d\u4ee4\u8fd0\u884c\u7ed3\u679c\uff08\u547d\u4ee4\u8fd0\u884c\u6210\u529f\u7ed3\u679c\u4e3a0\uff0c\u5426\u5219\u4e3a\u975e0\uff09\u4f5c\u4e3a\u8fd4\u56de\u503c<\/p>\n<pre><code class=\"language-python\">[root@egon ~]# cat b.sh \nfunction test1(){\n  echo 111\n  echo 222\n  echo 333\n  xxx  # \u8fd0\u884c\u8be5\u547d\u4ee4\u51fa\u9519\n}\n\ntest1\necho $?\n[root@egon ~]# .\/b.sh \n111\n222\n333\n.\/b.sh:\u884c5: xxx: \u672a\u627e\u5230\u547d\u4ee4\n127<\/code><\/pre>\n<\/li>\n<li>\n<p>2\u3001\u5982\u679c\u51fd\u6570\u5185\u6709return\uff0c\u90a3\u4e48return\u540e\u8ddf\u7684\u53ea\u80fd\u662f\u6574\u578b\u503c\u5e76\u4e14\u8303\u56f4\u4e3a0-255\uff0c\u7528\u4e8e\u6807\u8bc6\u51fd\u6570\u7684\u8fd0\u884c\u7ed3\u679c\u662f\u5426\u6b63\u786e\uff0c \u4e0eC \u8bed\u8a00\u4e0d\u540c\uff0cshell \u8bed\u8a00\u4e2d 0 \u4ee3\u8868 true\uff0c0 \u4ee5\u5916\u7684\u503c\u4ee3\u8868 false<\/p>\n<pre><code class=\"language-python\">[root@egon ~]# cat b.sh \nfunction test1(){\n  echo 111\n  echo 222\n  echo 333\n  return 0\n}\n\ntest1\necho $?  # \u7528$?\u83b7\u53d6\u51fd\u6570\u7684\u8fd4\u56de\u503c\n[root@egon ~]# .\/b.sh \n111\n222\n333\n0<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2>\u4e94 \u53d8\u91cf\u7684\u4f5c\u7528\u57df<\/h2>\n<p>Shell \u53d8\u91cf\u7684\u4f5c\u7528\u57df\uff08Scope\uff09\uff0c\u5c31\u662f Shell \u53d8\u91cf\u7684\u6709\u6548\u8303\u56f4\uff08\u53ef\u4ee5\u4f7f\u7528\u7684\u8303\u56f4\uff09\u3002<\/p>\n<p><strong>1\u3001\u5c40\u90e8\u53d8\u91cf\uff1a\u53ea\u80fd\u5728\u51fd\u6570\u5185\u8bbf\u95ee<\/strong><\/p>\n<p>\u4f7f\u7528local\u5173\u952e\u5b57\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u7684\u53d8\u91cf\u5c5e\u4e8e\u5c40\u90e8\u8303\u56f4\uff0c\u53ea\u80fd\u5728\u51fd\u6570\u5185\u4f7f\u7528,\u5982\u4e0b\u6240\u793a<\/p>\n<pre><code class=\"language-python\">[root@localhost shell]# cat hello.sh \n#!\/bin\/bash\n\n# \u5b9a\u4e49\u51fd\u6570\nfunction test(){\n    local x=111\n    echo &quot;\u51fd\u6570\u5185\u8bbf\u95eex\uff1a$x&quot;\n}\n\n# \u8c03\u7528\u51fd\u6570\ntest\n\necho &quot;\u5728\u51fd\u6570\u5916\u5373\u5168\u5c40\u8bbf\u95eex\uff1a$x&quot;  # \u65e0\u6cd5\u8bbf\u95ee\u51fd\u6570\u5185\u7684\u5c40\u90e8\u53d8\u91cf<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c<\/p>\n<pre><code class=\"language-python\">[root@localhost shell]# .\/hello.sh \n\u51fd\u6570\u5185\u8bbf\u95eex\uff1a111\n\u5728\u51fd\u6570\u5916\u5373\u5168\u5c40\u8bbf\u95eex\uff1a<\/code><\/pre>\n<p><strong>2\u3001\u5168\u5c40\u53d8\u91cf\uff1a\u53ef\u4ee5\u5728\u5f53\u524dshell\u8fdb\u7a0b\u4e2d\u4f7f\u7528<\/strong><\/p>\n<p>\u6240\u8c13\u5168\u5c40\u53d8\u91cf\uff0c\u5c31\u662f\u6307\u53d8\u91cf\u5728\u5f53\u524d\u7684\u6574\u4e2a Shell \u8fdb\u7a0b\u4e2d\u90fd\u6709\u6548\u3002\u6bcf\u4e2a Shell \u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u4f5c\u7528\u57df\uff0c\u5f7c\u6b64\u4e4b\u95f4\u4e92\u4e0d\u5f71\u54cd\u3002\u5728 Shell \u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u9ed8\u8ba4\u5c31\u90fd\u662f\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<pre><code class=\"language-python\">[root@localhost shell]# cat hello.sh \n#!\/bin\/bash\nx=2222\n\nfunction test(){\n    echo &quot;\u51fd\u6570\u5185\u8bbf\u95eex\uff1a$x&quot;\n}\ntest\necho &quot;\u5728\u51fd\u6570\u5916\u5373\u5168\u5c40\u8bbf\u95eex\uff1a$x&quot; <\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c<\/p>\n<pre><code class=\"language-python\">[root@localhost shell]# .\/hello.sh \n\u51fd\u6570\u5185\u8bbf\u95eex\uff1a2222\n\u5728\u51fd\u6570\u5916\u5373\u5168\u5c40\u8bbf\u95eex\uff1a2222<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f<\/p>\n<ul>\n<li>\n<p>1\u3001\u5728\u51fd\u6570\u5185\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5982\u679c\u6ca1\u6709\u7528local\u58f0\u660e\uff0c\u90a3\u4e48\u9ed8\u8ba4\u4e5f\u662f\u5168\u5c40\u53d8\u91cf\uff0cshell\u53d8\u91cf\u8bed\u6cd5\u7684\u8be5\u7279\u6027\u4e0ejs\u7684\u53d8\u91cf\u662f\u7c7b\u4f3c\u7684\uff08\u5728js\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u9ed8\u8ba4\u4e5f\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u9664\u975e\u52a0\u4e0a\u5173\u952e\u5b57var\uff09<\/p>\n<pre><code class=\"language-python\">[root@localhost shell]# cat hello.sh \n#!\/bin\/bash\nfunction test(){\n  x=2222  # \u5168\u5c40\u53d8\u91cf\n}\ntest\necho \"\u5728\u51fd\u6570\u5916\u5373\u5168\u5c40\u8bbf\u95eex\uff1a$x\"  \n\n[root@localhost shell]# .\/hello.sh \n\u5728\u51fd\u6570\u5916\u5373\u5168\u5c40\u8bbf\u95eex\uff1a2222<\/code><\/pre>\n<\/li>\n<li>\n<p>2\u3001\u6bcf\u6267\u884c\u4e00\u4e2a\u89e3\u91ca\u5668\uff0c\u90fd\u4f1a\u5f00\u542f\u4e00\u4e2a\u89e3\u91ca\u7684shell\u8fdb\u7a0b\uff0c\u6bcf\u4e2ashell\u8fdb\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u4f5c\u7528\u57df\u5f7c\u6b64\u4e92\u4e0d\u5e72\u6270<\/p>\n<pre><code class=\"language-python\">[root@localhost shell]# x=111  # \u8be5\u53d8\u91cf\u4ec5\u4ec5\u53ea\u5728\u5f53\u524dshell\u8fdb\u7a0b\u4e2d\u6709\u6548\uff0c\u5bf9\u65b0\u7684shell\u8fdb\u7a0b\u65e0\u5f71\u54cd\n[root@localhost shell]# echo $x\n111\n[root@localhost shell]# bash  # \u6267\u884cbash\u89e3\u91ca\u5668\uff0c\u5219\u5f00\u542f\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\uff0c\u6216\u8005\u5e72\u8106\u76f4\u63a5\u6253\u5f00\u4e00\u4e2a\u65b0\u7684\u7ec8\u7aef\n[root@localhost shell]# echo $x\n\n[root@localhost shell]# <\/code><\/pre>\n<\/li>\n<li>\n<p>3\u3001\u9700\u8981\u5f3a\u8c03\u7684\u662f\uff1a<\/p>\n<p>\u5168\u5c40\u53d8\u91cf\u7684\u4f5c\u7528\u8303\u56f4\u662f\u5f53\u524d\u7684 Shell \u8fdb\u7a0b\uff0c\u800c\u4e0d\u662f\u5f53\u524d\u7684 Shell \u811a\u672c\u6587\u4ef6\uff0c\u5b83\u4eec\u662f\u4e0d\u540c\u7684\u6982\u5ff5\u3002\u6253\u5f00\u4e00\u4e2a Shell \u7a97\u53e3\u5c31\u521b\u5efa\u4e86\u4e00\u4e2a Shell \u8fdb\u7a0b\uff0c\u6253\u5f00\u591a\u4e2a Shell \u7a97\u53e3\u5c31\u521b\u5efa\u4e86\u591a\u4e2a Shell \u8fdb\u7a0b\uff0c\u6bcf\u4e2a Shell \u8fdb\u7a0b\u90fd\u662f\u72ec\u7acb\u7684\uff0c\u62e5\u6709\u4e0d\u540c\u7684\u8fdb\u7a0b ID\u3002\u5728\u4e00\u4e2a Shell \u8fdb\u7a0b\u4e2d\u53ef\u4ee5\u4f7f\u7528 source \u547d\u4ee4\u6267\u884c\u591a\u4e2a Shell \u811a\u672c\u6587\u4ef6\uff0c\u6b64\u65f6\u5168\u5c40\u53d8\u91cf\u5728\u8fd9\u4e9b\u811a\u672c\u6587\u4ef6\u4e2d\u90fd\u6709\u6548\u3002<\/p>\n<pre><code class=\"language-python\">[root@localhost shell]# echo $x\n\n[root@localhost shell]# cat hello.sh \n#!\/bin\/bash\nfunction test(){\n  x=2222  # \u5168\u5c40\u53d8\u91cf\n}\ntest\n\n[root@localhost shell]# source hello.sh  # \u5728\u5f53\u524dshell\u8fdb\u7a0b\u4e2d\u6267\u884c\uff0c\u4ea7\u751f\u4e00\u4e2a\u5168\u5c40\u53d8\u91cfx\n[root@localhost shell]# echo $x  # \u5728\u5f53\u524dshell\u8fdb\u7a0b\u4e2d\u8bbf\u95ee\u5168\u5c40\u53d8\u91cfx\uff0c\u53ef\u4ee5\u770b\u5230\n2222\n[root@localhost shell]# \n[root@localhost shell]# \n[root@localhost shell]# cat aaa.sh \n#!\/bin\/bash\necho $x\n\n[root@localhost shell]# source aaa.sh # \u5728\u5f53\u524dshell\u8fdb\u7a0b\u4e2d\u8bbf\u95ee\u5168\u5c40\u53d8\u91cfx\uff0c\u540c\u6837\u53ef\u4ee5\u770b\u5230\n2222<\/code><\/pre>\n<p>\u7ed3\u8bba\uff1a\u51fd\u6570test\u5185\u7684\u5168\u5c40\u53d8\u91cfx\u65e9\u5df2\u8d85\u8d8a\u4e86\u6587\u4ef6\uff0c\u5373\u5168\u5c40\u53d8\u91cf\u662f\u8d85\u8d8a\u6587\u4ef6\u7684\uff0c\u4f5c\u7528\u8303\u56f4\u662f\u6574\u4e2a\u5f53\u524dbash\u8fdb\u7a0b<\/p>\n<\/li>\n<\/ul>\n<p><strong>3\u3001\u73af\u5883\u53d8\u91cf\uff1a\u5728\u5f53\u524d\u8fdb\u7a0b\u7684\u5b50\u8fdb\u7a0b\u4e2d\u90fd\u53ef\u4ee5\u4f7f\u7528<\/strong><\/p>\n<p>\u5168\u5c40\u53d8\u91cf\u53ea\u5728\u5f53\u524d Shell \u8fdb\u7a0b\u4e2d\u6709\u6548\uff0c\u5bf9\u5176\u5b83 Shell \u8fdb\u7a0b\u548c\u5b50\u8fdb\u7a0b\u90fd\u65e0\u6548\u3002\u5982\u679c\u4f7f\u7528<code>export<\/code>\u547d\u4ee4\u5c06\u5168\u5c40\u53d8\u91cf\u5bfc\u51fa\uff0c\u90a3\u4e48\u5b83\u5c31\u5728\u6240\u6709\u7684\u5b50\u8fdb\u7a0b\u4e2d\u4e5f\u6709\u6548\u4e86\uff0c\u8fd9\u79f0\u4e3a\u201c\u73af\u5883\u53d8\u91cf\u201d\u3002<\/p>\n<p>\u73af\u5883\u53d8\u91cf\u88ab\u521b\u5efa\u65f6\u6240\u5904\u7684 Shell \u8fdb\u7a0b\u79f0\u4e3a\u7236\u8fdb\u7a0b\uff0c\u5982\u679c\u5728\u7236\u8fdb\u7a0b\u4e2d\u518d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u6765\u6267\u884c Shell \u547d\u4ee4\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65b0\u7684\u8fdb\u7a0b\u88ab\u79f0\u4f5c Shell \u5b50\u8fdb\u7a0b\u3002\u5f53 Shell \u5b50\u8fdb\u7a0b\u4ea7\u751f\u65f6\uff0c\u5b83\u4f1a\u7ee7\u627f\u7236\u8fdb\u7a0b\u7684\u73af\u5883\u53d8\u91cf\u4e3a\u81ea\u5df1\u6240\u7528\uff0c\u6240\u4ee5\u8bf4\u73af\u5883\u53d8\u91cf\u53ef\u4ece\u7236\u8fdb\u7a0b\u4f20\u7ed9\u5b50\u8fdb\u7a0b\u3002\u4e0d\u96be\u7406\u89e3\uff0c\u73af\u5883\u53d8\u91cf\u8fd8\u53ef\u4ee5\u4f20\u9012\u7ed9\u5b59\u8fdb\u7a0b\u3002<\/p>\n<pre><code class=\"language-python\">[root@localhost shell]# export y=333  # \u7237\u7237\n[root@localhost shell]# bash  # \u7238\u7238\n[root@localhost shell]# echo $y\n333\n[root@localhost shell]# bash  # \u5b59\u5b50\n[root@localhost shell]# echo $y\n333\n\nps\uff1a\u901a\u8fc7exit\u547d\u4ee4\u53ef\u4ee5\u4e00\u5c42\u4e00\u5c42\u5730\u9000\u51fa Shell\u3002<\/code><\/pre>\n<p>ps\uff1a\u547d\u4ee4set \u548c env<\/p>\n<pre><code class=\"language-python\">set:\u663e\u793a\u6240\u6709\u53d8\u91cf\nenv:\u73af\u5883\u53d8\u91cf<\/code><\/pre>\n<p>\u6ce8\u610f<\/p>\n<ul>\n<li>\n<p>1\u3001\u73af\u5883\u53d8\u91cf\u53ea\u80fd\u5411\u4e0b\u4f20\u9012\u800c\u4e0d\u80fd\u5411\u4e0a\u4f20\u9012\uff0c\u5373\u201c\u4f20\u5b50\u4e0d\u4f20\u7236\u201d\u3002<\/p>\n<\/li>\n<li>\n<p>2\u3001\u4e24\u4e2a\u6ca1\u6709\u7236\u5b50\u5173\u7cfb\u7684 Shell \u8fdb\u7a0b\u662f\u4e0d\u80fd\u4f20\u9012\u73af\u5883\u53d8\u91cf\u7684<\/p>\n<pre><code class=\"language-python\">\u6211\u4eec\u4e00\u76f4\u5f3a\u8c03\u7684\u662f\u73af\u5883\u53d8\u91cf\u5728 Shell \u5b50\u8fdb\u7a0b\u4e2d\u6709\u6548\uff0c\u5e76\u6ca1\u6709\u8bf4\u5b83\u5728\u6240\u6709\u7684 Shell \u8fdb\u7a0b\u4e2d\u90fd\u6709\u6548\uff1b\u5982\u679c\u4f60\u901a\u8fc7\u7ec8\u7aef\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684 Shell \u7a97\u53e3\uff0c\u90a3\u5b83\u5c31\u4e0d\u662f\u5f53\u524d Shell \u7684\u5b50\u8fdb\u7a0b\uff0c\u73af\u5883\u53d8\u91cf\u5bf9\u8fd9\u4e2a\u65b0\u7684 Shell \u8fdb\u7a0b\u4ecd\u7136\u662f\u65e0\u6548\u7684\u3002<\/code><\/pre>\n<\/li>\n<li>\n<p>3\u3001\u73af\u5883\u53d8\u91cf\u4e5f\u662f\u4e34\u65f6\u7684<\/p>\n<pre><code class=\"language-python\">[root@localhost ~]# ps aux |grep bash$ |grep -v grep\nroot     123436  0.0  0.1 116356  2960 pts\/0    Ss   21:52   0:00 -bash\nroot     123492  0.0  0.1 116472  2932 pts\/0    S    21:54   0:00 bash\nroot     123520  0.0  0.1 116440  2988 pts\/0    S    21:54   0:00 bash\n\n\u6ce8\u610f\uff1a\n# -\u5f00\u5934\u7684bash\u4ee3\u8868\u662f\u5728\u767b\u5f55\u7ec8\u7aef\u767b\u5f55\u7684\u9876\u7ea7shell\u8fdb\u7a0b\n# \u975e-\u5f00\u5934\u7684bash\u4ee3\u8868\u7684\u662f\u5b50shell\u8fdb\u7a0b\n\u4e00\u65e6\u9000\u51fa\u4e86\u5728\u7ec8\u7aef\u767b\u5f55\u7684\u9876\u7ea7shell\uff0c\u90a3\u4e48\u8be5\u7ec8\u7aef\u4e0b\u5f00\u542f\u7684\u6240\u6709\u5b50shell\u90fd\u4f1a\u88ab\u56de\u6536\uff0cexport\u8bbe\u7f6e\u7684\u73af\u5883\u53d8\u91cf\u968f\u5373\u6d88\u5931<\/code><\/pre>\n<p>\u6240\u4ee5\u8bf4\u73af\u5883\u53d8\u91cf\u4e5f\u662f\u4e34\u65f6\u7684\uff0c\u5982\u679c\u60f3\u8bbe\u7f6e\u6210\u6c38\u4e45\u7684\uff0c\u9700\u8981\u5c06\u53d8\u91cf\u5199\u5165shell\u914d\u7f6e\u6587\u4ef6\u4e2d\u624d\u53ef\u4ee5\uff0cShell \u8fdb\u7a0b\u6bcf\u6b21\u542f\u52a8\u65f6\u90fd\u4f1a\u6267\u884c\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u4ee3\u7801\u505a\u4e00\u4e9b\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u5982\u679c\u5c06\u53d8\u91cf\u653e\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u90a3\u4e48\u6bcf\u6b21\u542f\u52a8\u8fdb\u7a0b\u90fd\u4f1a\u5b9a\u4e49\u8fd9\u4e2a\u53d8\u91cf\u3002\u8bf7\u770b\u4e0b\u4e00\u5c0f\u8282<\/p>\n<\/li>\n<\/ul>\n<h2>\u516d \u767b\u5f55shell\u4e0e\u975e\u767b\u5f55shell<\/h2>\n<p><\/p><div id=\"rml_readmorelogin_placeholder\" style=\"position:relative;\"><div id=\"rml_fade_content\" style=\"position: absolute;\r\ntop:-10em;\r\nwidth:100%;\r\nheight:10em;\r\nbackground: -webkit-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -moz-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -o-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\r\nbackground-image: -ms-linear-gradient(rgba(255, 255, 255, 0) 0%,#ffffff 100%);\"><\/div><div class=\"wpf-controller aru_rml_from_in_post\" style=\"background-color:#eeeeee;border:5px solid #cce6ff;\" id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><h2 id=\"Header\">\u67e5\u770b\u66f4\u591a<\/h2><div id=\"Message\"><p>\u8054\u7cfb\u7ba1\u7406\u5458\u5fae\u4fe1tutu19192010\uff0c\u6ce8\u518c\u8d26\u53f7<\/p>\n<\/div><div id=\"StatusBarHeader\"><\/div><form id=\"ARU_ReadMoreLogin_ReadMoreLoginController\"><input name=\"post_id\" value=\"403\" type=\"hidden\"\/><input name=\"_init_callback\" value=\"InitLogin\" type=\"hidden\"\/><input name=\"post_id\" value=\"403\" type=\"hidden\"\/><input name=\"rt_ype\" value=\"1\" type=\"hidden\"\/><input name=\"nonce\" value=\"089930ca25\" type=\"hidden\"\/><input name=\"_wpnonce\" value=\"ee095c8387\" type=\"hidden\"\/><input name=\"_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_proxy_controller\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginController\" type=\"hidden\"\/><input name=\"_view\" value=\"ARU_ReadMoreLogin\\ReadMoreLoginView\" type=\"hidden\"\/><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\" width=\"400px\"><table class=\"wpf-table-placeholder\"><tbody class=\"wpf-table-placeholder\"><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Username:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"username\" value=\"\" type=\"text\"\/><\/td><\/tr><tr class=\"wpf-table-placeholder\"><th class=\"wpf-table-placeholder-input\"><label class=\"wpf-label\">Password:<\/label><\/th><\/tr><tr class=\"wpf-table-placeholder\"><td class=\"wpf-table-placeholder-input\"><input class=\"regular-text text_input\" name=\"password\" value=\"\" type=\"password\"\/><\/td><\/tr><\/tbody><\/table><p class=\"wpf-table-placeholder submit\"><button class=\"wp_plugin_framework_ajax_button\" type=\"button\" style=\"background-color:#4D90FE;;color:#ffffff;;border:1px solid #3079ed;\" name=\"_event\" value=\"ButtonLogin\">Log in<\/button><\/p><\/td><td class=\"wpf-table-placeholder-input\"><\/td><\/tr><\/tbody><\/table><\/form><div id=\"ButtonStartRegister\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=register\">Register<\/a><\/div><div id=\"Link1\"><a href=\"https:\/\/egonlin.com\/wp-login.php?action=lostpassword\">Forgotten username or password?<\/a><\/div><div id=\"StatusBarFooter\"><\/div><\/div><\/div><div id=aru_remaining_content><\/div>","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570 \u4e00 \u51fd\u6570\u4ecb\u7ecd \u4ec0\u4e48\u662f\u51fd\u6570\uff1f\uff1f\uff1f \u51fd\u6570\u5c31\u662f\u7528\u6765\u76db\u653e\u4e00\u7ec4\u4ee3\u7801\u7684\u5bb9\u5668\uff0c\u51fd\u6570\u5185\u7684\u4e00\u7ec4\u4ee3\u7801\u5b8c\u6210\u4e00\u4e2a\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u79f0\u4e4b [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[66],"tags":[21,19,75,22,20],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/403"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=403"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/403\/revisions"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}