bash 当我通过 shell 脚本创建 .txt 文件时,为什么问号出现在文件名的末尾?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37566064/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:43:05  来源:igfitidea点击:

why questionmark comes in the end of filename when i create .txt file through shell script?

linuxbashshellgrep

提问by vipul chauhan

I am writing one shell script in which I am supposed to create 1 text file. When I do this, a question mark comes at the end of file name. what is the reason?

我正在编写一个 shell 脚本,我应该在其中创建 1 个文本文件。当我这样做时,一个问号出现在文件名的末尾。是什么原因?

I am trying below methods in bash script.

我正在 bash 脚本中尝试以下方法。

1) grep ERROR a1* > text.txt

1) grep ERROR a1* > text.txt

2) touch text.txt

2) touch text.txt

In both the methods, instead of text.txt, there is a file generated as text.txt?

在这两种方法中,不是 ,而是text.txt生成了一个文件text.txt?

what should I do to overcome this?

我该怎么做才能克服这个问题?

采纳答案by andlrc

Sounds like you script uses \r\nas line endings, this is typical DOS style line endings. Unix like systems uses \n. You should try to change the line feeds, eg with your favorite text editor:

听起来您的脚本\r\n用作行尾,这是典型的 DOS 样式行尾。类 Unix 系统使用\n. 您应该尝试更改换行符,例如使用您最喜欢的文本编辑器:

vim +'set ff=unix | x' my_script

Or with dos2unix:

或与dos2unix

dos2unix my_script

Or sed:

或 sed:

sed -i 's/\r$//' my_script