佛山网站制作哪个好薇,论坛网站制作模板,网站运营发展前景,php网站建设课程作业Linux Bash 中字符串操作所谓 子字符串 就是出现在其它字符串内的字符串. 比如 3382 就是 this is a 3382 test 的子字符串. 我们有多种方法可以从中把数字或指定部分字符串抽取出来.本文会向你展示在 bash shell 中如何获取或者说查找出子…Linux Bash 中字符串操作所谓 子字符串 就是出现在其它字符串内的字符串. 比如 3382 就是 this is a 3382 test 的子字符串. 我们有多种方法可以从中把数字或指定部分字符串抽取出来.本文会向你展示在 bash shell 中如何获取或者说查找出子字符串.在 Bash 中抽取子字符串其语法为:## 格式 ## ${parameter:offset:length}子字符串扩展是 bash 的一项功能. 它会扩展成 parameter 值中以 offset 为开始, 长为 length 个字符的字符串. 假设, $u 定义如下:## 定义变量 u ## uthis is a test那么下面参数的子字符串扩展会抽取出子字符串:var${u:10:4} echo ${var}结果为:test其中这些参数分别表示:10 : 偏移位置4 : 长度使用 IFS根据 bash 的 man 页说明:IFS (内部字段分隔符) 用于在扩展后进行单词分割, 并用内建的 read 命令将行分割为词. 默认值是.另一种 POSIX 就绪 POSIX ready 的方案如下:uthis is a test set -- $u echo $1 echo $2 echo $3 echo $4输出为:this is a test下面是一段 bash 代码, 用来从 Cloudflare cache 中去除带主页的 url.#!/bin/bash###################################################### Author - Vivek Gite## Purpose - Purge CF cache## License - Under GPL ver 3.x###################################################### set me first ## zone_idYOUR_ZONE_ID_HERE api_keyYOUR_API_KEY_HERE email_idYOUR_EMAIL_ID_HERE## hold data ## home_urlamp_url urls$## Show usage [ $urls ] { echoUsage: $0 url1 url2 url3; exit 1; }## Get home page url as we have various sub dirs on domain## /tips/## /faq/get_home_url(){localu$1IFS/set--$u echo${1}${IFS}${IFS}${3}${IFS}${4}${IFS}}echo echoPurging cache from Cloudflare...echoforuin$urlsdohome_url$(get_home_url $u)amp_url${u}amp/curl-X DELETEhttps://api.cloudflare.com/client/v4/zones/${zone_id}/purge_cache\-HX-Auth-Email: ${email_id}\-HX-Auth-Key: ${api_key}\-HContent-Type: application/json\--data{\files\:[\${u}\,\${amp_url}\,\${home_url}\]}echodoneecho它的使用方法为:~/bin/cf.clear.cache https://www.cyberciti.biz/faq/bash-for-loop/ https://www.cyberciti.biz/tips/linux-security.html借助 cut 命令可以使用 cut 命令来将文件中每一行或者变量中的一部分删掉. 它的语法为:uthis is a testecho$u|cut-d-f4echo$u|cut--delimiter --fields4############################################ WHERE## -d : Use a whitespace as delimiter## -f 4 : Select only 4th field########################################## var$(cut -d -f 4 $u) echo ${var}想了解更多请阅读 bash 的 man 页:man bash man cut来源: http://www.mzh.ren/how-to-extract-substring-in-bash.html