Linux 带有 Control-m 字符的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10559774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File with Control-m characters
提问by user1390445
we are working on windows machine and code is deployed in linux.
我们正在 Windows 机器上工作,代码部署在 linux 中。
Some how while developing some linux scripts we have some ctrl-m character. release is provided to customer.
一些如何在开发一些 linux 脚本时我们有一些 ctrl-m 字符。发布提供给客户。
what can be the impact on ctrl-m characters in shell scripts.
对 shell 脚本中的 ctrl-m 字符有什么影响。
nus
努斯
回答by Ignacio Vazquez-Abrams
It will change the shebang line, so that the interpreter isn't found.
它将更改shebang 行,以便找不到解释器。
$ ./t.sh
bash: ./t.sh: /bin/bash^M: bad interpreter: No such file or directory
回答by Adam Liss
The ^M
characters are carriage returns. Windows and DOS terminate lines of text with CR
(^M
, or ASCII code 13) followed by LF
(^J
, or linefeed, ASCII code 10). Linux uses just LF
, so the carriage returns appear as part of the text and are interpreted as such. That means they'll break scripts.
该^M
字符是回车。Windows 和 DOS 使用CR
( ^M
, 或 ASCII 代码 13) 后跟LF
( ^J
, 或换行, ASCII 代码 10) 来终止文本行。Linux 只使用LF
,因此回车作为文本的一部分出现并被解释为这样。这意味着他们会破坏脚本。
Most linux distributions come with a utility called dos2unixpre-installed. You can use it to convert the end-of-line characters from DOS style to linux style.
大多数 linux 发行版都预装了一个名为dos2unix的实用程序。您可以使用它来将行尾字符从 DOS 样式转换为 linux 样式。
Some editors will do the conversion automatically for you; others have options that let you specify which mode to use.
一些编辑器会自动为您进行转换;其他一些选项可让您指定要使用的模式。
回答by Neeraj
open the file in vi editor
在 vi 编辑器中打开文件
press escape and type below on command line in the file.
按转义键并在文件的命令行上键入以下内容。
:%s/^M//g
:%s/^M//g
make sure ^M is added by hitting Cntrl key + V + M then hit enter
确保通过按 Cntrl 键 + V + M 添加 ^M 然后按 Enter
it will remove the characters and you can save the file.
它将删除字符,您可以保存文件。
回答by prasankn
You can use the below syntax too. Since in windows, this Control+v+m will not work , you can use \r instead.
您也可以使用以下语法。由于在 Windows 中,此 Control+v+m 不起作用,您可以使用 \r 代替。
Open the file in binary mode to view the Control-M characters using vim editor with -b option and use the below command to replace the Control-M characters
使用带有 -b 选项的 vim 编辑器以二进制模式打开文件以查看 Control-M 字符,并使用以下命令替换 Control-M 字符
%s@\r@@g
%s@\r@@g