{"id":1183,"date":"2024-07-29T09:09:00","date_gmt":"2024-07-29T01:09:00","guid":{"rendered":"http:\/\/blue.yn.cn\/?p=1183"},"modified":"2024-10-10T16:06:31","modified_gmt":"2024-10-10T08:06:31","slug":"%e5%87%bd%e6%95%b0%e4%b8%8e%e6%95%b0%e7%bb%84-array","status":"publish","type":"post","link":"http:\/\/blue.yn.cn\/?p=1183","title":{"rendered":"\u51fd\u6570\u4e0e\u6570\u7ec4-array"},"content":{"rendered":"<h3>shell\u51fd\u6570<\/h3>\n<h4>\u4ec0\u4e48\u662fshell\u51fd\u6570<\/h4>\n<ul>\n<li>\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<\/li>\n<\/ul>\n<h4>\u4e3a\u4f55\u9700\u8981\u51fd\u6570<\/h4>\n<ul>\n<li>\u6bd4\u5982\uff1a\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\n<ul>\n<li>\u51fd\u6570\u80fd\u51cf\u5c11\u4ee3\u7801\u5197\u4f59\uff0c\u589e\u52a0\u4ee3\u7801\u7684\u53ef\u8bfb\u6027<\/li>\n<li>\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<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>\u51fd\u6570\u57fa\u7840\u8bed\u6cd5<\/h4>\n<ol>\n<li>\u5b9a\u4e49<code>shell<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u4e24\u79cd\u65b9\u5f0f\u8fdb\u884c\u5b9a\u4e49<\/li>\n<\/ol>\n<pre><code class=\"language-bash\"># \u65b9\u5f0f\u4e00\nname()\n{\n    command1\n    command2\n    ...\n    commandn\n}\n\n# \u65b9\u5f0f\u4e8c\nfunction name\n{\n    command1\n    command2\n    ...\n    commandn\n}<\/code><\/pre>\n<ol start=\"2\">\n<li>\u8c03\u7528\u51fd\u6570\uff0c\u76f4\u63a5\u4f7f\u7528\u51fd\u6570\u540d\u8c03\u7528\u3002\uff08\u53ef\u4ee5\u7406\u89e3\u4e3a<code>shell<\/code>\u7684\u4e00\u6761\u547d\u4ee4\uff09<\/li>\n<\/ol>\n<pre><code class=\"language-bash\">fun() { echo &quot;hello world&quot;; }\n\n# \u8c03\u7528\u51fd\u6570\nfun\nhello world<\/code><\/pre>\n<h4>\u51fd\u6570\u53c2\u6570\u4f20\u9012<\/h4>\n<p>\u5728\u51fd\u6570\u5185\u90e8\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570<code>$1\u3001$2...<\/code>\uff0c\u8c03\u7528\u51fd\u6570<code>function_name $1 $2 ...<\/code><\/p>\n<ol>\n<li>\u51fd\u6570\u4e2d\u4f20\u9012\u5355\u4e2a\u53c2\u6570\u793a\u4f8b<\/li>\n<\/ol>\n<pre><code class=\"language-bash\">fun() { echo &quot;hello,$1&quot;; }\n\n# \u8c03\u7528\nfun monday\nhello,monday<\/code><\/pre>\n<ol start=\"2\">\n<li>\u51fd\u6570\u4e2d\u4f20\u9012\u591a\u4e2a\u53c2\u6570\u793a\u4f8b<\/li>\n<\/ol>\n<pre><code class=\"language-bash\">fun() { echo &quot;hello,$1 $2 $3&quot;; }\nfun linux shell python\nhello,linux shell python<\/code><\/pre>\n<ol start=\"3\">\n<li>\u51fd\u6570\u4e2d\u4f20\u9012\u591a\u4e2a\u53c2\u6570\uff0c<code>$*<\/code>\u63a5\u6536\u6240\u6709\u7684\u53c2\u6570\u4f20\u9012<\/li>\n<\/ol>\n<pre><code class=\"language-bash\">fun() { echo &quot;hello,$*&quot;; }\nfun monday tuesday wednesday thursday friday saturday sunday\nhello,monday tuesday wednesday thursday friday saturday sunday<\/code><\/pre>\n<h4>\u51fd\u6570\u5b9e\u73b0\u8ba1\u7b97\u5668\u529f\u80fd\u811a\u672c<\/h4>\n<ul>\n<li>\u9700\u6c42\uff1a\u5199\u4e00\u4e2a\u811a\u672c\uff0c\u8be5\u811a\u672c\u53ef\u4ee5\u5b9e\u73b0\u8ba1\u7b97\u5668\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8fdb\u884c<code>+-*\/<\/code>\u56db\u5468\u8ba1\u7b97\n<ul>\n<li><code>sh cal.sh 40 + 30<\/code><\/li>\n<li><code>sh cal.sh 40 - 30<\/code><\/li>\n<li><code>sh cal.sh 40 * 30<\/code><\/li>\n<li><code>sh cal.sh 40 \/ 30<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">cat cal.sh\n#!\/usr\/bin\/bash\n\ncalsum() {\n        case $2 in\n        +)\n                echo &quot;$[ $1 + $3 ]&quot;\n                ;;\n        -)\n                echo &quot;$[ $1 - $3 ]&quot;\n                ;;\n        x)\n                echo &quot;$[ $1 * $3 ]&quot;\n                ;;\n        \/)\n                echo &quot;$[ $1 \/ $3 ]&quot;\n                ;;\n        esac\n}\n#\u8c03\u7528\u51fd\u6570\u5e76\u8fdb\u884c\u53c2\u6570\u4f20\u9012\ncalsum $1 $2 $3<\/code><\/pre>\n<h4>\u51fd\u6570\u5b9e\u73b0nginx\u542f\u505c\u811a\u672c<\/h4>\n<p>\u9700\u6c42\uff1a\u5199\u4e00\u4e2a\u811a\u672c\uff0c\u5b9e\u73b0<code>nginx<\/code>\u670d\u52a1\u7684\u542f\u52a8\u3001\u505c\u6b62\u3001\u91cd\u542f<\/p>\n<pre><code class=\"language-bash\">cat nginx.sh\n#!\/usr\/bin\/bash\n. \/etc\/init.d\/functions\n\n#\u5b9a\u4e49\u51fd\u6570\uff0c\u51fd\u6570\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u53c2\u6570\nnginx_when() {\n        if [ $? -eq 0 ];then\n                action &quot;nginx $1 is ok!&quot; \/bin\/true\n        else\n                action &quot;nginx $1 is error...&quot; \/bin\/false\n        fi\n}\n\n#\u63a5\u6536\u811a\u672c\u4f20\u5165\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u53c2\u6570\uff0c\u7136\u540e\u8fdb\u884c\u5339\u914d\ncase $1 in\n        start)\n                nginx\n                nginx_when $1\n                ;;\n        stop)\n                nginx -s stop\n                nginx_when $1\n                ;;\n        reload)\n                nginx -s reload\n                nginx_when $1\n                ;;\n        *)\n                echo &quot;USAGE: $0 { start|stop|reload }&quot;\nesac<\/code><\/pre>\n<h4>\u51fd\u6570\u72b6\u6001\u8fd4\u56de\u503c<\/h4>\n<ul>\n<li><code>shell<\/code>\u7684\u51fd\u6570\u8fd4\u56de\u503c\uff0c\u4e5f\u7b97\u662f\u9000\u51fa\u7684\u72b6\u6001\uff0c\u5728<code>shell<\/code>\u4e2d\u53ea\u6709<code>echo\u3001return<\/code>\u4e24\u79cd\u65b9\u5f0f\n<ul>\n<li><code>return<\/code>\u8fd4\u56de\u503c\uff1a\u53ea\u80fd\u8fd4\u56de1-255\u7684\u6574\u6570\uff0c\u51fd\u6570\u4f7f\u7528<code>return<\/code>\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\u4ec5\u8fd4\u56de0\u62161\uff1b0\u8868\u793a\u6210\u529f\uff0c1\u8868\u793a\u5931\u8d25\u3002<\/li>\n<li><code>echo<\/code>\u8fd4\u56de\u503c\uff1a\u4f7f\u7528<code>echo<\/code>\u53ef\u4ee5\u8fd4\u56de\u4efb\u4f55\u5b57\u7b26\u4e32\u7ed3\u679c\uff0c\u901a\u5e38\u7528\u4e8e\u8fd4\u56de\u6570\u636e\uff0c\u6bd4\u5982\u4e00\u4e2a\u5b57\u7b26\u4e32\u503c\u6216\u8005\u5217\u8868\u503c<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5>echo\u6570\u636e\u8fd4\u56de\u793a\u4f8b<\/h5>\n<pre><code class=\"language-bash\">cat echo.sh\n#!\/usr\/bin\/bash\n\nget_users() {\n        users=$(cat \/etc\/passwd | awk -F &#039;:&#039; &#039;{print $1}&#039;)\n        echo $users\n}\n#get_users\n\n#\u53ef\u4ee5\u5bf9\u62ff\u5230\u7684\u51fd\u6570\u7ed3\u679c\u8fdb\u884c\u904d\u5386\nindex=1\nfor u in $(get_users)\ndo\n        echo &quot;the $index user is :$u&quot;\n        let index++\ndone<\/code><\/pre>\n<h3>shell\u6570\u7ec4<\/h3>\n<h4>\u4ec0\u4e48\u662f\u6570\u7ec4<\/h4>\n<ul>\n<li>\u6570\u7ec4\u5176\u5b9e\u4e5f\u662f\u53d8\u91cf\uff0c\u4f20\u7edf\u7684\u53d8\u91cf\u53ea\u80fd\u5b58\u50a8\u4e00\u4e2a\u503c\uff0c\u4f46\u6570\u7ec4\u53ef\u4ee5\u5b58\u50a8\u591a\u4e2a\u503c<\/li>\n<\/ul>\n<h4>\u6570\u7ec4\u7684\u5206\u7c7b<\/h4>\n<ul>\n<li><code>shell<\/code>\u6570\u7ec4\u5206\u4e3a\u666e\u901a\u6570\u7ec4\u548c\u5173\u8054\u6570\u7ec4\n<ul>\n<li>\u666e\u901a\u6570\u7ec4\uff1a\u53ea\u80fd\u4f7f\u7528\u6574\u6570\u4f5c\u4e3a\u6570\u7ec4\u7d22\u5f15<\/li>\n<li>\u5173\u8054\u6570\u7ec4\uff1a\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u4f5c\u4e3a\u6570\u7ec4\u7d22\u5f15<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u666e\u901a\u6570\u7ec4\uff1a<code>books=(linux nginx shell)<\/code><\/p>\n<table>\n<thead>\n<tr>\n<th>linux<\/th>\n<th>nginx<\/th>\n<th>shell<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>1<\/td>\n<td>2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u666e\u901a\u6570\u7ec4\u4e0b\u6807\u53ea\u80fd\u662f\u6574\u6570<\/p>\n<p>\u5173\u8054\u6570\u7ec4\uff1a<code>info=([name]=xiaoming [age]=18 [skill]=linux)<\/code><\/p>\n<table>\n<thead>\n<tr>\n<th>xiaoming<\/th>\n<th>age<\/th>\n<th>linux<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>name<\/td>\n<td>age<\/td>\n<td>skill<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5173\u8054\u6570\u7ec4\u7684\u4e0b\u6807\u53ef\u4ee5\u662f\u5b57\u7b26\u4e32<\/p>\n<h4>\u6570\u7ec4\u57fa\u672c\u4f7f\u7528<\/h4>\n<h5>\u666e\u901a\u6570\u7ec4\u8d4b\u503c<\/h5>\n<ul>\n<li>\n<p>\u666e\u901a\u6570\u7ec4\u7684\u8d4b\u503c\uff08\u6ce8\u610f\uff1a\u666e\u901a\u6570\u7ec4\u4ec5\u80fd\u4f7f\u7528\u6574\u6570\u6765\u4f5c\u4e3a\u7d22\u5f15\uff09<br \/>\n\u65b9\u5f0f\u4e00\uff1a\u9488\u5bf9\u6bcf\u4e2a\u7d22\u5f15\u8fdb\u884c\u8d4b\u503c\uff08\u6570\u7ec4\u540d[\u7d22\u5f15]=\u53d8\u91cf\u503c\uff09<\/p>\n<pre><code class=\"language-bash\">array[0]=pear\narray[1]=apple\narray[2]=orange<\/code><\/pre>\n<p>\u65b9\u5f0f\u4e8c\uff1a\u4e00\u6b21\u8d4b\u591a\u4e2a\u503c\uff08\u6570\u7ec4\u540d=\uff08\u591a\u4e2a\u53d8\u91cf\u503c\uff09\uff09<\/p>\n<pre><code class=\"language-bash\">array3=(tom jack alice [20]=docker)\necho ${array3[@]}\ntom jack alice docker\necho ${!array3[@]}\n0 1 2 20<\/code><\/pre>\n<\/li>\n<\/ul>\n<h5>\u666e\u901a\u6570\u7ec4\u5143\u7d20\u8bbf\u95ee<\/h5>\n<ol>\n<li>\n<p>\u6570\u7ec4\u540d\u52a0\u7d22\u5f15\u5373\u53ef\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20<\/p>\n<pre><code class=\"language-bash\">echo ${array[0]}\npear<\/code><\/pre>\n<\/li>\n<li>\n<p>\u8bbf\u95ee\u6570\u7ec4\u4e2d\u6240\u6709\u7684\u6570\u636e<\/p>\n<pre><code class=\"language-bash\">echo ${array[@]}\npear apple orange<\/code><\/pre>\n<\/li>\n<li>\n<p>\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\uff0c\u5728\u6570\u636e\u904d\u5386\u7684\u65f6\u5019\u6709\u7528<\/p>\n<pre><code class=\"language-bash\">echo ${!array[@]}\n0 1 2<\/code><\/pre>\n<\/li>\n<li>\n<p>\u7edf\u8ba1\u6570\u7ec4\u5143\u7d20\u7684\u4e2a\u6570<\/p>\n<pre><code class=\"language-bash\">echo ${#array[@]}\n3<\/code><\/pre>\n<\/li>\n<\/ol>\n<h5>\u5173\u8054\u6570\u7ec4\u8d4b\u503c<\/h5>\n<ul>\n<li>\u5173\u8054\u6570\u7ec4\u7684\u8d4b\u503c\uff08\u6ce8\u610f\uff1a\u5173\u8054\u6570\u7ec4\u80fd\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\u4f5c\u4e3a\u7d22\u5f15\uff09<\/li>\n<\/ul>\n<ol>\n<li>\n<p>\u5fc5\u987b\u5148\u7533\u660e\u8fd9\u662f\u4e00\u4e2a\u5173\u8054\u6570\u7ec4<br \/>\n<code>declare -A info<\/code><\/p>\n<\/li>\n<li>\n<p>\u65b9\u5f0f\u4e00\uff1a\u5173\u8054\u6570\u7ec4\u7684\u8d4b\u503c\uff0c\u6570\u7ec4\u540d[\u7d22\u5f15]=\u53d8\u91cf\u503c<\/p>\n<pre><code class=\"language-bash\">info[index1]=pear\ninfo[index2]=apple\ninfo[index3]=orange<\/code><\/pre>\n<\/li>\n<li>\n<p>\u65b9\u5f0f\u4e8c\uff1a\u5173\u8054\u6570\u7ec4\u7684\u8d4b\u503c\uff0c\u6570\u7ec4\u540d\uff08[\u7d22\u5f151]=\u53d8\u91cf\u503c1 [\u7d22\u5f152]=\u53d8\u91cf\u503c2\uff09<br \/>\n<code>info2=([index1]=linux [index2]=nginx [index3]=docker [index4]=&#039;bash shell&#039;)<\/code><\/p>\n<\/li>\n<li>\n<p>\u67e5\u770b\u5173\u8054\u6570\u7ec4<\/p>\n<pre><code class=\"language-bash\">declare -A\ndeclare -A info=&#039;([index1]=&quot;pear&quot; [index2]=&quot;apple&quot; [index3]=&quot;orange&quot; )&#039;\ndeclare -A info2=&#039;([index4]=&quot;bash shell&quot; [index1]=&quot;linux&quot; [index2]=&quot;nginx&quot; [index3]=&quot;docker&quot; )&#039;<\/code><\/pre>\n<\/li>\n<\/ol>\n<h5>\u5173\u8054\u6570\u7ec4\u8bbf\u95ee<\/h5>\n<ol>\n<li>\n<p>\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20<\/p>\n<pre><code class=\"language-bash\">echo ${info2[index2]}\nnginx<\/code><\/pre>\n<\/li>\n<li>\n<p>\u8bbf\u95ee\u6570\u7ec4\u4e2d\u6240\u6709\u6570\u636e<\/p>\n<pre><code class=\"language-bash\">echo ${info2[@]}\nbash shell linux nginx docker<\/code><\/pre>\n<\/li>\n<li>\n<p>\u8bbf\u95ee\u6570\u7ec4\u4e2d\u6240\u6709\u5143\u7d20\u7684\u7d22\u5f15<\/p>\n<pre><code class=\"language-bash\">echo ${!info2[@]}\nindex4 index1 index2 index3<\/code><\/pre>\n<\/li>\n<\/ol>\n<h4>\u666e\u901a\u6570\u7ec4\u573a\u666f<\/h4>\n<h5>\u666e\u901a\u6570\u7ec4\u793a\u4f8b\u811a\u672c<\/h5>\n<pre><code class=\"language-bash\">cat array01.sh\n#!\/usr\/bin\/bash\n\ni=0\nwhile read line\ndo\n        hosts[i++]=${line}\ndone &lt; \/etc\/hosts\n\nfor i in ${!hosts[@]}\ndo\n        echo \u7d22\u5f15\uff1a$i \u7d22\u5f15\u5bf9\u5e94\u7684\u5185\u5bb9\uff1a&quot;${hosts[$i]}&quot;\ndone\n\n# \u6267\u884c\u7ed3\u679c\u5982\u4e0b\nsh array01.sh\n\u7d22\u5f15\uff1a0 \u7d22\u5f15\u5bf9\u5e94\u7684\u5185\u5bb9\uff1a127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4\n\u7d22\u5f15\uff1a1 \u7d22\u5f15\u5bf9\u5e94\u7684\u5185\u5bb9\uff1a::1         localhost localhost.localdomain localhost6 localhost6.localdomain6<\/code><\/pre>\n<h5>\u5b9e\u73b0\u8d27\u5e01\u5151\u6362\u7cfb\u7edf\u811a\u672c<\/h5>\n<ul>\n<li>\n<p>\u6839\u636e\u4e1a\u52a1\u9700\u6c42\uff0c\u73b0\u8981\u6c42\u5f00\u53d1\u4e00\u4e2a\u8d27\u5e01\u5151\u6362\u7684\u670d\u52a1\u7cfb\u7edf\uff0c\u5177\u4f53\u8981\u6c42\u5982\u4e0b<\/p>\n<ul>\n<li>1.\u5b9e\u73b0\u4eba\u6c11\u5e01\u5151\u6362\u7f8e\u5143\u7684\u529f\u80fd<\/li>\n<li>2.\u5b9e\u73b0\u7f8e\u5143\u5151\u6362\u4eba\u6c11\u5e01\u7684\u529f\u80fd<\/li>\n<li>3.\u5b9e\u73b0\u4eba\u6c11\u5e01\u5151\u6362\u6b27\u5143\u7684\u529f\u80fd<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u8d27\u5e01\u5151\u6362\u5229\u7387\uff1a1\u7f8e\u5143=7.14\u4eba\u6c11\u5e01\u30011\u5143=0.12\u6b27\u5143<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">sh exchange.sh\n******\u6b22\u8fce\u4f7f\u7528\u8d27\u5e01\u8f6c\u6362\u670d\u52a1\u7cfb\u7edf******\n====================================\n1.\u6b22\u8fce\u4f7f\u7528 \u4eba\u6c11\u5e01\u8f6c\u6362\u7f8e\u5143 \u670d\u52a1\n\u60a8\u9700\u8981\u8f6c\u6362\u7684\u4eba\u6c11\u5e01\u4e3a\uff1a100 \u5143\n\u5151\u6362\u6210\u7f8e\u5143\u4e3a\uff1a14.01$\n====================================\n2.\u6b22\u8fce\u4f7f\u7528 \u7f8e\u5143\u8f6c\u6362\u4eba\u6c11\u5e01 \u670d\u52a1\n\u60a8\u9700\u8981\u8f6c\u6362\u7684\u7f8e\u5143\u4e3a\uff1a100 $\n\u5151\u6362\u6210\u4eba\u6c11\u5e01\u4e3a\uff1a714RMB\n====================================\n3.\u6b22\u8fce\u4f7f\u7528 \u4eba\u6c11\u5e01\u8f6c\u6362\u6b27\u5143 \u670d\u52a1\n\u60a8\u9700\u8981\u8f6c\u6362\u7684\u4eba\u6c11\u5e01\u4e3a\uff1a100 \u5143\n\u5151\u6362\u6210\u6b27\u5143\u4e3a\uff1a12.00\u6b27\n\ncat exchange.sh\n#!\/usr\/bin\/bash\n\nyour_money=100\nservice_menu=(\u4eba\u6c11\u5e01\u8f6c\u6362\u7f8e\u5143 \u7f8e\u5143\u8f6c\u6362\u4eba\u6c11\u5e01 \u4eba\u6c11\u5e01\u8f6c\u6362\u6b27\u5143)\n\necho &quot;******\u6b22\u8fce\u4f7f\u7528\u8d27\u5e01\u8f6c\u6362\u670d\u52a1\u7cfb\u7edf******&quot;\nfor service in ${!service_menu[@]}\ndo\n        if [ $service -eq 0 ];then\n                echo &quot;====================================&quot;\n                echo &quot;1.\u6b22\u8fce\u4f7f\u7528 ${service_menu[service]} \u670d\u52a1&quot;\n                echo &quot;\u60a8\u9700\u8981\u8f6c\u6362\u7684\u4eba\u6c11\u5e01\u4e3a\uff1a$your_money \u5143&quot;\n                new_money=$(awk -v your_money=$your_money &#039;BEGIN {printf &quot;%.2f&quot;,your_money\/7.14}&#039;)\n                echo &quot;\u5151\u6362\u6210\u7f8e\u5143\u4e3a\uff1a${new_money}$&quot;\n        fi\n        if [ $service -eq 1 ];then\n                echo &quot;====================================&quot;\n                echo &quot;2.\u6b22\u8fce\u4f7f\u7528 ${service_menu[service]} \u670d\u52a1&quot;\n                echo &quot;\u60a8\u9700\u8981\u8f6c\u6362\u7684\u7f8e\u5143\u4e3a\uff1a$your_money $&quot;\n                new_money=$(awk -v your_money=$your_money &#039;BEGIN {print your_money*7.14}&#039;)\n                echo &quot;\u5151\u6362\u6210\u4eba\u6c11\u5e01\u4e3a\uff1a${new_money}RMB&quot;\n        fi\n        if [ $service -eq 2 ];then\n                echo &quot;====================================&quot;\n                echo &quot;3.\u6b22\u8fce\u4f7f\u7528 ${service_menu[service]} \u670d\u52a1&quot;\n                echo &quot;\u60a8\u9700\u8981\u8f6c\u6362\u7684\u4eba\u6c11\u5e01\u4e3a\uff1a$your_money \u5143&quot;\n                new_money=$(awk -v your_money=$your_money &#039;BEGIN {printf &quot;%.2f&quot;,your_money*0.12}&#039;)\n                echo &quot;\u5151\u6362\u6210\u6b27\u5143\u4e3a\uff1a${new_money}\u6b27&quot;\n        fi\ndone<\/code><\/pre>\n<h5>\u83b7\u53d6\u7cfb\u7edf\u6240\u6709\u7aef\u53e3\u7684\u811a\u672c<\/h5>\n<ul>\n<li>\u9700\u6c42\uff1a\u4f7f\u7528<code>shell<\/code>\u6570\u7ec4\u7f16\u5199\u811a\u672c\uff0c\u7528\u6765\u83b7\u53d6\u4e3b\u673a\u7684\u6240\u6709\u7aef\u53e3\uff0c\u6548\u679c\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">sh discovery_port.sh\n{\n        &quot;data&quot;:[\n                {&quot;{#TCP_PORT}&quot;:&quot;10050&quot;},\n                {&quot;{#TCP_PORT}&quot;:&quot;111&quot;},\n                {&quot;{#TCP_PORT}&quot;:&quot;22&quot;},\n                {&quot;{#TCP_PORT}&quot;:&quot;3306&quot;},\n                {&quot;{#TCP_PORT}&quot;:&quot;80&quot;},\n                {&quot;{#TCP_PORT}&quot;:&quot;9000&quot;}\n        ]\n}\n\ncat discovery_port.sh\n#!\/usr\/bin\/bash\n\n# \u6570\u7ec4\narray_port=($(netstat -lntp | awk &#039;{print $4}&#039; | awk -F : &#039;{print $NF}&#039; | egrep &quot;^[0-9]+&quot; | sort | uniq))\n\n# \u7edf\u8ba1\u6570\u7ec4\u7684\u957f\u5ea6\narray_langth=${#array_port[@]}\n\n# \u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\nindex=1\n\n# \u904d\u5386\u6570\u7ec4\uff0c\u7ec4\u88c5\u6570\u636e\nprintf &quot;{\\n&quot;\nprintf &quot;\\t\\&quot;data\\&quot;:[\\n&quot;\n\nfor port in ${array_port[@]}\ndo\n        if [ $index -eq $array_langth ];then\n                printf &quot;\\t\\t{\\&quot;{#TCP_PORT}\\&quot;:\\&quot;$port\\&quot;}\\n&quot;\n        else\n                printf &quot;\\t\\t{\\&quot;{#TCP_PORT}\\&quot;:\\&quot;$port\\&quot;},\\n&quot;\n\n        fi\n        index=$[ index + 1 ]\ndone\n\nprintf &quot;\\t]\\n&quot;\nprintf &quot;}\\n&quot;<\/code><\/pre>\n<h4>\u6570\u7ec4\u904d\u5386\u4e0e\u5faa\u73af<\/h4>\n<h5>\u4ec0\u4e48\u662f\u6570\u7ec4\u904d\u5386<\/h5>\n<ul>\n<li>\u6570\u7ec4\u904d\u5386\u5176\u5b9e\u5c31\u662f\u4f7f\u7528\u5bf9\u6570\u7ec4\u8fdb\u884c\u6279\u91cf\u8d4b\u503c\uff0c\u7136\u540e\u901a\u8fc7\u5faa\u73af\u65b9\u5f0f\u6279\u91cf\u53d6\u51fa\u6570\u7ec4\u7684\u503c<\/li>\n<\/ul>\n<h5>\u6570\u7ec4\u904d\u5386\u7684\u610f\u4e49<\/h5>\n<ul>\n<li>\n<p>\u6570\u7ec4\u904d\u5386\u7684\u610f\u4e49\u5728\u4e8e\uff0c\u7edf\u8ba1\u67d0\u4e2a\u5b57\u6bb5\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u90a3\u4e48\u904d\u5386\u7684\u65b9\u5f0f\u6709\u5982\u4e0b\u4e24\u79cd\uff1a<br \/>\n<del>- 1.\u901a\u8fc7\u6570\u7ec4\u7684\u4e2a\u6570\u8fdb\u884c\u904d\u5386\uff08\u4e0d\u63a8\u8350\uff09<\/del><\/p>\n<ul>\n<li>2.\u901a\u8fc7\u6570\u7ec4\u7684\u7d22\u5f15\u8fdb\u884c\u904d\u5386\uff08\u63a8\u8350\uff09<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5982\u679c\u9700\u8981\u7edf\u8ba1\u4e00\u4e2a\u6587\u4ef6\u4e2d\u67d0\u4e2a\u5b57\u6bb5\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u600e\u4e48\u529e\uff1f<\/p>\n<ul>\n<li>\u8981\u7edf\u8ba1\u8c01\u5c31\u5c06\u8c01\u4f5c\u4e3a\u6570\u7ec4\u7684\u7d22\u5f15\uff0c\u7136\u540e\u83b7\u53d6\u7d22\u5f15\u51fa\u73b0\u7684\u6b21\u6570<\/li>\n<li>\u503c\u5f97\u4e00\u63d0\u7684\u662f\u8be5\u65b9\u6cd5\u4ec5\u652f\u6301\u5173\u8054\u6570\u7ec4<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5>\u5173\u8054\u6570\u7ec4\u7edf\u8ba1\u793a\u4f8b\u811a\u672c<\/h5>\n<p>\u7edf\u8ba1\u6587\u4ef6\u4e2d\u7537\u751f\u3001\u5973\u751f\u51fa\u73b0\u7684\u6b21\u6570<\/p>\n<ol>\n<li>\u51c6\u5907\u5bf9\u5e94\u7684\u6587\u4ef6<\/li>\n<\/ol>\n<pre><code class=\"language-bash\">cat sex.txt\njack m\nalice f\ntom m\nrose f\nrobin m\nyj m\ncindy f\n\n# \u7b2c\u4e8c\u5217\u4f5c\u4e3a\u7d22\u5f15\u540d\u79f0\uff0c\u7136\u540e\u8ba9\u7d22\u5f15\u540d\u79f0\u81ea\u589e\uff0c\u51fa\u73b0\u4e00\u6b21+1\n# \u904d\u5386\u8fd9\u4e2a\u7d22\u5f15\uff0c\u53d6\u51fa\u5bf9\u5e94\u7684\u503c\uff0c\u6b21\u6570<\/code><\/pre>\n<ol start=\"2\">\n<li>\u7f16\u5199\u904d\u5386\u811a\u672c<\/li>\n<\/ol>\n<pre><code class=\"language-bash\">cat count_sex.sh\n#!\/usr\/bin\/bash\n\ndeclare -A sex\n\n# \u5bf9\u6570\u7ec4\u8fdb\u884c\u8d4b\u503c\nwhile read line\ndo\n        # \u53d6\u51fa\u7b2c\u4e8c\u5217\u7684\u5185\u5bb9\n        type=$(echo $line | awk &#039;{print $2}&#039;)\n        # \u5b9a\u4e49\u4e00\u4e2a\u5173\u8054\u6570\u7ec4\uff0c\u8ba9\u6570\u7ec4\u7684\u503c\u4e0d\u65ad\u81ea\u589e\n        let sex[$type]++\ndone &lt; sex.txt\n\n# \u904d\u5386\u6570\u7ec4\nfor i in ${!sex[@]}\ndo\n        echo &quot;$i ${sex[$i]}&quot;\ndone<\/code><\/pre>\n<ol start=\"3\">\n<li>\u6b65\u9aa4\u62c6\u5206\u8bb2\u89e3<\/li>\n<\/ol>\n<pre><code class=\"language-bash\">declare -A sex\nsex=([m]=1 [f]=1)\nlet sex[m]++\nlet sex[f]++\necho ${!sex[@]}\nf m\necho ${sex[@]}\n2 2<\/code><\/pre>\n<h5>\u7edf\u8ba1shell\u7c7b\u578b\u6b21\u6570\u811a\u672c<\/h5>\n<p>\u9700\u6c42\uff1a\u4f7f\u7528<code>shell<\/code>\u6570\u7ec4\u7edf\u8ba1<code>\/etc\/passwd<\/code>\u7684\u4e0d\u540c<code>shell<\/code>\u7c7b\u578b\u7684\u6570\u91cf<\/p>\n<pre><code class=\"language-bash\"> cat shell_count.sh\n#!\/usr\/bin\/bash\n\ndeclare -A shell\n\n# \u5bf9\u6570\u7ec4\u8fdb\u884c\u8d4b\u503c\nwhile read line\ndo\n        type=$(echo $line | awk -F : &#039;{print $NF}&#039;)\n        let shell[$type]++\ndone &lt; \/etc\/passwd\n\n# \u5bf9\u6570\u7ec4\u8fdb\u884c\u904d\u5386\nfor i in ${!shell[@]}\ndo\n        echo \u7d22\u5f15\u662f$i\uff0c\u7d22\u5f15\u7684\u503c\u662f\uff1a${shell[$i]}\ndone<\/code><\/pre>\n<h5>\u7edf\u8ba1nginx ip\u6b21\u6570\u811a\u672c<\/h5>\n<p>\u9700\u6c42\uff1a\u4f7f\u7528<code>shell<\/code>\u6570\u7ec4\u7edf\u8ba1<code>\/var\/log\/nginx\/access.log<\/code>\u6587\u4ef6\uff0c\u76f8\u540c<code>ip<\/code>\u8bbf\u95ee\u7684\u6b21\u6570<\/p>\n<pre><code class=\"language-bash\">cat array_nginx_count.sh\n#!\/usr\/bin\/bash\ndeclare -A nginx\n\n# \u7ed9\u5173\u8054\u6570\u7ec4\u7684\u7d22\u5f15\u8fdb\u884c\u8d4b\u503c\nwhile read line\ndo\n        type=$(echo $line | awk &#039;{print $1}&#039;)\n        let nginx[$type]++\ndone &lt; \/var\/log\/nginx\/access.log\n\n# \u904d\u5386\u6570\u7ec4\nfor i in ${!nginx[@]}\ndo\n        echo &quot;IP\u662f\uff1a$i \u51fa\u73b0\u7684\u6b21\u6570\u662f ${nginx[$i]}&quot;\ndone<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>shell\u51fd\u6570 \u4ec0\u4e48\u662fshell\u51fd\u6570 \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 \u4e3a\u4f55\u9700\u8981\u51fd\u6570 \u6bd4\u5982&#8230; &raquo; <a class=\"read-more-link\" href=\"http:\/\/blue.yn.cn\/?p=1183\">\u9605\u8bfb\u5168\u6587<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-1183","post","type-post","status-publish","format-standard","hentry","category-shell"],"_links":{"self":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts\/1183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1183"}],"version-history":[{"count":15,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts\/1183\/revisions"}],"predecessor-version":[{"id":1531,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=\/wp\/v2\/posts\/1183\/revisions\/1531"}],"wp:attachment":[{"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1183"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blue.yn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}