Bash - seamlessly run scripts with CRLF line ends

I use VM (in my case just boot2docker) to run docker containers on a Windows host. For convenience, my source files are mapped to the host file system, so text files are used by default in the end of Windows-style CRLF lines instead of Unix-style LF endings.

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

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

Is there a way in which I can somehow tell bash / sh to the interpreter to automatically convert \ r \ n to \ n and run the file?

Of course, I could do a few piplelining like this cat script.sh | tr -d "\r" | sh cat script.sh | tr -d "\r" | sh cat script.sh | tr -d "\r" | sh or even create an alias for this, but this will not cover the situation when one script includes another.

The only acceptable solution I have found so far is to install Git to check for UNIX source files.

+5
source share
4 answers

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 receive and run them using " $@ " .

Edit: I tried this with the following bash ^ M script:

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

This will fail if your script contains other dotted scripts, in which case you should avoid this work and switch to Unix format.
You said that you can check in Unix format, or you can change the files yourself.
And commit in git is an improvement to have unix files stored in git in unix format. When you really want to edit them under windows, subsequently change the settings of your editor or dos2unix.

+2
source

You can commit the .gitattributes file in your repo to the root folder to automatically tell Git to apply LF line endings to all .sh files when checking, even on Windows. Thus, you or other developers should not forget to configure Git on every computer where you clone your repo.

 # Set line endings to LF, even on Windows. Otherwise, execution within Docker fails. # See https://help.github.com/articles/dealing-with-line-endings/ *.sh text eol=lf 
+2
source

The best solution I have is to use editors (like Notepad ++ or Sublime Text) that you can save directly using the correct eol style.

Therefore, I should not dos2unix any file in the boot2docker session.

+1
source

I think you could write a script like

 #!/bin/bash #bash_run_unixified #unixify and run in bash dos2unix < "$1" | /bin/bash 

and assuming you saved it in / urs / local / bin / bash _run_unixified with the appropriate permissions ( sudo chmod o+r,o+x ), you can attach your scripts with

 #!/usr/local/bin/bash_run_unixified #This script has CRLFs in it 

If you do not need such unusual shebang lines, you could

 $ ln -s /usr/local/bin/{bash_run_unixified,bash} 

and then use (As Walter A. notes, it would be nice to associate it with bash^M )

 #!/usr/bin/env bash 

since your line is shebang ( /usr/local/bin/ usually closer to the beginning of your $PATH than /bin , so the env command should select bash , which refers to bash_run_unixified ).

(Caution: I have not tested anything)

0
source

All Articles