(原创)Windows下编译的Shell脚本不能再Linux中运行的解决办法

一、原理

Windows编译的文件和Linux编译的文件格式不太一样,导致在Linux运行Shell脚本的时候会提示:/bin/bash^M: bad interpreter: 没有那个文件或目录。

原因是这样的:

1、Windows编译的文件结束时(回车+换行)

2、Linux编译的文件结束时(换行)

           

这样导致了Windows编译的文件放在Linux中会有[noeol][dos]的Flag标示。

如果运行CAT命令可以更直观的看到两个不同操作系统产生的文件差异,Windows产生的shell脚本文件中多了一个^M

命令格式:cat -A filename
cat -A windows.sh
cat -A linux.sh

二、解决办法

在Windows生成的脚本进入vim编译模式,输入“:set fileformat=unix #转换成unix格式”,回车后wq保存退出即可。

现在可以用./Window.sh或者sh Window.sh在Linux中运行Shell脚本了,很神奇有木有^-^

大功告成~

原文地址:https://www.cnblogs.com/demonzyz/p/4494883.html