巴什 - 无缝运行的脚本与CRLF行结束 [英] Bash - seamlessly run scripts with CRLF line endings

查看:198
本文介绍了巴什 - 无缝运行的脚本与CRLF行结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的VM(在我的情况简单地boot2docker)运行在Windows主机上泊坞窗容器。对于舒适,我的源文件从主机文件系统映射,所以文本文件使用的Windows风格CRLF行结束符,而不是Unix风格的LF结局是默认。

I am using VM (in my case simply boot2docker) to run docker containers on Windows host. For convinience, my source files are mapped from host file system, so text files are by default using Windows-style CRLF line endings instead of Unix-style LF endings.

当我尝试从泊坞窗容器一些sh文件,我会得到一个错误

When I try to run some .sh file from docker container, I'll get an error

bash: ./script.sh: /bin/bash^M: bad interpreter: No such file or directory

有没有一种方法,我怎么能以某种方式告诉庆典/ SH间preTER自动转换\\ r \\ n到\\ n和运行文件?

当然,我可以做一些piplelining这样的猫script.sh | TR -d\\ r| SH ,甚至创建为一个别名,但它不会支付的情况,其中一个脚本包含另一个。

Sure, I could do some piplelining like this cat script.sh | tr -d "\r" | sh or even create an alias for that, but it would not cover situation where one script includes another.

到目前为止,我已经找到,唯一可接受的解决办法是设置的Git签UNIX格式源文件。

The only acceptable solution I have found so far, is to set Git to checkout source files in UNIX format.

推荐答案

对于一些脚本解决方案 LN -s /斌/庆典/斌/ bash的^ M 将失败。结果
因此,创建一个脚本/斌/ bash的^ M将DOS2UNIX的他们得到的参数和$ @

For some scripts the solution ln -s /bin/bash /bin/bash^M will fail.
So create a script /bin/bash^M that will dos2unix the arguments they get and run them with "$@".

编辑:
我用下面的bash ^尝试了中号脚本:

I tried it with the following bash^M script:

#!/bin/bash
PROG=$1
shift
dos2unix $PROG > /dev/null 2>&1
$PROG "$@"

在你的脚本包含其他脚本以一个点,在这种情况下,你应该避免这种变通,去了Unix格式这将失败。结果
你说你能以Unix的格式签,也可以自行更改文件。结果
并提交到Git的,它是有存储在git的UNIX格式文件UNIX改善。如果你真的想在Windows下对它们进行编辑,修改后的编辑器或者DOS2UNIX的设置。

This will fail when your script includes other scripts with a dot, in that case you should avoid this work-around and go for the Unix format.
You said you can checkout in Unix format, or you can self change the files.
And commit into git, it is an improvement to have unix files stored in git in unix format. When you really want to edit them under windows, change the settings of your editor or dos2unix afterwards.

这篇关于巴什 - 无缝运行的脚本与CRLF行结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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