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

2020-08-18 11:57:33 +08:00
 weichengwu

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

原始文本:

+ fir p build/Runner.ipa -T $FIR_TOKEN
I, [2020-08-17T18:31:29.047488 #39460]  INFO -- : Fetching com.some.bundleid@fir.im 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<xxx@163.com>.......
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 是空的,请问哪里写错了?

2217 次点击
所在节点    编程
3 条回复
Aliencn
2020-08-18 13:14:44 +08:00
Published succeed 那一行是不是多了换行符
weichengwu
2020-08-18 13:41:49 +08:00
@Aliencn #1 没有换行,是浏览器里显示不下换行了
weichengwu
2020-08-18 15:46:51 +08:00
@Aliencn #1 感谢,您说得是对的,命令行回显打开后看到 `\r` 了,我居然没想道是换行符

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/699234

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX