简体   繁体   English

Bash脚本,当我不希望它回显时

[英]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.

相关问题 当我也不想要Bash脚本时,请使用双引号 - Bash Script Echoing double quotes when I don't want it too 在bash中包装一个perl脚本-向IRC机器人回显 - wrapping a perl script in bash - echo to IRC bot Bash替换字符串我不想要 - Bash replaceing string i don't want it to bash脚本-我想检查XLS是否为空。 如果是这样,我什么也不想做。 如果不是,我想做点什么 - bash script - I want to check if XLS is empty. if it is, i don't want to do anything. If it is not, I want to do something 为什么我不能在bash脚本中添加带有“ echo >>”的行? - Why I can't add lines with “echo >>” from bash script? 当我从 bash 脚本开始时,Freenet 消息系统不起作用 - Freenet Message System don't work when i start with bash script 当我不想要它时,ksh脚本中的eval命令函数是通配符 - eval command function within ksh script is globbing when I don't want it to 我想使用parallel-ssh在多个服务器上运行bash脚本,但它只是打印echo语句 - I want to use parallel-ssh to run a bash script on multiple servers, but it simple prints the echo statements 在bash中执行mysql命令时不回显特定的错误消息 - Don't echo specific error message when executing mysql command in bash 我不明白 Bash 脚本中“$x*”中的 * 是什么 - I don't understand what the * in "$x*" in my Bash Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM