CentOS: $‘\r‘: command not found

一、问题

在CentOS中执行脚本deploy.sh时,报错$‘\r’: command not found
在这里插入图片描述
原因:shell脚本是在Windows下编写,通过工具软件传输到CentOS中。而Windows下行结尾符号为\r\nlinux下行结尾时\n,两者不同,所以无法识别,需要转换一下。

二、解决

//执行sh  deploy.sh base,报错$'\r': command not found
sh  deploy.sh base

//1、dos2unix 脚本名,此处是dos2unix deploy.sh
//如果执行dos2unix deploy.sh报-bash:dos2unix:command not found,就使用yum install -y dos2unix 安装dos2unix
//如果执行成功,会报一个dos2unix: converting file deploy.sh to Unix format ...
dos2unix deploy.sh  

//2、安装dos2unix
yum install -y dos2unix

//3、再次执行dos2unix deploy.sh命令
//如果dos2unix deploy.sh执行成功,会报一个dos2unix: converting file deploy.sh to Unix format ...
//如果dos2unix deploy.sh执行成功后,再次执行sh  deploy.sh base还不行,依旧报 $'\r': command not found,就执行下面的sed命令


//4、sed -i 's/\r//' 脚本名
sed -i 's/\r//' deploy.sh

//5、再次执行sh  deploy.sh base
sh  deploy.sh base

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
以上dos2unix deploy.sh和sed -i ‘s/\r//’ deploy.sh 都执行成功后,再次执行sh deploy.sh base(此处对应上面5、再次执行sh deploy.sh base)开始执行脚本命令,pull、build、running,创建images与container。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三、参考资料

$‘\r‘: command not found的解决方法

shell脚本执行错误 $‘\r’:command not found

Linux下执行Shell脚本出现$‘\r‘: command not found解决方法

Linux 错误: $‘\r’: command not found

猜你喜欢

转载自blog.csdn.net/qyfx123456/article/details/131874240