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

本人菜鸡,请教一个 shell 脚本问题

  •  
  •   weichengwu · 2020-08-18 11:57:33 +08:00 · 2203 次点击
    这是一个创建于 1559 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求:从一段文本中提取字符串拼装并返回

    原始文本:

    + fir p build/Runner.ipa -T $FIR_TOKEN
    I, [2020-08-17T18:31:29.047488 #39460]  INFO -- : Fetching [email protected] uploading info......
    I, [2020-08-17T18:31:29.047540 #39460]  INFO -- : Uploading app: Runner-1.0.0(Build 1)
    I, [2020-08-17T18:31:29.209935 #39460]  INFO -- : Publishing app via xxx<[email protected]>.......
    I, [2020-08-17T18:31:29.209980 #39460]  INFO -- : ✈ -------------------------------------------- ✈
    I, [2020-08-17T18:31:29.209991 #39460]  INFO -- : begin to upload ...
    D, [2020-08-17T18:31:29.210023 #39460] DEBUG -- : begin to upload icon
    D, [2020-08-17T18:31:29.210159 #39460] DEBUG -- : icon_url = https://fir-app-icon.oss-cn-beijing.aliyuncs.com/8dff2f7f2lf0aa7cd1d2314cc09d6375db7bf239, icon_info = #<File:0x00007fd81db5c578>
    D, [2020-08-17T18:31:29.405214 #39460] DEBUG -- : begin to callback api
    D, [2020-08-17T18:31:29.486529 #39460] DEBUG -- : callback api finished
    D, [2020-08-17T18:31:29.486567 #39460] DEBUG -- : upload icon finished
    D, [2020-08-17T18:31:29.486579 #39460] DEBUG -- : begin to upload binary ...
    D, [2020-08-17T18:31:29.486692 #39460] DEBUG -- : binary_url = https://fir-app-binary.oss-cn-beijing.aliyuncs.com/15f4fdf09fee89137ca42d377903cb9jfc325a75, binary_info = #<File:0x00007fd81b8c9f60>
    D, [2020-08-17T18:31:29.486714 #39460] DEBUG -- : {"CONTENT-DISPOSITION"=>"attachment; filename* = UTF-8''Runner.ipa", "Content-Type"=>"application/octet-stream", "date"=>"Mon, 17 Aug 2020 10:31:29 GMT", "x-oss-date"=>"Mon, 17 Aug 2020 10:31:29 GMT", "authorization"=>"OSS xxx"}
    D, [2020-08-17T18:33:01.875110 #39460] DEBUG -- : begin to callback api
    D, [2020-08-17T18:33:02.798259 #39460] DEBUG -- : callback api finished
    D, [2020-08-17T18:33:02.798294 #39460] DEBUG -- : upload binary ... finished
    I, [2020-08-17T18:33:02.798362 #39460]  INFO -- : File uploaded. During: 93 seconds, Upload Speed: 2269 KB/s 
    I, [2020-08-17T18:33:02.798376 #39460]  INFO -- : App id is 582320e1calfs83366c7ayes
    I, [2020-08-17T18:33:02.798383 #39460]  INFO -- : Release id is 5f3a5fhsb2eb489a96sadekl
    I, [2020-08-17T18:33:02.798409 #39460]  INFO -- : Fetch app info from fir.im
    I, [2020-08-17T18:33:02.911772 #39460]  INFO -- : end upload
    I, [2020-08-17T18:33:02.911811 #39460]  INFO -- : ✈ -------------------------------------------- ✈
    I, [2020-08-17T18:33:02.911835 #39460]  INFO -- : Published succeed: http://d.firim.info/hspo
    I, [2020-08-17T18:33:05.827140 #39460]  INFO -- : 
    

    需要找到 Published succeed: 所在行后面的内容,加上 Release id is 所在行后面的内容

    拼装后的目标:

    http://d.firim.info/hspo?release_id=5f3a5fhsb2eb489a96sadekl
    

    附上我写的脚本:

    #!/bin/bash
    
    fir_result=$(</dev/stdin)
    
    get_substr_after() {
        local match_line=$(grep "$1" <<< "$fir_result")
        echo ${match_line#*$1}
    }
    
    short_url=$(get_substr_after "Published succeed: ")
    release_id=$(get_substr_after "Release id is ")
    
    echo "${short_url}?release_id=${release_id}"
    

    实际输出结果:

    ?release_id=5f3a5fhsb2eb489a96sadekl
    

    short_url 是空的,请问哪里写错了?

    第 1 条附言  ·  2020-08-18 13:44:00 +08:00
    `Published succeed: http://d.firim.info/hspo` 是显示在一行里的,浏览器里换行了。
    第 2 条附言  ·  2020-08-18 15:47:22 +08:00
    已解决

    ```
    short_url=${short_url//[$'\r\n']}
    ```
    Aliencn
        1
    Aliencn  
       2020-08-18 13:14:44 +08:00
    Published succeed 那一行是不是多了换行符
    weichengwu
        2
    weichengwu  
    OP
       2020-08-18 13:41:49 +08:00
    @Aliencn #1 没有换行,是浏览器里显示不下换行了
    weichengwu
        3
    weichengwu  
    OP
       2020-08-18 15:46:51 +08:00
    @Aliencn #1 感谢,您说得是对的,命令行回显打开后看到 `\r` 了,我居然没想道是换行符
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1092 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 18:28 · PVG 02:28 · LAX 10:28 · JFK 13:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.