用 shell 脚本写一个日志的监控脚本给钉钉
变量里边的一些特殊字符需要替换掉。比如单引号,双引号,大括号等
按照$(var/被替换字符 /替换字符) 一直不成功
所以想要个 demo
另外感觉用 shell 写比 python 写麻烦的多
大家觉得呢
1
weixiangzhe 2020 年 10 月 30 日
sed awk 了解一下,大部分有 shell 的环境都有 python 吧,那就改成 python 呗
|
2
codehz 2020 年 10 月 30 日
你括号错了
|
3
webshe11 2020 年 10 月 30 日
麻烦的话还是改 Python 好使,易读又严谨
|
4
ai277014717 2020 年 10 月 30 日
很难找到能覆盖 100%case 的 printf 试试吧
|
5
ai277014717 2020 年 10 月 30 日
我试过用 ruby 来做
`printf '%s\n' "$VAR" | ruby -e "require 'json'; puts JSON.dump(ARGF.read)" | tr -d '\n'` |
6
codehz 2020 年 10 月 30 日
特殊符号用斜杠转义,其他的不变
${var/\'/2333} |
7
awanganddong OP 这是我写的
将变量 messageStr 变量里边的单引号和双引号转化为 0 然而失败了 ${messageStr/\'\"/0} 蛋蛋忧伤 |
8
jfcherng 2020 年 10 月 30 日
https://tldp.org/LDP/abs/html/parameter-substitution.html
${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. |
9
somalia 2020 年 10 月 30 日
我之前也要替换成 '*' ,放弃 shell 用了 python
|
10
misaka19000 2020 年 10 月 30 日
换 Python 写
|
11
awanganddong OP @jfcherng 看了文档,特殊字符还是没转化为 a
``` 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 ``` |
12
jfcherng 2020 年 10 月 30 日
因為你根本用錯啊... Pattern 不是 char list...
[Clover@Clover-NB Desktop]$ src="x'\"y" [Clover@Clover-NB Desktop]$ echo ${src//[\'\"]/a} xaay |
13
awanganddong OP |
14
jfcherng 2020 年 10 月 30 日
就只是一般的 regex
看到 pattern 通常不是 regex 就是能只能頂多用 wildcard (*) |
15
awanganddong OP 技术扎实,属于大牛级别。
|
16
jfcherng 2020 年 10 月 30 日
@awanganddong #15 謝謝你 今晚可以開心入眠了, 不過 regex 是很常見的東西 :)
|