shell 怎么将变量中的特殊字符替换掉
用 shell 脚本写一个日志的监控脚本给钉钉
变量里边的一些特殊字符需要替换掉。比如单引号,双引号,大括号等
按照$(var/被替换字符 /替换字符) 一直不成功
所以想要个 demo
另外感觉用 shell 写比 python 写麻烦的多
大家觉得呢
用 shell 脚本写一个日志的监控脚本给钉钉
变量里边的一些特殊字符需要替换掉。比如单引号,双引号,大括号等
按照$(var/被替换字符 /替换字符) 一直不成功
所以想要个 demo
另外感觉用 shell 写比 python 写麻烦的多
大家觉得呢
${messageStr/'”/0}
蛋蛋忧伤
${var/Pattern/Replacement}
First match of Pattern, within var replaced with Replacement.
If Replacement is omitted, then the first match of Pattern is replaced by nothing, that is, deleted.
${var//Pattern/Replacement}
Global replacement. All matches of Pattern, within var replaced with Replacement.
As above, if Replacement is omitted, then all occurrences of Pattern are replaced by nothing, that is, deleted.
“`
myDate=`date “+%Y-%m-%d %R”`
message=`less ‘laravel.log’|grep ‘local.ERROR’|grep “${myDate}”`
messageStr=””
for line in $message
do
messageStr=”$messageStr$line”
done
if [ $messageStr ];then
messageStr=${messageStr//'”/a}
echo $messageStr;
curl ‘https://oapi.dingtalk.com/robot/send?access_token=token’
-H “Content-type: application/json”
-X POST
-d ‘{“msgtype”: “text”,”text”: {“content”: “‘$messageStr'”},”isAtAll”: true}’
fi
“`
[[email protected] Desktop]$ src=”x'”y”
[[email protected] Desktop]$ echo ${src//[‘”]/a}
xaay
另外提个问题,[] 这个不太懂,我看文档上边也没体现
看到 pattern 通常不是 regex 就是能只能頂多用 wildcard (*)