0

I use C# to create shell scripts to automate my tasks in Linux. In order to do this, I use below structure:

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

However when I move my file to linux due to EOL difference in windows and linux (which a fixed suggested here for linux), EOLs in shell file need to be corrected with dos2unix command. I want to know how can I manipulate my file in C# so it is not required to execute dos2unix.

What would be the way to replace new lines with linux format with minimum effort?

Community
  • 1
  • 1
VSB
  • 9,825
  • 16
  • 72
  • 145
  • You could just append ascii 10 at the end of each line. – rory.ap Mar 29 '17 at 15:09
  • @rory.ap DOS uses carriage return and line feed ("\r\n") as a line ending, which Unix uses just line feed ("\n")., so I think I should remove '\r'. – VSB Mar 29 '17 at 15:24
  • Possible duplicate of [Writing Unix style text file in C#](http://stackoverflow.com/questions/7841761/writing-unix-style-text-file-in-c-sharp) – Max Mar 29 '17 at 15:47

1 Answers1

0

Using this answer, I should change

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

to

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

The output file will not require dos2unix anymore.

Community
  • 1
  • 1
VSB
  • 9,825
  • 16
  • 72
  • 145