{"id":90121,"date":"2024-03-04T02:03:47","date_gmt":"2024-03-03T18:03:47","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=90121"},"modified":"2024-03-04T02:03:47","modified_gmt":"2024-03-03T18:03:47","slug":"bash-extract-string","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/bash-extract-string.html","title":{"rendered":"Bash\u4e0b\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6240\u8c13\u201c\u5b50\u5b57\u7b26\u4e32\u201d\u5c31\u662f\u51fa\u73b0\u5728\u5176\u5b83\u5b57\u7b26\u4e32\u5185\u7684\u5b57\u7b26\u4e32\u3002 \u6bd4\u5982 \u201c3382\u201d \u5c31\u662f \u201cthis is a 3382 test\u201d \u7684\u5b50\u5b57\u7b26\u4e32\u3002 \u6211\u4eec\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4ece\u4e2d\u628a\u6570\u5b57\u6216\u6307\u5b9a\u90e8\u5206\u5b57\u7b26\u4e32\u62bd\u53d6\u51fa\u6765\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\n\u672c\u6587\u4f1a\u5411\u4f60\u5c55\u793a\u5728 bash shell \u4e2d\u5982\u4f55\u83b7\u53d6\u6216\u8005\u8bf4\u67e5\u627e\u51fa\u5b50\u5b57\u7b26\u4e32\u3002<\/p>\n

\u5728 Bash \u4e2d\u62bd\u53d6\u5b50\u5b57\u7b26\u4e32<\/strong><\/div>\n

\u5176\u8bed\u6cd5\u4e3a\uff1a<\/p>\n

## \u683c\u5f0f ##\r\n${parameter:offset:length}\r\n<\/pre>\n

\u5b50\u5b57\u7b26\u4e32\u6269\u5c55\u662f bash \u7684\u4e00\u9879\u529f\u80fd\u3002\u5b83\u4f1a\u6269\u5c55\u6210 parameter \u503c\u4e2d\u4ee5 offset \u4e3a\u5f00\u59cb\uff0c\u957f\u4e3a length \u4e2a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u3002 \u5047\u8bbe\uff0c $u \u5b9a\u4e49\u5982\u4e0b:<\/p>\n

## \u5b9a\u4e49\u53d8\u91cf u ##\r\nu=\"this is a test\"\r\n<\/pre>\n

\u90a3\u4e48\u4e0b\u9762\u53c2\u6570\u7684\u5b50\u5b57\u7b26\u4e32\u6269\u5c55\u4f1a\u62bd\u53d6\u51fa\u5b50\u5b57\u7b26\u4e32:<\/p>\n

var=\"${u:10:4}\"\r\necho \"${var}\"\r\n<\/pre>\n

\u7ed3\u679c\u4e3a:<\/p>\n

test\r\n<\/pre>\n

\u5176\u4e2d\u8fd9\u4e9b\u53c2\u6570\u5206\u522b\u8868\u793a\uff1a<\/p>\n

10 : \u504f\u79fb\u4f4d\u7f6e
\n4 : \u957f\u5ea6<\/p>\n

\u4f7f\u7528 IFS<\/strong><\/div>\n

\u6839\u636e bash \u7684 man \u9875\u8bf4\u660e:<\/p>\n

IFS \uff08\u5185\u90e8\u5b57\u6bb5\u5206\u9694\u7b26\uff09\u7528\u4e8e\u5728\u6269\u5c55\u540e\u8fdb\u884c\u5355\u8bcd\u5206\u5272\uff0c\u5e76\u7528\u5185\u5efa\u7684 read \u547d\u4ee4\u5c06\u884c\u5206\u5272\u4e3a\u8bcd\u3002\u9ed8\u8ba4\u503c\u662f\u3002<\/p>\n

\u53e6\u4e00\u79cd POSIX \u5c31\u7eeaPOSIX ready\u7684\u65b9\u6848\u5982\u4e0b\uff1a<\/p>\n

u=\"this is a test\"\r\nset -- $u\r\necho \"$1\"\r\necho \"$2\"\r\necho \"$3\"\r\necho \"$4\"\r\n<\/pre>\n

\u8f93\u51fa\u4e3a\uff1a<\/p>\n

this\r\nis\r\na\r\ntest\r\n<\/pre>\n

\u4e0b\u9762\u662f\u4e00\u6bb5 bash \u4ee3\u7801\uff0c\u7528\u6765\u4ece Cloudflare cache \u4e2d\u53bb\u9664\u5e26\u4e3b\u9875\u7684 url\u3002<\/p>\n

#\uff01\/bin\/bash\r\n####################################################\r\n## Author - Vivek Gite {https:\/\/www.cyberciti.biz\/}\r\n## Purpose - Purge CF cache\r\n## License - Under GPL ver 3.x+\r\n####################################################\r\n## set me first ##\r\nzone_id=\"YOUR_ZONE_ID_HERE\"\r\napi_key=\"YOUR_API_KEY_HERE\"\r\nemail_id=\"YOUR_EMAIL_ID_HERE\"\r\n## hold data ##\r\nhome_url=\"\"\r\namp_url=\"\"\r\nurls=\"$@\"\r\n## Show usage\r\n[ \"$urls\" == \"\" ] && { echo \"Usage: $0 url1 url2 url3\"; exit 1; }\r\n## Get home page url as we have various sub dirs on domain\r\n## \/tips\/\r\n## \/faq\/\r\nget_home_url(){\r\nlocal u=\"$1\"\r\nIFS='\/'\r\nset -- $u\r\necho \"${1}${IFS}${IFS}${3}${IFS}${4}${IFS}\"\r\n}\r\necho\r\necho \"Purging cache from Cloudflare\u3002.\u3002\"\r\necho\r\nfor u in $urls\r\ndo\r\nhome_url=\"$(get_home_url $u)\"\r\namp_url=\"${u}amp\/\"\r\ncurl -X DELETE \"https:\/\/api.cloudflare.com\/client\/v4\/zones\/${zone_id}\/purge_cache\" \\\r\n-H \"X-Auth-Email: ${email_id}\" \\\r\n-H \"X-Auth-Key: ${api_key}\" \\\r\n-H \"Content-Type: application\/json\" \\\r\n--data \"{\\\"files\\\":[\\\"${u}\\\"\uff0c\\\"${amp_url}\\\"\uff0c\\\"${home_url}\\\"]}\"\r\necho\r\ndone\r\necho\r\n<\/pre>\n

\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\u4e3a\uff1a<\/p>\n

~\/bin\/cf.clear.cache https:\/\/www.cyberciti.biz\/faq\/bash-for-loop\/ \r\nhttps:\/\/www.cyberciti.biz\/tips\/linux-security.html\r\n<\/pre>\n
\u501f\u52a9 cut \u547d\u4ee4<\/strong><\/div>\n

\u53ef\u4ee5\u4f7f\u7528 cut \u547d\u4ee4\u6765\u5c06\u6587\u4ef6\u4e2d\u6bcf\u4e00\u884c\u6216\u8005\u53d8\u91cf\u4e2d\u7684\u4e00\u90e8\u5206\u5220\u6389\u3002\u5b83\u7684\u8bed\u6cd5\u4e3a\uff1a<\/p>\n

u=\"this is a test\"\r\necho \"$u\" | cut -d' ' -f 4\r\necho \"$u\" | cut --delimiter=' ' --fields=4\r\n##########################################\r\n## WHERE\r\n## -d' ' : Use a whitespace as delimiter\r\n## -f 4 : Select only 4th field\r\n##########################################\r\nvar=\"$(cut -d' ' -f 4 <<< $u)\"\r\necho \"${var}\"\r\n<\/pre>\n

\u60f3\u4e86\u89e3\u66f4\u591a\u8bf7\u9605\u8bfb bash \u7684 man \u9875\uff1a<\/p>\n

man bash\r\nman cut\r\n<\/pre>\n

\u53e6\u8bf7\u53c2\u89c1\uff1a Bash String Comparison: Find Out IF a Variable Contains a Substring
\nvia: https:\/\/www.cyberciti.biz\/faq\/how-to-extract-substring-in-bash\/<\/a><\/p>\n

\u4f5c\u8005\uff1aVivek Gite<\/a> \u8bd1\u8005\uff1alujun9972<\/a> \u6821\u5bf9\uff1awxy<\/a><\/p>\n

\u672c\u6587\u7531 LCTT<\/a> \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd<\/a> \u8363\u8a89\u63a8\u51fa<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/linux.cn\/article-9127-1.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/bash-extract-string.html<\/a>\u7f16\u8f91\uff1a\u5f20\u5b8f\u5b87\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb \u6240\u8c13\u201c\u5b50\u5b57\u7b26\u4e32\u201d\u5c31\u662f\u51fa\u73b0\u5728\u5176\u5b83\u5b57\u7b26\u4e32\u5185\u7684\u5b57\u7b26\u4e32\u3002 \u6bd4\u5982 \u201c3382\u201d \u5c31\u662f \u201cthis is a 338 […]<\/p>\n","protected":false},"author":1329,"featured_media":90123,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-90121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/90121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=90121"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/90121\/revisions"}],"predecessor-version":[{"id":90194,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/90121\/revisions\/90194"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/90123"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=90121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=90121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=90121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}