{"id":1506,"date":"2020-06-05T19:19:53","date_gmt":"2020-06-05T11:19:53","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1506"},"modified":"2020-06-05T19:19:53","modified_gmt":"2020-06-05T11:19:53","slug":"shell%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1506","title":{"rendered":"Shell\u7f16\u7a0b\u5165\u95e8"},"content":{"rendered":"<div>\n<h2>Shell \u662f\u4ec0\u4e48\uff1f<\/h2>\n<p>Shell \u662f\u4e00\u4e2a\u547d\u4ee4\u89e3\u91ca\u6743\uff0c\u5b83\u4e3a\u7528\u6237\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5411 Linux \u5185\u6838\u53d1\u9001\u8bf7\u6c42\u4ee5\u4fbf\u8fd0\u884c\u7a0b\u5e8f\u754c\u9762\u7cfb\u7edf\u7ea7\u7a0b\u5e8f\uff0c\u7528\u6237\u53ef\u4ee5\u7528 Shell \u6765\u542f\u52a8\u3001\u6302\u8d77\u3001\u505c\u6b62\u751a\u81f3\u662f\u7f16\u5199\u4e00\u4e9b\u7a0b\u5e8f\u3002<\/p>\n<div class=\\\"image-package\\\">\n<div class=\\\"image-container\\\">\n<div class=\\\"image-view\\\" data-width=\\\"262\\\" data-height=\\\"223\\\"><img class=\\\"\\\" src=\\\"\/\/upload-images.jianshu.io\/upload_images\/15942529-77674b0f89170251.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/262\/format\/webp\\\" data-original-src=\\\"\/\/upload-images.jianshu.io\/upload_images\/15942529-77674b0f89170251.png\\\" data-original-width=\\\"262\\\" data-original-height=\\\"223\\\" data-original-format=\\\"image\/png\\\" data-original-filesize=\\\"4880\\\" data-image-index=\\\"0\\\" \/><\/div>\n<\/div>\n<div class=\\\"image-caption\\\"><\/div>\n<\/div>\n<h2>Shell \u7f16\u7a0b\u5feb\u901f\u5165\u95e8<\/h2>\n<p>\u8fdb\u5165 Linux \u7ec8\u7aef\uff0c\u7f16\u5199\u4e00\u4e2a Shell \u811a\u672c hello.sh \uff1a<\/p>\n<div>\n<pre>#!\/bin\/bash\necho \\'hello world!\\'\n<\/pre>\n<\/div>\n<p>\u8fd0\u884c\uff1a<\/p>\n<div>\n<pre>\/\/ \u65b9\u6cd51\n# sh hello.sh\n\/\/ \u65b9\u6cd52\n# chmod +x hello.sh\n# .\/hello.sh\n<\/pre>\n<\/div>\n<p>\u7ec8\u7aef\u6253\u5370\u51fa <code>hello world!<\/code> \u3002<br \/>\n<strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li><code>#!<\/code> \u544a\u8bc9\u7cfb\u7edf\u8fd9\u4e2a\u811a\u672c\u9700\u8981\u4ec0\u4e48\u89e3\u91ca\u5668\u6765\u6267\u884c\u3002<\/li>\n<li>\u6587\u4ef6\u6269\u5c55\u540d <code>.sh<\/code> \u4e0d\u662f\u5f3a\u5236\u8981\u6c42\u7684\u3002<\/li>\n<li>\u65b9\u6cd51 \u76f4\u63a5\u8fd0\u884c\u89e3\u91ca\u5668\uff0c<code>hello.sh<\/code> \u4f5c\u4e3a Shell \u89e3\u91ca\u5668\u7684\u53c2\u6570\u3002\u6b64\u65f6 Shell \u811a\u672c\u5c31\u4e0d\u9700\u8981\u6307\u5b9a\u89e3\u91ca\u5668\u4fe1\u606f\uff0c\u7b2c\u4e00\u884c\u53ef\u4ee5\u53bb\u6389\u3002<\/li>\n<li>\u65b9\u6cd52 hello.sh \u4f5c\u4e3a\u53ef\u6267\u884c\u7a0b\u5e8f\u8fd0\u884c\uff0cShell \u811a\u672c\u7b2c\u4e00\u884c\u4e00\u5b9a\u8981\u6307\u5b9a\u89e3\u91ca\u5668\u3002<\/li>\n<\/ul>\n<h2>Shell \u53d8\u91cf<\/h2>\n<h3>\u5b9a\u4e49<\/h3>\n<p>Shell \u53d8\u91cf\u5206\u4e3a<strong>\u7cfb\u7edf\u53d8\u91cf<\/strong>\u548c<strong>\u81ea\u5b9a\u4e49\u53d8\u91cf<\/strong>\u3002\u7cfb\u7edf\u53d8\u91cf\u6709$HOME\u3001$PWD\u3001$USER\u7b49\uff0c\u663e\u793a\u5f53\u524d Shell \u4e2d\u6240\u6709\u53d8\u91cf\uff1a<code>set<\/code> \u3002<br \/>\n\u53d8\u91cf\u540d\u53ef\u4ee5\u7531\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u4e0b\u5212\u7ebf\u7ec4\u6210\uff0c\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3002<\/p>\n<h3>\u57fa\u672c\u8bed\u6cd5<\/h3>\n<ul>\n<li><strong>\u5b9a\u4e49\u53d8\u91cf\uff1a<\/strong>\u53d8\u91cf\u540d=\u53d8\u91cf\u503c\uff0c\u7b49\u53f7\u4e24\u4fa7\u4e0d\u80fd\u6709\u7a7a\u683c\uff0c\u53d8\u91cf\u540d\u4e00\u822c\u4e60\u60ef\u7528\u5927\u5199\u3002<\/li>\n<li><strong>\u5220\u9664\u53d8\u91cf\uff1a<\/strong>unset \u53d8\u91cf\u540d \u3002<\/li>\n<li><strong>\u58f0\u660e\u9759\u6001\u53d8\u91cf\uff1a<\/strong>readonly \u53d8\u91cf\u540d\uff0c\u9759\u6001\u53d8\u91cf\u4e0d\u80fdunset\u3002<\/li>\n<li><strong>\u4f7f\u7528\u53d8\u91cf\uff1a<\/strong>$\u53d8\u91cf\u540d<\/li>\n<\/ul>\n<h3>\u5c06\u547d\u4ee4\u8fd4\u56de\u503c\u8d4b\u7ed9\u53d8\u91cf\uff08\u91cd\u70b9\uff09<\/h3>\n<ul>\n<li>A=`ls` \u53cd\u5f15\u53f7,\u6267\u884c\u91cc\u9762\u7684\u547d\u4ee4<\/li>\n<li>A=$(ls) \u7b49\u4ef7\u4e8e\u53cd\u5f15\u53f7<\/li>\n<\/ul>\n<h2>Shell \u73af\u5883\u53d8\u91cf<\/h2>\n<h3>\u5b9a\u4e49<\/h3>\n<div class=\\\"image-package\\\">\n<div class=\\\"image-container\\\">\n<div class=\\\"image-container-fill\\\"><\/div>\n<div class=\\\"image-view\\\" data-width=\\\"601\\\" data-height=\\\"458\\\"><img class=\\\"\\\" src=\\\"\/\/upload-images.jianshu.io\/upload_images\/15942529-c8521cd0c1a5b19e.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/601\/format\/webp\\\" data-original-src=\\\"\/\/upload-images.jianshu.io\/upload_images\/15942529-c8521cd0c1a5b19e.png\\\" data-original-width=\\\"601\\\" data-original-height=\\\"458\\\" data-original-format=\\\"image\/png\\\" data-original-filesize=\\\"24757\\\" data-image-index=\\\"1\\\" \/><\/div>\n<\/div>\n<div class=\\\"image-caption\\\"><\/div>\n<\/div>\n<h3>\u57fa\u672c\u8bed\u6cd5<\/h3>\n<ol>\n<li>export \u53d8\u91cf\u540d=\u53d8\u91cf\u503c\uff0c\u5c06 Shell \u53d8\u91cf\u8f93\u51fa\u4e3a\u73af\u5883\u53d8\u91cf\u3002<\/li>\n<li>source \u914d\u7f6e\u6587\u4ef6\u8def\u5f84\uff0c\u8ba9\u4fee\u6539\u540e\u7684\u914d\u7f6e\u4fe1\u606f\u7acb\u5373\u751f\u6548\u3002<\/li>\n<li>echo $\u53d8\u91cf\u540d\uff0c\u68c0\u67e5\u73af\u5883\u53d8\u91cf\u662f\u5426\u751f\u6548<\/li>\n<\/ol>\n<h2>\u4f4d\u7f6e\u53c2\u6570\u53d8\u91cf<\/h2>\n<h3>\u57fa\u672c\u8bed\u6cd5<\/h3>\n<ul>\n<li><img class=\\\"math-inline\\\" src=\\\"https:\/\/math.jianshu.com\/math?formula=n%20%EF%BC%9A\\\" alt=\\\"n \uff1a\\\" \/>1 \u4ee3\u8868\u547d\u4ee4\u672c\u8eab\u3001<img class=\\\"math-inline\\\" src=\\\"https:\/\/math.jianshu.com\/math?formula=1-\\\" alt=\\\"1-\\\" \/>9 \u4ee3\u8868\u7b2c1\u52309\u4e2a\u53c2\u6570\uff0c10\u4ee5\u4e0a\u53c2\u6570\u7528\u82b1\u62ec\u53f7\uff0c\u5982${10}\u3002<\/li>\n<li>$* \uff1a\u547d\u4ee4\u884c\u4e2d\u6240\u6709\u53c2\u6570\uff0c\u4e14\u628a\u6240\u6709\u53c2\u6570\u770b\u6210\u4e00\u4e2a\u6574\u4f53\u3002<\/li>\n<li>$@ \uff1a\u547d\u4ee4\u884c\u4e2d\u6240\u6709\u53c2\u6570\uff0c\u4e14\u628a\u6bcf\u4e2a\u53c2\u6570\u533a\u5206\u5bf9\u5f85\u3002<\/li>\n<li>$# \uff1a\u6240\u6709\u53c2\u6570\u4e2a\u6570\u3002<\/li>\n<\/ul>\n<h3>\u5b9e\u4f8b\uff1a<\/h3>\n<p>\u7f16\u5199 Shell \u811a\u672c positionPara.sh \uff0c\u8f93\u51fa\u547d\u4ee4\u884c\u8f93\u5165\u7684\u5404\u4e2a\u53c2\u6570\u4fe1\u606f\u3002<\/p>\n<div class=\\\"_2Uzcx_\\\">\n<pre>#!\/bin\/bash\n# \u8f93\u51fa\u5404\u4e2a\u53c2\u6570\necho $0 $1 $2\necho $*\necho $@\necho \u53c2\u6570\u4e2a\u6570=$#\n<\/pre>\n<\/div>\n<p>\u8fd0\u884c\uff1a<\/p>\n<div>\n<pre># chmod +x positionPara.sh\n# .\/positionPara.sh 10 20\n<\/pre>\n<\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<div>\n<pre>positionPara.sh 10 20\n10 20\n10 20\n\u53c2\u6570\u4e2a\u6570=2\n<\/pre>\n<\/div>\n<h2>\u9884\u5b9a\u4e49\u53d8\u91cf<\/h2>\n<h3>\u5b9a\u4e49<\/h3>\n<p>\u5728\u8d4b\u503c\u5b9a\u4e49\u4e4b\u524d\uff0c\u4e8b\u5148\u5728 Shell \u811a\u672c\u4e2d\u76f4\u63a5\u5f15\u7528\u7684\u53d8\u91cf\u3002<\/p>\n<h3>\u57fa\u672c\u8bed\u6cd5<\/h3>\n<ul>\n<li>$$ \uff1a\u5f53\u524d\u8fdb\u7a0b\u7684 PID \u8fdb\u7a0b\u53f7\u3002<\/li>\n<li>$! \uff1a\u540e\u53f0\u8fd0\u884c\u7684\u6700\u540e\u4e00\u4e2a\u8fdb\u7a0b\u7684 PID \u8fdb\u7a0b\u53f7\u3002<\/li>\n<li>$? \uff1a\u6700\u540e\u4e00\u6b21\u6267\u884c\u7684\u547d\u4ee4\u7684\u8fd4\u56de\u72b6\u6001\uff0c0\u4e3a\u6267\u884c\u6b63\u786e\uff0c\u975e0\u6267\u884c\u5931\u8d25\u3002<\/li>\n<\/ul>\n<h3>\u5b9e\u4f8b\uff1a<\/h3>\n<p>\u7f16\u5199 Shell \u811a\u672c prePara.sh \uff0c\u8f93\u51fa\u547d\u4ee4\u884c\u8f93\u5165\u7684\u5404\u4e2a\u53c2\u6570\u4fe1\u606f\u3002<\/p>\n<div>\n<pre>#!\/bin\/bash\necho \u5f53\u524d\u7684\u8fdb\u7a0b\u53f7=$$\n# &amp;\uff1a\u4ee5\u540e\u53f0\u7684\u65b9\u5f0f\u8fd0\u884c\u7a0b\u5e8f\n.\/hello.sh &amp;\necho \u6700\u540e\u4e00\u4e2a\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u53f7=$!\necho \u6700\u540e\u6267\u884c\u7684\u547d\u4ee4\u7ed3\u679c=$?\n<\/pre>\n<\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<div>\n<pre>\u5f53\u524d\u7684\u8fdb\u7a0b\u53f7=41752\n\u6700\u540e\u4e00\u4e2a\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u53f7=41753\n\u6700\u540e\u6267\u884c\u7684\u547d\u4ee4\u7ed3\u679c=0\n# hello world!\n<\/pre>\n<\/div>\n<h2>\u8fd0\u7b97\u7b26<\/h2>\n<h3>\u57fa\u672c\u8bed\u6cd5<\/h3>\n<ul>\n<li><img class=\\\"math-inline\\\" src=\\\"https:\/\/math.jianshu.com\/math?formula=((%E8%BF%90%E7%AE%97%E5%BC%8F))%20%E6%88%96\\\" alt=\\\"((\u8fd0\u7b97\u5f0f)) \u6216\\\" \/>[\u8fd0\u7b97\u5f0f]<\/li>\n<li>expr m + n \u6ce8\u610f expr \u8fd0\u7b97\u7b26\u95f4\u8981\u6709\u7a7a\u683c<\/li>\n<li>expr m &#8211; n<\/li>\n<li>expr \\\\*\uff0c\/\uff0c% \u5206\u522b\u4ee3\u8868\u4e58\uff0c\u9664\uff0c\u53d6\u4f59<\/li>\n<\/ul>\n<h3>\u5b9e\u4f8b<\/h3>\n<div>\n<pre># \u7b2c1\u79cd\u65b9\u5f0f $(())\necho $(((2+3)*4))\n# \u7b2c2\u79cd\u65b9\u5f0f $[]\uff0c\u63a8\u8350\necho $[(2+3)*4]\n# \u4f7f\u7528 expr\nTEMP=`expr 2 + 3`\necho `expr $TEMP \\\\* 4`\n<\/pre>\n<\/div>\n<h2>\u6761\u4ef6\u5224\u65ad<\/h2>\n<h3>\u57fa\u672c\u8bed\u6cd5<\/h3>\n<p>[ condition ] \u6ce8\u610fcondition\u524d\u540e\u8981\u6709\u7a7a\u683c\u3002\u975e\u7a7a\u8fd4\u56de0\uff0c0\u4e3a true\uff0c\u5426\u5219\u4e3afalse \u3002<\/p>\n<h3>\u5b9e\u4f8b<\/h3>\n<div>\n<pre>#!\/bin\/bash\nif [ \\'test01\\' = \\'test\\' ]\nthen\n    echo \\'\u7b49\u4e8e\\'\nfi\n# 20\u662f\u5426\u5927\u4e8e10\nif [ 20 -gt 10]\nthen\n    echo \\'\u5927\u4e8e\\'\nfi\n# \u662f\u5426\u5b58\u5728\u6587\u4ef6\/root\/shell\/a.txt\nif [ -e \/root\/shell\/a.txt ]\nthen\n    echo \\'\u5b58\u5728\\'\nfi\nif [ \\'test02\\' = \\'test02\\' ] &amp;&amp; echo \\'hello\\' || echo \\'world\\'\nthen\n    echo \\'\u6761\u4ef6\u6ee1\u8db3\uff0c\u6267\u884c\u540e\u9762\u7684\u8bed\u53e5\\'\nfi\n<\/pre>\n<\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<div>\n<pre>\u5927\u4e8e\nhello\n\u6761\u4ef6\u6ee1\u8db3\uff0c\u6267\u884c\u540e\u9762\u7684\u8bed\u53e5\n<\/pre>\n<\/div>\n<h2>\u6d41\u7a0b\u63a7\u5236<\/h2>\n<h3>if \u5224\u65ad<\/h3>\n<h4>\u57fa\u672c\u8bed\u6cd5<\/h4>\n<div>\n<pre>if [ \u6761\u4ef6\u5224\u65ad\u5f0f ];then\n    \u7a0b\u5e8f\nfi\n# \u6216\u8005\uff08\u63a8\u8350\uff09\nif [ \u6761\u4ef6\u5224\u65ad\u5f0f ]\nthen\n    \u7a0b\u5e8f\nelif [ \u6761\u4ef6\u5224\u65ad\u5f0f ]\nthen\n    \u7a0b\u5e8f\nfi\n<\/pre>\n<\/div>\n<h4>\u5b9e\u4f8b<\/h4>\n<p>\u7f16\u5199 Shell \u7a0b\u5e8f\uff1a\u5982\u679c\u8f93\u5165\u7684\u53c2\u6570\u5927\u4e8e60\uff0c\u8f93\u51fa\u201c\u53ca\u683c\u201d\uff0c\u5426\u5219\u8f93\u51fa\u201c\u4e0d\u53ca\u683c\u201d\u3002<\/p>\n<div>\n<pre>#!\/bin\/bash\nif [ $1 -ge 60 ]\nthen\n    echo \u53ca\u683c\nelif [ $1 -lt 60 ]\nthen\n    echo \\\"\u4e0d\u53ca\u683c\\\"\nfi\n<\/pre>\n<\/div>\n<h3>case \u5206\u652f<\/h3>\n<h4>\u57fa\u672c\u8bed\u6cd5<\/h4>\n<div>\n<pre>case $\u53d8\u91cf\u540d in\n\\\"\u503c1\\\")\n\u5982\u679c\u53d8\u91cf\u503c\u7b49\u4e8e\u503c1\uff0c\u5219\u6267\u884c\u6b64\u5904\u7a0b\u5e8f1\n;;\n\\\"\u503c2\\\")\n\u5982\u679c\u53d8\u91cf\u503c\u7b49\u4e8e\u503c2\uff0c\u5219\u6267\u884c\u6b64\u5904\u7a0b\u5e8f2\n;;\n...\u7701\u7565\u5176\u5b83\u5206\u652f...\n*)\n\u5982\u679c\u53d8\u91cf\u503c\u4e0d\u7b49\u4e8e\u4ee5\u4e0a\u5217\u51fa\u7684\u503c\uff0c\u5219\u6267\u884c\u6b64\u5904\u7a0b\u5e8f\n;;\nesac\n<\/pre>\n<\/div>\n<h4>\u5b9e\u4f8b<\/h4>\n<p>\u5f53\u547d\u4ee4\u884c\u53c2\u6570\u4e3a1\u65f6\u8f93\u51fa\u201c\u5468\u4e00\u201d\uff0c2\u65f6\u8f93\u51fa\u201c\u5468\u4e8c\u201d\uff0c\u5176\u4ed6\u60c5\u51b5\u8f93\u51fa\u201c\u5176\u5b83\u201d\u3002<\/p>\n<div>\n<pre>case $1 in\n\\\"1\\\")\necho \u5468\u4e00\n;;\n\\\"2\\\")\necho \u5468\u4e8c\n;;\n*)\necho \u5176\u5b83\n;;\nesac\n<\/pre>\n<\/div>\n<h3>for \u5faa\u73af<\/h3>\n<h4>\u57fa\u672c\u8bed\u6cd5<\/h4>\n<div>\n<pre># \u8bed\u6cd51\nfor \u53d8\u91cf\u540d in \u503c1 \u503c2 \u503c3...\ndo\n    \u7a0b\u5e8f\ndone\n# \u8bed\u6cd52\nfor ((\u521d\u59cb\u503c;\u5faa\u73af\u63a7\u5236\u6761\u4ef6;\u53d8\u91cf\u53d8\u5316))\ndo\n    \u7a0b\u5e8f\ndone\n<\/pre>\n<\/div>\n<h4>\u5b9e\u4f8b<\/h4>\n<ol>\n<li>\u6253\u5370\u547d\u4ee4\u884c\u8f93\u5165\u7684\u53c2\u6570\u3002\n<div>\n<pre> #!\/bin\/bash\n # \u4f7f\u7528$*\n for i in \\\"$*\\\"\n do\n     echo \\\"the arg is $i\\\"\n done\n echo \\\"==================\\\"\n # \u4f7f\u7528$@\n for j in \\\"$@\\\"\n do\n     echo \\\"the arg is $j\\\"\n done\n<\/pre>\n<\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\uff08\u56de\u987e\u4e00\u4e0b <code>$*<\/code> \u548c <code>$@<\/code> \u7684\u533a\u522b\uff09\uff1a<\/p>\n<div>\n<pre> the arg is 1 2 3\n ==================\n the arg is 1\n the arg is 2\n the arg is 3\n<\/pre>\n<\/div>\n<\/li>\n<li>\u8f93\u51fa\u4ece1\u52a0\u5230100\u7684\u503c\u3002\n<div>\n<pre> #!\/bin\/bash\n SUM=0\n for ((i=1;i&lt;=100;i++))\n do\n     SUM=$[$SUM+$i]\n done\n echo $SUM\n<\/pre>\n<\/div>\n<\/li>\n<\/ol>\n<h3>while \u5faa\u73af<\/h3>\n<h4>\u57fa\u672c\u8bed\u6cd5<\/h4>\n<div>\n<pre>while [ \u6761\u4ef6\u5224\u65ad\u5f0f ]\ndo\n    \u7a0b\u5e8f\ndone\n<\/pre>\n<\/div>\n<h4>\u5b9e\u4f8b<\/h4>\n<p>\u8f93\u51fa\u4ece1\u52a0\u5230100\u7684\u503c\u3002<\/p>\n<div>\n<pre>#!\/bin\/bash\nSUM=0\ni=0\nwhile [ $i -le $1 ]\ndo\n    SUM=$[$SUM+$i]\n    i=$[$i+1]\ndone\necho $SUM\n<\/pre>\n<\/div>\n<h2>\u8bfb\u53d6\u63a7\u5236\u53f0\u8f93\u5165<\/h2>\n<h3>\u57fa\u672c\u8bed\u6cd5<\/h3>\n<p>read(\u9009\u9879)(\u53c2\u6570)<br \/>\n<strong>\u9009\u9879<\/strong><\/p>\n<ul>\n<li>-p\uff1a\u6307\u5b9a\u8bfb\u53d6\u503c\u65f6\u7684\u63d0\u793a\u7b26<\/li>\n<li>-t\uff1a\u6307\u5b9a\u8bfb\u53d6\u503c\u65f6\u7b49\u5f85\u7684\u65f6\u95f4\uff08\u79d2\uff09\uff0c\u5982\u679c\u6ca1\u6709\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u8f93\u5165\uff0c\u5c31\u4e0d\u518d\u7b49\u5f85\u4e86\u3002<\/li>\n<\/ul>\n<p><strong>\u53c2\u6570<\/strong><\/p>\n<ul>\n<li>\u53d8\u91cf\u540d\uff1a\u8bfb\u53d6\u503c\u7684\u53d8\u91cf\u540d<\/li>\n<\/ul>\n<h3>\u5b9e\u4f8b<\/h3>\n<p>\u8bfb\u53d6\u63a7\u5236\u53f0\u8f93\u5165\u4e00\u4e2anum\u503c\u3002<\/p>\n<div>\n<pre>#!\/bin\/bash\nread -p \\\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570num1=\\\" NUM1\necho \\\"\u4f60\u8f93\u5165num1\u7684\u503c\u662f\uff1a$NUM1\\\"\nread -t 10 -p \\\"\u8bf7\u572810\u79d2\u5185\u8f93\u5165\u4e00\u4e2a\u6570num2=\\\" NUM2\necho \\\"\u4f60\u8f93\u5165num2\u7684\u503c\u662f\uff1a$NUM2\\\"\n<\/pre>\n<\/div>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<div>\n<pre>\u8bf7\u8f93\u5165\u4e00\u4e2a\u6570num1=10\n\u4f60\u8f93\u5165num1\u7684\u503c\u662f\uff1a10\n\u8bf7\u572810\u79d2\u5185\u8f93\u5165\u4e00\u4e2a\u6570num2=20\n\u4f60\u8f93\u5165num2\u7684\u503c\u662f\uff1a20\n<\/pre>\n<\/div>\n<h2>\u51fd\u6570<\/h2>\n<p>\u548c\u5176\u5b83\u7f16\u7a0b\u8bed\u8a00\u4e00\u6837\uff0cShell \u7f16\u7a0b\u6709\u7cfb\u7edf\u51fd\u6570\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u672c\u6587\u53ea\u4e3e\u4e24\u4e2a\u5e38\u7528\u7cfb\u7edf\u51fd\u6570\u3002<\/p>\n<h3>\u7cfb\u7edf\u51fd\u6570<\/h3>\n<ul>\n<li>basename\uff0c\u5220\u6389\u8def\u5f84\u6700\u540e\u4e00\u4e2a \/ \u524d\u7684\u6240\u6709\u90e8\u5206\uff08\u5305\u62ec\/\uff09\uff0c\u5e38\u7528\u4e8e\u83b7\u53d6\u6587\u4ef6\u540d\u3002<br \/>\n<h5>\u57fa\u672c\u8bed\u6cd5<\/h5>\n<ul>\n<li>basename [pathname] [suffx]<\/li>\n<li>basename [string] [suffx]<\/li>\n<li>\u5982\u679c\u6307\u5b9a suffx\uff0c\u4e5f\u4f1a\u5220\u6389pathname\u6216string\u7684\u540e\u7f00\u90e8\u5206\u3002<\/li>\n<\/ul>\n<h5>\u5b9e\u4f8b<\/h5>\n<div>\n<pre>  # basename \/usr\/bin\/sort\n  sort\n  # basename include\/stdio.h\n  stdio.h\n  # basename include\/stdio.h .h\n  stdio\n<\/pre>\n<\/div>\n<\/li>\n<li>dirname\uff0c\u5220\u6389\u8def\u5f84\u6700\u540e\u4e00\u4e2a \/ \u540e\u7684\u6240\u6709\u90e8\u5206\uff08\u5305\u62ec\/\uff09\uff0c\u5e38\u7528\u4e8e\u83b7\u53d6\u6587\u4ef6\u8def\u5f84\u3002<br \/>\n<h5>\u57fa\u672c\u8bed\u6cd5<\/h5>\n<ul>\n<li>dirname pathname<\/li>\n<li>\u5982\u679c\u8def\u5f84\u4e2d\u4e0d\u542b \/ \uff0c\u5219\u8fd4\u56de \\&#8217;.\\&#8217; \uff08\u5f53\u524d\u8def\u5f84\uff09\u3002<\/li>\n<\/ul>\n<h5>\u5b9e\u4f8b<\/h5>\n<div>\n<pre>  # dirname \/usr\/bin\/\n  \/usr\n  # dirname dir1\/str dir2\/str\n  dir1\n  dir2\n  # dirname stdio.h\n  .\n<\/pre>\n<\/div>\n<\/li>\n<\/ul>\n<h3>\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<h4>\u57fa\u672c\u8bed\u6cd5<\/h4>\n<div>\n<pre>[ function ] funname[()]\n{\n    Action;\n    [return int;]\n}\n# \u8c03\u7528\nfunname \u53c2\u65701 \u53c2\u65702...\n<\/pre>\n<\/div>\n<h4>\u5b9e\u4f8b<\/h4>\n<p>\u8ba1\u7b97\u8f93\u5165\u4e24\u4e2a\u53c2\u6570\u7684\u548c\u3002<\/p>\n<div>\n<pre>#!\/bin\/bash\nfunction getSum(){\n    SUM=$[$n1+$n2]\n    echo \\\"sum=$SUM\\\"\n}\nread -p \\\"\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u53c2\u6570n1\uff1a\\\" n1\nread -p \\\"\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u53c2\u6570n2\uff1a\\\" n2\n# \u8c03\u7528 getSum \u51fd\u6570\ngetSum $n1 $n2<\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Shell \u662f\u4ec0\u4e48\uff1f Shell \u662f\u4e00\u4e2a\u547d\u4ee4\u89e3\u91ca\u6743\uff0c\u5b83\u4e3a\u7528\u6237\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5411 Linux \u5185\u6838\u53d1\u9001\u8bf7\u6c42\u4ee5\u4fbf\u8fd0\u884c\u7a0b\u5e8f &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1506\"> <span class=\"screen-reader-text\">Shell\u7f16\u7a0b\u5165\u95e8<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[102,124],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"Shell \u662f\u4ec0\u4e48\uff1f Shell \u662f\u4e00\u4e2a\u547d\u4ee4\u89e3\u91ca\u6743\uff0c\u5b83\u4e3a\u7528\u6237\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5411 Linux \u5185\u6838\u53d1\u9001\u8bf7\u6c42\u4ee5\u4fbf\u8fd0\u884c\u7a0b\u5e8f&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1506"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1506"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1506\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1506"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}