【问题标题】:Windows CRLF to Unix LF Issues in VagrantVagrant 中的 Windows CRLF 到 Unix LF 问题
【发布时间】:2015-04-23 10:45:10
【问题描述】:

我正在使用Vagrant 配置一些虚拟机。情况如下:

主机:Windows 7(64 位)

访客:Ubuntu 14.04(64 位)

我在将 CRLF 行结尾转换为 LF 时遇到问题。这导致共享文件夹中的 bash 脚本在来宾计算机中失败(见下文)。

vagrant@vagrant-host:/vagrant/bin$ sudo bash build-ubuntu-14.04.1-c
make.sh
build-ubuntu-14.04.1-cmake.sh: line 5: $'\r': command not found
build-ubuntu-14.04.1-cmake.sh: line 19: $'\r': command not found
: invalid option04.1-cmake.sh: line 21: set: -
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]
build-ubuntu-14.04.1-cmake.sh: line 22: $'\r': command not found
build-ubuntu-14.04.1-cmake.sh: line 24: $'\r': command not found
build-ubuntu-14.04.1-cmake.sh: line 26: $'\r': command not found
build-ubuntu-14.04.1-cmake.sh: line 29: $'\r': command not found
build-ubuntu-14.04.1-cmake.sh: line 36: $'\r': command not found
build-ubuntu-14.04.1-cmake.sh: line 42: $'\r': command not found
build-ubuntu-14.04.1-cmake.sh: line 46: $'\r': command not found
build-ubuntu-14.04.1-cmake.sh: line 48: $'\r': command not found
build-ubuntu-14.04.1-cmake.sh: line 50: $'\r': command not found
build-ubuntu-14.04.1-cmake.sh: line 226: syntax error: unexpected end of file

在我的 Vagrantfile 中,我将 shell privisioner 参数 binary 设置为 false。

# Provision the VM
ubuntu.vm.provision "shell" do |s|
  # replace Windows line endings with Unix line endings
  s.binary = false

  s.inline = "sudo apt-get update;
              sudo bash vagrant/bin/build-ubuntu-14.04.1-cmake.sh"
end

根据 Vagrant 文档:

binary (boolean) - Vagrant 自动将 Windows 行尾替换为 Unix 行尾。如果这是真的,那么 Vagrant 不会这样做。默认情况下,这是“假”。如果 shell 配置程序通过 WinRM 进行通信,则默认为“true”。

这里有什么问题?我是否忽略了文档中的某些内容?


更新 1: 我尝试按照this Stack Overflow answer 中的建议编辑我的本地 Git 设置,但没有运气。另外,我在项目的根目录中添加了一个.gitattributes 文件,并在该文件中添加了以下内容:

# detect all text files and automatically normalize them (convert CRLF to LF)
*       text=auto

我还阅读了 Git 提供的 "Dealing with line endings" 文档。当我提交到我的存储库时,CRLF 被转换为 LF,但是当我在 Windows 工作区中签出更改时,LF 被转换为 CRLF。这是我在 Git 工作流程中想要的确切行为。问题在于 Vagrant。我设置的 binary 标志没有按照文档描述的方式执行。


更新 2: 更改 s.binary = true 解决了这个问题。但是,我认为应该重新处理文档中的措辞。文档指出“如果这个 [标志] 为真,那么 Vagrant 不会 这样做 [将 CRLF 更改为 LF]。”据我了解,如果设置了此标志,Vagrant 将不会将 CRLF 更改为 LF。但是,如果设置为 true,则 CRLF 更改为 LF。

【问题讨论】:

  • 你应该回答你自己的问题,因为你解决了它。此外,git 的行尾内容仅对文件系统和 git 数据库之间的数据移动有影响。它不会在您的共享文件夹上执行递归 dos2unix。不过,如果您有一个单独的 Windows 和 linux 工作目录,并且使用 git pull 在它们之间移动代码,这将是有效的。您可以使用带有 LF 结尾的规范提交版本,以及带有 CRLF 行结尾的 Windows 签出。 (以及没有转换或强制到 LF 结尾的 Linux 结帐。)
  • 您当时使用的是哪个 vagrant 版本?
  • @DeanRather 我用的是最新的,1.7.1

标签: vagrant virtual-machine vagrantfile eol


【解决方案1】:

也许完成以上:

2020 年 9 月,在 Windows 10 计算机上使用 Vagrant 2.2.10,默认行为显然是尝试替换 Windows 行尾。然而,这意味着如果你在 Vagrantfile 中使用 Linux 换行符 - 它将不起作用!

我被几个稍微不同的错误弄糊涂了,比如

"Vagrantfile:1: syntax error, unexpected tIDENTIFIER, 
expecting end-of-input [+ reference to different places in the Vagrantfile]"

通过猜测,我终于有了将行尾从 Linux LF 更改为 Windows CR+LF 的想法,这使得一切正常。

TL;DR:在 Windows 上使用 Vagrant,在你的 流浪文件。

【讨论】:

    【解决方案2】:

    你说得对,documentation about binary 具有误导性。我提出了a pull-request,这已在文档页面上更正。

    所以现在它声明:

    binary (boolean) - Vagrant 自动替换 Windows 行尾 带有 Unix 行尾。 如果这是false,那么 Vagrant 不会这样做 这个。默认为false。如果 shell 配置器是 通过 WinRM 进行通信,默认为 true

    因此,要将 Windows 行尾 (CRLF) 替换为 Unix 行尾 (LF),您需要设置:

    s.binary = true
    

    替代解决方案包括:

    • 通过以下方式手动更改行尾:

      • 使用dos2unix 命令,
      • 使用ex 命令,例如

        ex +'bufdo! %! tr -d \\r' -scxa *.sh
        
    • 将以下行添加到您的 Bashrc 文件中(例如 ~/.bashrcgist

      export SHELLOPTS
      set -o igncr
      

    如果您使用 Git 对代码进行版本控制,您应该:

    • 通过将core.autocrlf 选项设置为inputfalse 来配置OS X 上的Git 以正确处理行尾。

      如果你已经在 Windows 上安装了 Git,最常见的错误是在安装过程中选择了 Checkout Windows-style 选项,所以你应该重新安装它并选择:Checkout as-is 和提交 Unix 风格的行尾(core.autocrlf 设置为 input)或按原样签出,按原样提交core.autocrlf 设置为 false)。

    • 考虑在您的存储库 (.gitattributes) 中创建 git 规范化文件,以确保在结帐和签入时都没有 CRLF 行尾,例如:

      *.sh     text eol=lf
      

      因此,编辑您的配置脚本的人不会破坏行尾。

    • 另请阅读:Dealing with line endings GitHub 帮助。

    • 相关:'\r': command not found - .bashrc / .bash_profile

    【讨论】:

    • 这是个好消息!谢谢。
    【解决方案3】:

    正如上面在我的更新中指定的,更改 s.binary = true 解决了这个问题。但是,我认为应该重新处理文档中的措辞。文档指出“如果这个 [标志] 为真,那么 Vagrant 将不会这样做 [将 CRLF 更改为 LF]。”据我了解,如果设置了此标志,Vagrant 将不会将 CRLF 更改为 LF。但是,如果设置为 true,则 CRLF 更改为 LF。

    【讨论】:

      猜你喜欢
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 2022-12-11
      • 2020-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多