在 C# 中创建 linux shell 脚本文件(不需要 dos2unix )
Create linux shell script files in C# (which does not need dos2unix )
我使用 C# 创建 shell 脚本来自动执行 Linux 中的任务。
为此,我使用以下结构:
List<string> batchFileLines = new List<string>();
batchFileLines.Add("shell command 1");
batchFileLines.Add("shell command 2");
System.IO.File.WriteAllLines(shellBatchFileName, batchFileLines.ToArray());
然而,由于 windows 和 linux (which a fixed suggested here for linux) 中的 EOL 差异,当我将文件移动到 linux 时,shell 文件中的 EOL 需要用 dos2unix
命令更正。
我想知道如何在 C# 中操作我的文件,以便不需要执行 dos2unix
。
用 linux 格式替换新行最省力的方法是什么?
使用this answer,我应该改变
System.IO.File.WriteAllLines(shellBatchFileName, batchFileLines.ToArray());
到
System.IO.File.WriteAllText(shellBatchFileName, string.Join("\n", batchFileLines.ToArray()));
输出文件将不再需要 dos2unix
。
我使用 C# 创建 shell 脚本来自动执行 Linux 中的任务。 为此,我使用以下结构:
List<string> batchFileLines = new List<string>();
batchFileLines.Add("shell command 1");
batchFileLines.Add("shell command 2");
System.IO.File.WriteAllLines(shellBatchFileName, batchFileLines.ToArray());
然而,由于 windows 和 linux (which a fixed suggested here for linux) 中的 EOL 差异,当我将文件移动到 linux 时,shell 文件中的 EOL 需要用 dos2unix
命令更正。
我想知道如何在 C# 中操作我的文件,以便不需要执行 dos2unix
。
用 linux 格式替换新行最省力的方法是什么?
使用this answer,我应该改变
System.IO.File.WriteAllLines(shellBatchFileName, batchFileLines.ToArray());
到
System.IO.File.WriteAllText(shellBatchFileName, string.Join("\n", batchFileLines.ToArray()));
输出文件将不再需要 dos2unix
。