{"id":764,"date":"2022-03-13T20:49:22","date_gmt":"2022-03-13T12:49:22","guid":{"rendered":"https:\/\/www.buyao007.icu\/?p=764"},"modified":"2022-03-13T20:49:24","modified_gmt":"2022-03-13T12:49:24","slug":"5-shell%e7%bc%96%e7%a8%8b%e4%b9%8b%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/www.buyao007.icu\/?p=764","title":{"rendered":"5.Shell\u7f16\u7a0b\u4e4b\u6570\u7ec4"},"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.\u6570\u7ec4\u57fa\u672c\u6982\u8ff0<\/h2>\n\n\n\n<p><strong><em>1.\u4ec0\u4e48\u662f\u6570\u7ec4<\/em><\/strong><\/p>\n\n\n\n<p>\u6570\u7ec4\u5176\u5b9e\u4e5f\u7b97\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\u3002<\/p>\n\n\n\n<p><strong><em>2.\u6570\u7ec4\u7684\u5206\u7c7b<\/em><\/strong><\/p>\n\n\n\n<p>shell\u6570\u7ec4\u5206\u4e3a\u666e\u901a\u5c5e\u7ec4\u548c\u5173\u8054\u6570\u7ec4<br>\u666e\u901a\u6570\u7ec4\uff1a\u53ea\u80fd\u4f7f\u7528\u6574\u6570\u4f5c\u4e3a\u6570\u7ec4\u7d22\u5f15<br>\u5173\u8054\u6570\u7ec4\uff1a\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u4f5c\u4e3a\u6570\u7ec4\u7d22\u5f15<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">2.\u6570\u7ec4\u7684\u5b9a\u4e49\u65b9\u5f0f<\/h2>\n\n\n\n<p><strong><em>1.\u666e\u901a\u6570\u7ec4<\/em><\/strong><\/p>\n\n\n\n<p>1. \u666e\u901a\u6570\u7ec4\u7684\u5b9a\u4e49\u65b9\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7b2c\u4e00\u79cd:\r\n&#91;root@test ~]#  array&#91;1]=shell\r\n&#91;root@test ~]#  array&#91;2]=mysql\r\n&#91;root@test ~]#  array&#91;3]=docker\r\n&#91;root@test ~]# declare -a\r\ndeclare -a array='(&#91;1]=\"shell\" &#91;2]=\"mysql\" &#91;3]=\"docker\")'\r\n\u7b2c\u4e8c\u79cd\u5b9a\u4e49\u65b9\u5f0f  \u4f7f\u7528\u9ed8\u8ba4\u65b9\u5f0f\u5b9a\u4e49\u591a\u4e2a\u503c  \u5e38\u7528\u7684\u5b9a\u4e49\u65b9\u5f0f\r\n&#91;root@test ~]# array=(shell mysql docker)\r\n&#91;root@test ~]# declare -a\r\ndeclare -a array='(&#91;0]=\"shell\" &#91;1]=\"mysql\" &#91;2]=\"docker\")'\r\n&#91;root@test ~]# array=(10.0.0.1 10.0.0.2 10.0.0.7 10.0.0.61)\r\n&#91;root@test ~]# declare -a\r\ndeclare -a array='(&#91;0]=\"10.0.0.1\" &#91;1]=\"10.0.0.2\" &#91;2]=\"10.0.0.7\" &#91;3]=\"10.0.0.61\")'\r\n\u7b2c\u4e09\u79cd\u5b9a\u4e49 \u6df7\u5408\u5b9a\u4e49\r\n&#91;root@test ~]# array=(shell mysql &#91;5]=docker &#91;10]=kvm)\r\n&#91;root@test ~]# declare -a\r\ndeclare -a array='(&#91;0]=\"shell\" &#91;1]=\"mysql\" &#91;5]=\"docker\" &#91;10]=\"kvm\")'\r\n\u7b2c\u56db\u79cd\u5b9a\u4e49 \u547d\u4ee4\r\n&#91;root@test \/tmp]# touch {1..3}.txt\r\n&#91;root@test \/tmp]# ll\r\ntotal 0\r\n-rw-r--r-- 1 root root 0 Jun 30 16:31 1.txt\r\n-rw-r--r-- 1 root root 0 Jun 30 16:31 2.txt\r\n-rw-r--r-- 1 root root 0 Jun 30 16:31 3.txt\r\n&#91;root@test \/tmp]# array=(`ls`)\r\n&#91;root@test \/tmp]# declare -a\r\ndeclare -a array='(&#91;0]=\"1.txt\" &#91;1]=\"2.txt\" &#91;2]=\"3.txt\")'\r<\/code><\/pre>\n\n\n\n<p>2.\u5982\u4f55\u67e5\u770b\u6570\u7ec4<\/p>\n\n\n\n<p>declare -a\u00a0\u00a0 # \u5728\u5185\u5b58\u4e2d\u5b9a\u4e49\u7684\u6570\u7ec4<\/p>\n\n\n\n<p>3.\u5982\u4f55\u53d6\u6d88\u6570\u7ec4<\/p>\n\n\n\n<p>unset \u6570\u7ec4\u540d<\/p>\n\n\n\n<p>4.\u5982\u4f55\u67e5\u770b\u6570\u7ec4\u4e2d\u7684\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@test \/tmp]# array=(mysql docker shell)\r\n&#91;root@test \/tmp]# echo ${array&#91;*]}\r\nmysql docker shell\r\n&#91;root@test \/tmp]# echo ${array&#91;@]}\r\nmysql docker shell\r<\/code><\/pre>\n\n\n\n<p>5.\u5982\u4f55\u67e5\u770b\u67d0\u4e2a\u7d22\u5f15\u4e2d\u7684\u503c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@test \/tmp]# echo ${array&#91;2]}\r\nshell\r\n&#91;root@test \/tmp]# echo ${array&#91;1]}\r\ndocker\r\n&#91;root@test \/tmp]# echo ${array&#91;0]}\r\nmysql\r<\/code><\/pre>\n\n\n\n<p>6.\u5982\u4f55\u67e5\u770b\u6570\u7ec4\u7684\u7d22\u5f15<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@test \/tmp]# echo ${!array&#91;*]}\r\n0 1 2\r\n&#91;root@test \/tmp]# echo ${!array&#91;@]}\r\n0 1 2\r<\/code><\/pre>\n\n\n\n<p>7.\u5982\u4f55\u67e5\u770b\u603b\u5171\u7684\u7d22\u5f15\u6570\u91cf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@test \/tmp]# echo ${#array&#91;@]}\r\n3\r<\/code><\/pre>\n\n\n\n<p>8.\u6570\u7ec4\u6848\u4f8b\u63a2\u6d4b\u5730\u5740\u662f\u5426\u80fdping\u901a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@test \/tmp]# cat test.sh\r\n#!\/bin\/sh\r\nip=(10.0.0.1 10.0.0.2 10.0.0.7)\r\n\r\nfor i in ${ip&#91;*]}\r\ndo\r\n     ping -c1 -W1 $i &amp;>\/dev\/null\r\n     &#91; $? -eq 0 ] &amp;&amp; echo \"$i \u662f\u901a\u7684\"\r\ndone\r<\/code><\/pre>\n\n\n\n<p><strong><em>2.\u5173\u8054\u6570\u7ec4<\/em><\/strong><\/p>\n\n\n\n<p>1.\u5173\u8054\u6570\u7ec4\u7684\u5b9a\u4e49\u65b9\u5f0f\uff08\u5fc5\u987b\u5148\u58f0\u660e\u4e3a\u5173\u8054\u6570\u7ec4\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@test \/tmp]# declare -A array\r\n&#91;root@test \/tmp]# array&#91;index1]=shell\r\n&#91;root@test \/tmp]# array&#91;index2]=mysql\r\n&#91;root@test \/tmp]# array&#91;index3]=docker\r\n&#91;root@test \/tmp]# echo ${array&#91;*]}\r\nshell mysql docker\r\n&#91;root@test \/tmp]# echo ${!array&#91;*]}\r\nindex1 index2 index3\r<\/code><\/pre>\n\n\n\n<p>2.\u5173\u8054\u6570\u7ec4\u6848\u4f8b\u7edf\u8ba1\u7537\u5973\u51fa\u73b0\u7684\u6b21\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@test \/tmp]# cat nannu.txt\r\nm\r\nm\r\nf\r\nm\r\nf\r\nm\r\nx\r\n&#91;root@test \/tmp]# cat nannu.sh \r\ndeclare -A array\r\nfor i in `cat nannu.txt`\r\ndo\r\nlet array&#91;$i]++\r\n#\u76f8\u5f53\u4e8elet array&#91;f]++##echo ${array&#91;f]}=1\uff0c\u6bcf\u6b21\u5faa\u73af\u81ea\u589e1\r\n#\u76f8\u5f53\u4e8elet array&#91;m]++##echo ${array&#91;f]}=1\uff0c\u6bcf\u6b21\u5faa\u73af\u81ea\u589e1\r\ndone\r\nfor i in ${!array&#91;*]}\r\ndo\r\n     echo \"$i \u51fa\u73b0\u4e86 ${array&#91;$i]} \u6b21\"\r\ndone\r\n&#91;root@test \/tmp]# sh nannu.sh \r\nf \u51fa\u73b0\u4e86 2 \u6b21\r\nm \u51fa\u73b0\u4e86 4 \u6b21\r\nx \u51fa\u73b0\u4e86 1 \u6b21\r<\/code><\/pre>\n\n\n\n<p>3.\u5173\u8054\u6570\u7ec4\u6848\u4f8b\u7edf\u8ba1ip\u8bbf\u95ee\u6b21\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@test \/tmp]# cat ip.sh\r\n#!\/bin\/sh\r\ndeclare -A array\r\nwhile read line\r\ndo\r\n       ip=`echo $line|awk '{print $1}'`\r\n       let array&#91;$ip]++\r\ndone&lt;\/var\/log\/nginx\/access.log\r\nfor i in ${!array&#91;*]}\r\ndo\r\n    echo $i \u51fa\u73b0\u4e86 ${array&#91;$i]} \u6b21\r\ndone\r<\/code><\/pre>\n\n\n\n<p><\/p>\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\/764"}],"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=764"}],"version-history":[{"count":1,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":765,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/764\/revisions\/765"}],"wp:attachment":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}