【问题标题】:Create linux shell script files in C# (which does not need dos2unix )在 C# 中创建 linux shell 脚本文件(不需要 dos2unix )
【发布时间】:2017-03-29 15:05:42
【问题描述】:

我使用 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 时,需要使用 dos2unix 命令更正 shell 文件中的 EOL。 我想知道如何在 C# 中操作我的文件,因此不需要执行dos2unix

用 linux 格式替换新行的方法是什么?

【问题讨论】:

  • 你可以在每一行的末尾附加 ascii 10。
  • @rory.ap DOS 使用回车和换行 ("\r\n") 作为行尾,Unix 只使用换行 ("\n")。所以我想我应该删除“\r”。

标签: c# linux shell portability eol


【解决方案1】:

使用this answer,我应该改变

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

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

输出文件将不再需要dos2unix

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多