{"id":762,"date":"2022-03-13T20:34:09","date_gmt":"2022-03-13T12:34:09","guid":{"rendered":"https:\/\/www.buyao007.icu\/?p=762"},"modified":"2022-03-13T20:34:10","modified_gmt":"2022-03-13T12:34:10","slug":"4-shell%e7%bc%96%e7%a8%8b%e4%b9%8b%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.buyao007.icu\/?p=762","title":{"rendered":"4. shell\u7f16\u7a0b\u4e4b\u51fd\u6570"},"content":{"rendered":"\n<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6\u7fa4\u67b6\u6784\u4e0e\u81ea\u52a8\u5316\u8fd0\u7ef4\uff0c\u66fe\u8d1f\u8d23\u56fd\u5185\u67d0\u5927\u578b\u535a\u5ba2\u7f51\u7ad9\u8fd0\u7ef4\u5de5\u4f5c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">1.\u51fd\u6570\u57fa\u672c\u6982\u8ff0<\/h2>\n\n\n\n<p><strong><em>1.\u4ec0\u4e48\u662fshell\u51fd\u6570<\/em><\/strong><\/p>\n\n\n\n<p>\u51fd\u6570\u5176\u5b9e\u5c31\u662f\u4e00\u5806\u547d\u4ee4\u7684\u5408\u96c6\uff0c\u7528\u6765\u5b8c\u6210\u7279\u5b9a\u529f\u80fd\u7684\u4ee3\u7801\u5757\u3002<\/p>\n\n\n\n<p><strong><em>2.\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u51fd\u6570<\/em><\/strong><\/p>\n\n\n\n<p>\u6bd4\u5982\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u5224\u65ad\u529f\u80fd\uff0c\u5b8c\u5168\u53ef\u4ee5\u5c06\u5176\u5c01\u88c5\u4e3a\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u6837\u5728\u5199\u7a0b\u5e8f\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u4e0d\u5fc5\u91cd\u590d\u7f16\u5199\u3002\u8fd9\u6837\u80fd\u51cf\u5c11\u4ee3\u7801\u5197\u4f59\uff0c\u53ef\u8bfb\u6027\u66f4\u5f3a\u3002\u51fd\u6570\u548c\u53d8\u91cf\u7c7b\u4f3c\uff0c\u5fc5\u987b\u5148\u5b9a\u4e49\u624d\u53ef\u4ee5\u8c03\u7528\uff0c\u5982\u679c\u5b9a\u4e49\u4e0d\u8c03\u7528\u5219\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">2.\u51fd\u6570\u57fa\u7840\u8bed\u6cd5<\/h2>\n\n\n\n<p><strong><em>1.\u5b9a\u4e49shell\u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u4e09\u79cd\u65b9\u5f0f\u8fdb\u884c\u5b9a\u4e49\u3002<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u65b9\u5f0f\u4e00\r\nfun1(){\r\n     echo \"\u7b2c\u4e00\u79cd\u5b9a\u4e49\u51fd\u6570\u7684\u65b9\u6cd5\"\r\n}\r\n#\u65b9\u5f0f\u4e8cfun2\u4e0e{\u4e4b\u95f4\u5fc5\u987b\u6709\u7a7a\u683c\r\nfunction fun2 {\r\n     echo \"\u7b2c\u4e8c\u79cd\u5b9a\u4e49\u51fd\u6570\u7684\u65b9\u6cd5\"\r\n}\r\n#\u65b9\u5f0f\u4e09\r\nfunction fun3(){\r\n     echo \"\u7b2c\u4e09\u79cd\u5b9a\u4e49\u51fd\u6570\u7684\u65b9\u6cd5\"\r\n}\r<\/code><\/pre>\n\n\n\n<p><strong><em>2.\u5982\u4f55\u8c03\u7528\u51fd\u6570\uff0c\u76f4\u63a5\u4f7f\u7528\u51fd\u6570\u540d\u8c03\u7528\u3002\uff08\u53ef\u4ee5\u7406\u89e3\u4e3a\uff1ashell\u7684\u4e00\u6761\u547d\u4ee4\uff09<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@fsf ~]# fun() { echo \"\u4f60\u597d\u4e16\u754c\";}\r\n&#91;root@fsf ~]# fun\r\n\u4f60\u597d\u4e16\u754c\r<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">3.\u51fd\u6570\u53c2\u6570\u4f20\u9012<\/h2>\n\n\n\n<p>\u5728\u51fd\u6570\u5185\u90e8\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570$1,$2\u2026\u2026,\u8c03\u7528\u51fd\u6570function_name $1 $2\u2026\u2026<\/p>\n\n\n\n<p><strong><em>1.\u51fd\u6570\u4e2d\u4f20\u9012\u53c2\u6570<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@fsf ~]# fun2() { echo \"hello,$1\" ;}\r\n&#91;root@fsf ~]# fun2 linux\r\nhello,linux\r<\/code><\/pre>\n\n\n\n<p><strong><em>2.\u51fd\u6570\u4e2d\u63a5\u6536\u591a\u4e2a\u53c2\u6570<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@fsf ~]# fun3() { echo \"hello,$1,$2\" ;}\r\n&#91;root@fsf ~]# fun3 linux mysql\r\nhello,linux,mysql\r<\/code><\/pre>\n\n\n\n<p><strong><em>3.\u51fd\u6570\u4e2d\u4f20\u9012\u591a\u4e2a\u53c2\u6570$*,\u63a5\u6536\u6240\u6709\u7684\u53c2\u6570\u4f20\u53c2<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@fsf ~]# fun4() { echo \"hello,$*\" ;}\r\n&#91;root@fsf ~]# fun4 linux mysql redis https\r\nhello,linux mysql redis https\r<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">4.\u51fd\u6570\u72b6\u6001\u8fd4\u56de<\/h2>\n\n\n\n<p>Shell\u7684\u51fd\u6570\u8fd4\u56de\u503c\uff0c\u4e5f\u7b97\u662f\u9000\u51fa\u7684\u72b6\u6001\u3002\u5728shell\u4e2d\u53ea\u6709echo\u3001return\u4e24\u79cd\u65b9\u5f0f\u30021.return\u8fd4\u56de\u503c\uff0c\u53ea\u80fd\u8fd4\u56de1-255\u7684\u6574\u6570\uff0c\u51fd\u6570\u4f7f\u7528return\u8fd4\u56de\u503c\uff0c\u901a\u5e38\u53ea\u662f\u7528\u6765\u4f9b\u5176\u4ed6\u5730\u65b9\u8c03\u7528\u83b7\u53d6\u72b6\u6001\uff0c\u56e0\u6b64\u901a\u5e38\u8fd4\u56de0\u62161\uff1b0\u8868\u793a\u6210\u529f\uff0c1\u8868\u793a\u5931\u8d25\u30022.echo\u8fd4\u56de\u503c\uff1b\u4f7f\u7528echo\u53ef\u4ee5\u8fd4\u56de\u4efb\u4f55\u5b57\u7b26\u4e32\u7ed3\u679c\uff0c\u901a\u5e38\u7528\u6237\u8fd4\u56de\u6570\u636e\uff0c\u6bd4\u5982\u4e00\u4e2a\u5b57\u7b26\u4e32\u503c\u6216\u8005\u5217\u8868\u503c\u3002<\/p>\n\n\n\n<p><strong><em>1.shell\u51fd\u6570echo\u8fd4\u56de\u5b57\u7b26\u4e32<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@fsf ~]# cat fun_echo.sh\r\n#!\/bin\/bash\r\nget_users() {\r\n   users=`cat \/etc\/passwd|cut -d: -f1`\r\n   echo $users\r\n}\r\nuser_list=`get_users`\r\nindex=1\r\nfor u in $user_list\r\ndo\r\n  echo \"The $index user is: $u \"\r\n  let index++\r\ndone\r<\/code><\/pre>\n\n\n\n<p><strong><em>2.shell\u51fd\u6570\u7684return\u8fd4\u56de\u503c\u793a\u4f8b<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@fsf ~]# cat fun_return_1.sh\r\n#!\/bin\/bash\r\nfile=\/etc\/passwd\r\nt_file() {\r\nif &#91; -f $file ];then\r\n  return 123\r\n else\r\n return 1\r\n  \r\n}\r\nt_file\r\necho $?\r\n&#91;root@fsf ~]# sh fun_return_1.sh\r\n123\r<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">5.\u7cfb\u7edf\u5185\u7f6e\u51fd\u6570\u5e93<\/h2>\n\n\n\n<p>\/etc\/init.d\/functions<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">6.\u51fd\u6570\u53d8\u91cf\u5c40\u90e8\u751f\u6548<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>fun(){\r\nlocal num=10\t\t\t\t# \u52a0\u4e0alocal\u53ea\u5728\u5f53\u524d\u7684fun\u51fd\u6570\u4e2d\u751f\u6548\r\n     for i in `seq 10`\r\n     do\r\n           total=$&#91;$num+$i]\r\n     done\r\n     echo \u8ba1\u7b97\u7ed3\u679c\u4e3a $total\r\n}\r<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/762"}],"collection":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=762"}],"version-history":[{"count":1,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/762\/revisions"}],"predecessor-version":[{"id":763,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/762\/revisions\/763"}],"wp:attachment":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}