• 请不要在回答技术问题时复制粘贴 AI 生成的内容
awanganddong
V2EX  ›  程序员

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

  •  
  •   awanganddong · Oct 30, 2020 · 2769 views
    This topic created in 2213 days ago, the information mentioned may be changed or developed.

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

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

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

    所以想要个 demo

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

    大家觉得呢

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

    ${messageStr/\'\"/0}

    蛋蛋忧伤
    jfcherng
    jfcherng
    Oct 30, 2020
    8
    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
    somalia
    Oct 30, 2020
    9
    我之前也要替换成 '*' ,放弃 shell 用了 python
    misaka19000
    misaka19000
    Oct 30, 2020
    10
    换 Python 写
    awanganddong
    awanganddong
    OP
    Oct 30, 2020
    11
    @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
    jfcherng
    Oct 30, 2020
    12
    因為你根本用錯啊... Pattern 不是 char list...

    [Clover@Clover-NB Desktop]$ src="x'\"y"
    [Clover@Clover-NB Desktop]$ echo ${src//[\'\"]/a}
    xaay
    awanganddong
    awanganddong
    OP
    Oct 30, 2020
    13
    @jfcherng 感谢感谢

    另外提个问题,[] 这个不太懂,我看文档上边也没体现
    jfcherng
    jfcherng
    Oct 30, 2020
    14
    就只是一般的 regex

    看到 pattern 通常不是 regex 就是能只能頂多用 wildcard (*)
    awanganddong
    awanganddong
    OP
    Oct 30, 2020
    15
    技术扎实,属于大牛级别。
    jfcherng
    jfcherng
    Oct 30, 2020
    16
    @awanganddong #15 謝謝你 今晚可以開心入眠了, 不過 regex 是很常見的東西 :)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1038 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 18:47 · PVG 02:47 · LAX 10:47 · JFK 13:47
    ♥ Do have faith in what you're doing.