Vagrant 中 Windows CRLF 到 Unix LF 的问题 [英] Windows CRLF to Unix LF Issues in Vagrant

查看:21
本文介绍了Vagrant 中 Windows CRLF 到 Unix LF 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on provisioning a few VMs using Vagrant. Here's the situation:

主机:Windows 7(64 位)

Host: Windows 7 (64-bit)

访客:Ubuntu 14.04(64 位)

Guest: Ubuntu 14.04 (64-bit)

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

I am having an issue getting the CRLF line endings to convert to LFs. This is causing the bash scripts in the shared folder to fail within the guest machine (see below).

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.

In my Vagrantfile I have set the shell privisioner parameter binary to 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 文档:

As per the Vagrant documentation:

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

binary (boolean) - Vagrant automatically replaces Windows line endings with Unix line endings. If this is true, then Vagrant will not do this. By default this is "false". If the shell provisioner is communicating over WinRM, this defaults to "true".

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

What's the issue here? Am I overlooking something in the documentation?

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

Update 1: I have tried to edit my local Git settings as recommended in this Stack Overflow answer, but no luck. Also, I have added a .gitattributes file to the root directory of the project and added the following to that file:

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

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

I have also read over "Dealing with line endings" document provided by Git. When I commit to my repository CRLFs are converted to LFs, but when I checkout changes in a Windows workspace the LFs are converted to CRLFs. This is the exact behavior I want in my Git workflow. The issue is with Vagrant. The binary flag that I set doesn't perform the way the documentation describes.

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

Update 2: Changing s.binary = true fixed the issue. However, I think the wording in the documentation should be re-addressed. The documentation states that "If this [the flag] is true, then Vagrant will not do this [change CRLF to LF]." As I understand then, Vagrant will not change CRLFs to LFs if this flag is set. However, CRLFs are changed to LFs if this is set to true.

推荐答案

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

As was specified above in my update, changing s.binary = true fixed the issue. However, I think the wording in the documentation should be re-addressed. The documentation states that "If this [the flag] is true, then Vagrant will not do this [change CRLF to LF]." As I understand then, Vagrant will not change CRLFs to LFs if this flag is set. However, CRLFs are changed to LFs if this is set to true.

这篇关于Vagrant 中 Windows CRLF 到 Unix LF 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆