V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
awanganddong
V2EX  ›  程序员

shell 怎么将变量中的特殊字符替换掉

  •  
  •   awanganddong · 2020-10-30 17:26:43 +08:00 · 2001 次点击
    这是一个创建于 1273 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用 shell 脚本写一个日志的监控脚本给钉钉

    变量里边的一些特殊字符需要替换掉。比如单引号,双引号,大括号等

    按照$(var/被替换字符 /替换字符) 一直不成功

    所以想要个 demo

    另外感觉用 shell 写比 python 写麻烦的多

    大家觉得呢

    16 条回复    2020-10-30 21:48:11 +08:00
    weixiangzhe
        1
    weixiangzhe  
       2020-10-30 17:35:26 +08:00
    sed awk 了解一下,大部分有 shell 的环境都有 python 吧,那就改成 python 呗
    codehz
        2
    codehz  
       2020-10-30 17:36:42 +08:00
    你括号错了
    webshe11
        3
    webshe11  
       2020-10-30 17:39:15 +08:00
    麻烦的话还是改 Python 好使,易读又严谨
    ai277014717
        4
    ai277014717  
       2020-10-30 17:53:59 +08:00
    很难找到能覆盖 100%case 的 printf 试试吧
    ai277014717
        5
    ai277014717  
       2020-10-30 17:55:14 +08:00
    我试过用 ruby 来做
    `printf '%s\n' "$VAR" | ruby -e "require 'json'; puts JSON.dump(ARGF.read)" | tr -d '\n'`
    codehz
        6
    codehz  
       2020-10-30 17:57:20 +08:00
    特殊符号用斜杠转义,其他的不变
    ${var/\'/2333}
    awanganddong
        7
    awanganddong  
    OP
       2020-10-30 18:43:36 +08:00
    这是我写的
    将变量 messageStr 变量里边的单引号和双引号转化为 0
    然而失败了

    ${messageStr/\'\"/0}

    蛋蛋忧伤
    jfcherng
        8
    jfcherng  
       2020-10-30 19:06:22 +08:00
    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.
    somalia
        9
    somalia  
       2020-10-30 19:32:25 +08:00
    我之前也要替换成 '*' ,放弃 shell 用了 python
    misaka19000
        10
    misaka19000  
       2020-10-30 19:36:12 +08:00
    换 Python 写
    awanganddong
        11
    awanganddong  
    OP
       2020-10-30 21:08:50 +08:00
    @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

    ```
    jfcherng
        12
    jfcherng  
       2020-10-30 21:15:26 +08:00
    因為你根本用錯啊... Pattern 不是 char list...

    [Clover@Clover-NB Desktop]$ src="x'\"y"
    [Clover@Clover-NB Desktop]$ echo ${src//[\'\"]/a}
    xaay
    awanganddong
        13
    awanganddong  
    OP
       2020-10-30 21:28:39 +08:00
    @jfcherng 感谢感谢

    另外提个问题,[] 这个不太懂,我看文档上边也没体现
    jfcherng
        14
    jfcherng  
       2020-10-30 21:31:41 +08:00
    就只是一般的 regex

    看到 pattern 通常不是 regex 就是能只能頂多用 wildcard (*)
    awanganddong
        15
    awanganddong  
    OP
       2020-10-30 21:41:31 +08:00
    技术扎实,属于大牛级别。
    jfcherng
        16
    jfcherng  
       2020-10-30 21:48:11 +08:00
    @awanganddong #15 謝謝你 今晚可以開心入眠了, 不過 regex 是很常見的東西 :)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2734 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:48 · PVG 23:48 · LAX 08:48 · JFK 11:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.