{"id":47342,"date":"2023-04-29T14:19:37","date_gmt":"2023-04-29T06:19:37","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=47342"},"modified":"2023-04-29T14:19:37","modified_gmt":"2023-04-29T06:19:37","slug":"bash3-bash4-different","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/bash3-bash4-different.html","title":{"rendered":"bash3\u4e0ebash4\u6570\u7ec4\u7ed3\u6784\u7684\u533a\u522b"},"content":{"rendered":"
<\/p>\n
bash3.x\u4e0d\u652f\u6301\u5173\u8054\u6570\u7ec4(associative arrays)\u7ed3\u6784\uff0c\u53ea\u652f\u6301\u7528\u6570\u503c\u4f5c\u6570\u7ec4\u7d22\u5f15\uff0c\u5982<\/p>\n
declare -a Myarray\r\nMyarray[1]=\u201dwww.ttlsa.com\u201d\r\nMyarray[2]=\u201d39514058\u201d<\/pre>\nbash4.x\u652f\u6301\u5173\u8054\u6570\u7ec4\u7ed3\u6784\uff0c\u5982<\/p>\n
declare -A Myarray\r\nMyarray['website']=\u201dwww.ttlsa.com\u201d\r\nMyarray['QQqun']=\u201d39514058\u201d<\/pre>\n\u652f\u6301\u5173\u8054\u6570\u7ec4\u7ed3\u6784\uff0c\u5c06\u5927\u5927\u65b9\u4fbf\u5b9a\u4e49\u53d8\u91cf\uff0c\u66f4\u597d\u7684\u5199\u51fa\u65b9\u4fbf\u7b80\u6d01\u7684\u4ee3\u7801\u3002\u7b80\u5355\u5c31\u662f\u7f8e\u3002
\n\u6211\u4eec\u5de5\u4f5c\u4e2d\u770b\u5230\u4e00\u63a8if...elsif...else\u8bed\u53e5\uff0c\u5341\u591a\u4e2a\u5224\u65ad\uff0c\u773c\u775b\u90fd\u770b\u82b1\u4e86\uff0c\u4fee\u6539\u8d77\u6765\u4e5f\u4e0d\u65b9\u4fbf\u3002
\n\u987a\u4fbf\u6539\u4e86\u4e0b\uff1a<\/p>\n#!\/bin\/bash\r\n#\r\nftpserver=(\r\n \"121.207.242:ftpserver1.backup.xx.com\"\r\n \"121.207.240:ftpserver2.backup.xx.com\"\r\n \"121.207.250:ftpserver3.backup.xx.com\"\r\n )\r\n\r\nfunction process_array() {\r\n declare -a hash=(\"${!1}\")\r\n key=\"${2}\"\r\n for ftpsrv in \"${hash[@]}\"; do\r\n\t# key: ${ftpsrv%%:*}\r\n\t# value: ${ftpsrv##*:}\r\n if [ $key = ${ftpsrv%%:*} ];then\r\n ftpserver=${ftpsrv#*:}\r\n echo $ftpserver\r\n fi\r\n done\r\n}\r\nftp_s=$(process_array ftpserver[@] \"121.207.250\")\r\necho $ftp_s<\/pre>\n\u8bf4\u660e\u4e0b\uff1a121.207.242:ftpserver1.backup.xx.com<\/span>\uff0c\u5192\u53f7\u524d\u9762\u662fkey\uff0c\u540e\u9762\u662fvalue\uff0c\u4e0d\u540c\u7684IP\u6bb5\u5bf9\u5e94\u4e0d\u540c\u7684ftp\u5730\u5740\u3002\u6ca1\u6709\u5168\u90e8\u628aif...elsif...else\u8bed\u53e5\u6761\u4ef6\u7f57\u5217\u5230\u53d8\u91cfftpserver\u3002\u8981\u6dfb\u52a0\u66f4\u6539\u76f8\u5173\u4fe1\u606f\uff0c\u53ea\u9700\u66f4\u6539ftpserver\u53d8\u91cf\u5373\u53ef\u3002<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.ttlsa.com\/shell\/bash3-and-bash4-array\/<\/a><\/p>\n