问题
运行脚本时明明有文件,提示找不到文件,路径不存在
standard_init_linux.go:228: exec user process caused: no such file or directory
command not found
原因
Windows和Linux下文件换行符不同,Windows中换行符为CRLF(\n\r),Unix为LF(\n),
解决办法
1、Linux使用dos2unix工具将Windows下的换行符转换成Linux下换行符
$ yum install dos2unix
$ dos2unix run-lamp.sh
$ dos2unix Dockerfile
2、将编译器右下的文本编码格式修改为LF
补充:
将编译器默认格式从CRLF修改为LF,
原理
基本原理
Linux和Windows文本文件的行结束标志不同。在Linux中,文本文件用"/n"表示回车换行,而Windows用"/r/n"表示回车换行。有时候在Windows编写shell脚本时需要注意这个,否则shell脚本会报"No such file or directory"或"command not found line x"之类的错误,如果不知晓前因后果,肯定会被这个折腾得相当郁闷。
方式
使用file命令查看文件格式,显示如下格式
file <file_name>
Bourne-Again shell script, ASCII text executable, with CRLF line terminators
# 使用cat -v可以看到文件中的非打印字符。
cat -v <filename>
会在行尾显示M(\r的十六进制为0D,对应的控制符为M)
参考链接:CRLF line terminators导致shell脚本报错:command not found潇湘隐者.
Linux查看文件编码格式及文件编码转换.