2017-03-29 85 views
0

我使用C#创建shell脚本来在Linux中自动执行我的任务。 为了做到这一点,我用以下结构:在C#中创建linux shell脚本文件(不需要dos2unix)

List<string> batchFileLines = new List<string>(); 
batchFileLines.Add("shell command 1"); 
batchFileLines.Add("shell command 2"); 
System.IO.File.WriteAllLines(shellBatchFileName, batchFileLines.ToArray()); 

然而,当我将我的文件,将Linux由于在Windows和Linux(which a fixed suggested here for linux)EOL差异,在shell文件EOLS需要用dos2unix命令来修正。 我想知道如何在C#中操作我的文件,因此不需要执行dos2unix

以最小的努力用linux格式替换新行的方式是什么?

+0

您可以在每行的末尾添加ascii 10。 –

+0

@ rory.ap DOS使用回车符和换行符(“\ r \ n”)作为行尾,而Unix只使用换行符(“\ n”)。因此我认为我应该删除'\ r'。 – VSB

+0

[在C#中编写Unix样式文本文件]的可能重复(http://stackoverflow.com/questions/7841761/writing-unix-style-text-file-in-c-sharp) – Max

回答

0

使用this answer,我应该改变

System.IO.File.WriteAllLines(shellBatchFileName, batchFileLines.ToArray()); 

System.IO.File.WriteAllText(shellBatchFileName, string.Join("\n", batchFileLines.ToArray())); 

输出文件将不需要0​​了。