[英]Bash script, echo is wrapping when I don't want it to
I have a bash script wrapping a pair of curl
s, piped through some other builtins. 我有一个bash脚本,它包装了通过一对其他内置函数通过管道传递的curl
。 I want to print the result of each curl, and then parse the results and emit a third line that contains additional information. 我想打印每个curl的结果,然后解析结果并发出包含其他信息的第三行。 The final output should look like: 最终输出应如下所示:
https://e.thingzz.com/eqvjzc23xqo2s
https://e.thingzz.com/o7jlafrot2fok
https://e.thingzz.com/c/eqvjzc23xqo2s/o7jlafrot2fok
Instead, it looks like: 相反,它看起来像:
https://e.thingzz.com/eqvjzc23xqo2s
https://e.thingzz.com/o7jlafrot2fok
/o7jlafrot2fokv.com/c/eqvjzc23xqo2s
For some reason, it's wrapping at the exact length of the prior two lines. 由于某种原因,它以前两行的确切长度进行换行。
Script: 脚本:
#!/usr/bin/env bash
function myCurl()
{
curl ... | ... | ...
#omitted, results in printing a url like 'https://e.thingzz.com/UUID'
}
# param validation omitted
URL1=$(myCurl "${1}")
URL2=$(myCurl "${2}")
# print both URLs
echo $URL1
echo $URL2
# Parse URLs to get ID at end
# I've also tried using `cut`, in case there were invisible characters?
# I don't really know what I'm talking about.
URLID1=$(echo $URL1 | awk -F 'https://thing.com/' '{print $2}')
URLID2=$(echo $URL2 | awk -F 'https://thing.com/' '{print $2}')
# Doesn't work. Looks like:
# /o7jlafrot2fokv.com/c/eqvjzc23xqo2s
echo https://thing.com/c/${URLID1}/${URLID2}
# Proves I can echo something longer
echo doneaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
# Also wraps, exactly like the `echo` version
echo $URLID1 $URLID2 | awk '{ printf "https://thing.com/c/%s/%s", $1, $2 }'
I am far from a bash expert, and have no idea what's going on here. 我距离bash专家还很远,也不知道这里发生了什么。 I've tried variations on this from the command line (not from invoking a script), and it works. 我从命令行(而不是从调用脚本)尝试过这种方法,并且可以正常工作。 I'm really just at a loss here. 我真的很茫然。
EDIT: I replaced SCANID1 with URLID1 (and for 2 as well). 编辑:我用URLID1(以及2替换SCANID1)。 They were copy-paste errors. 它们是复制粘贴错误。
As Jonathan Leffler already remarked in a comment, the most likely explanation is the presence of Windows (CRLF) line endings in the data. 正如乔纳森·莱夫勒(Jonathan Leffler)在评论中所说,最可能的解释是数据中存在Windows(CRLF)行尾。 Unix uses LF line endings. Unix使用LF行尾。 On a terminal, a CR character moves the cursor to the beginning of the line without moving it to the next line. 在终端上,CR字符将光标移动到该行的开头,而不将其移动到下一行。 For example, if you run 例如,如果您运行
printf '%s\r%s\n' wibble foo
you'll see fooble
because wibble
gets displayed, then the cursor moves to the beginning of the line and foo
overwrites the beginning of wibble
. 您会看到fooble
因为显示了wibble
,然后光标移动到了该行的开头,而foo
覆盖了wibble
的开头。
Your script doesn't completely match your sample output, but it seems that the line 您的脚本与示例输出不完全匹配,但似乎该行
echo https://thing.com/c/${SCANID1}/${SCANID2}
produces output that has a slash and the value of SCANID2
at the beginning of the line. 产生的输出具有斜线,并且SCANID2
的开头具有SCANID2
的值。 That's happening because the value of SCANID1
ends with a CR character. 发生这种情况是因为SCANID1
的值以CR字符结尾。
To make your script robust against Windows line endings, remove any CR character, or at least any CR character at the end of a line (if you have CR characters in other places, you have other problems with your data). 为了使脚本对Windows行尾具有鲁棒性,请删除任何CR字符,或者至少删除行尾的任何CR字符(如果在其他位置有CR字符,则数据还有其他问题)。 In bash, "${var%$'\\r'}"
gives the value of var
minus the trailing CR if the value ends with a CR, and gives the value unchanged if it doesn't end with a CR. 在bash中, "${var%$'\\r'}"
给出的var
值减去结尾的CR(如果该值以CR结尾),如果值不以CR结束,则给出不变。
I recommend stripping the CRs early to avoid any problem: 我建议尽早剥离CR,以避免出现任何问题:
URL1=$(myCurl "${1}"); URL1=${URL1%$'\r'}
URL2=$(myCurl "${2}"); URL2=${URL2%$'\r'}
(You can't combine the command substitution and the truncation in a single assignment.) (您不能在单个分配中结合使用命令替换和截断。)
But you could also do it at the awk stage: 但是您也可以在awk阶段执行此操作:
SCANID1=$(echo "$URL1" | awk -F 'https://thing.com/' '{print sub("\r", "", $2)}')
Mind you, there's no need to invoke awk here. 请注意,这里不需要调用awk。
SCANID1=${URL1#*https://*/}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.