主持人:Windows 7(64位)
访客:Ubuntu 14.04(64位)
我有一个问题,让CRLF行结束转换为LFs.这导致共享文件夹中的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参数二进制设置为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 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”.
这有什么问题?我是否忽略了文档中的内容?
更新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.我设置的二进制标志不执行文档描述的方式.
更新2:
更改s.binary = true修复了问题.但是,我认为应该重新解决文件中的措辞.文档说明“如果此[标志]为真,那么Vagrant将不会这样做[将CRLF更改为LF].”据我所知,如果设置了此标志,Vagrant将不会将CRLF更改为LF.但是,如果将此设置为true,则CRLF将更改为LF.
binary
是误导性的.我已经提出了
a pull-request,这已经在文档页面上得到了纠正.
现在它说:
binary
(boolean) – Vagrant automatically replaces Windows line endings
with Unix line endings. If this isfalse
,then Vagrant will not do
this. By default this isfalse
. If the shell provisioner is
communicating over WinRM,this defaults totrue
.
因此,要用Unix行结尾(LF)替换Windows行结尾(CRLF),您需要设置:
s.binary = true
替代方案包括:
>通过以下方式手动更改行结尾:
>使用dos2unix命令,
>使用ex命令,例如
ex +'bufdo! %! tr -d \\r' -scxa *.sh
>将以下行添加到Bashrc文件中(例如〜/ .bashrc)gist:
export SHELLOPTS set -o igncr
如果您使用Git对代码进行版本控制,则应该:
>通过将core.autocrlf选项设置为input或false,在OS X上配置Git以正确处理行结尾.
如果你已经安装了Git On Windows,最常见的错误是在安装过程中选择Checkout Windows风格的选项,所以你应该重新安装它并选择:Checkout as-is并提交Unix风格的行结尾(core.autocrlf设置为输入)或按原样检出,按原样提交(core.autocrlf设置为false).
>考虑在您的存储库(.gitattributes
)中创建git规范化文件,以确保没有CRLF行结束,无论是在结账时还是在签到时,例如:
*.sh text eol=lf
所以人们编辑你的配置脚本,他们不会打破行结束.
>另请阅读:Dealing with line endings在GitHub帮助中.
>相关:‘\r’: command not found – .bashrc / .bash_profile.