为什么一个正确的shell脚本给包裹/截断/损坏的错误信息? [英] Why would a correct shell script give a wrapped/truncated/corrupted error message?

查看:250
本文介绍了为什么一个正确的shell脚本给包裹/截断/损坏的错误信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这似乎是它应该工作的命令的shell脚本,而是失败与一个奇怪的包裹/截断/损坏的错误消息。例如:

I have a shell script with a command that seems like it should work, but instead it fails with an odd wrapped/truncated/corrupted error message. Example:

$ ls -l myfile
-rw-r----- 1 me me 0 Aug  7 12:36 myfile
$ cat myscript 
ls -l myfile
$ bash myscript
: No such file or directory

该文件清楚地存在,但即使我没有,这是种错误消息我通常会得到:

The file clearly exist, but even if I didn't, this is the kind of error message I would normally get:

$ ls -l idontexist
ls: cannot access idontexist: No such file or directory

注意它包括工具名称 LS ,一个消息字符串和文件名而我却没有。

Notice how it includes the tool name ls, a message string and the filename while mine does not.

下面是我得到,如果我尝试使用的mysql 来代替。错误消息看起来像是被包裹,并且现在报价开始:

Here's what I get if I try to use mysql instead. The error message looks like it's been wrapped, and now starts with a quote:

Command:  mysql -h myhost.example.com
Expected: ERROR 2005 (HY000): Unknown MySQL server host 'myhost.example.com' (0)
Actual:   ' (0) 2005 (HY000): Unknown MySQL server host 'myhost.example.com

这是我的琐碎ssh命令应该工作,或者至少给出一个正常的错误消息,但取而代之的是包裹开始与一个冒号和奇怪重挫结尾:

And here's my trivial ssh command that should work, or at least give a normal error message, but which instead is wrapped to start with a colon and ends with strange clobbering:

Command:  ssh myhost
Expected: ssh: Could not resolve hostname myhost: Name or service not known
Actual:   : Name or service not knownname myhost

为什么会发生这种事,我怎么解决?

Why does this happen, and how do I fix it?

推荐答案

TL; DR:您的脚本或数据了Windows风格CRLF行结束

TL;DR: Your script or data has Windows style CRLF line endings.

通过删除回车转换为Unix风格。

Convert to Unix style by deleting the carriage returns.

他们可检测 ^ M 中的输出猫-v yourscript

$ cat -v myscript
ls -l myfile^M

如果你的脚本没有他们,你的数据可能 - 特别是如果从INI读/ CSV文件或卷曲

If your script doesn't have them, your data might -- especially if reading from ini/csv files or curl:

hostname=$(curl https://example.com/loginhost.txt)
ssh "$hostname"            # Shows strange error
echo "$hostname" | cat -v  # Shows myhost^M

如何删除?

设置你的编辑器来保存Unix行结尾的文件,又名行终止符或结束的行字符,并重新保存它。

How do I remove them?

Set your editor to save the file with Unix line endings, aka "line terminators" or "end-of-line characters", and resave it.

您可以在命令行中使用 DOS2UNIX的yourscript 猫也删除它们yourscript | TR -d'\\ r'> fixedscript

You can also remove them from a command line with dos2unix yourscript or cat yourscript | tr -d '\r' > fixedscript.

如果发现你的数据,你可以管你的源通过 TR -d'\\ r'

If found in your data, you can pipe your source through tr -d '\r':

hostname=$(curl https://example.com/loginhost.txt | tr -d '\r')

为什么回车导致奇怪的错误信息?

在回车字,又名CR或 \\ r ,使光标移动到行的开始,并从那里继续打印。换句话说,它开始覆盖从起始的行。这就是为什么他们奇怪的包裹:

Why do carriage returns cause strange error messages?

The "carriage return" character, aka CR or \r, causes the cursor to move to the start of the line, and continue printing from there. In other words, it starts overwriting the line from the start. This is why they wrap strangely:

Intended:     ssh: Could not resolve hostname myhost\r: Name or service not known

Written:      ssh: Could not resolve hostname myhost\r
Overwritten:  : Name or service not known
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                  
Result:       : Name or service not knownname myhost

这篇关于为什么一个正确的shell脚本给包裹/截断/损坏的错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆