美文网首页
CentOS 执行shell脚本报错 $‘\r‘: comma

CentOS 执行shell脚本报错 $‘\r‘: comma

作者: Rinaloving | 来源:发表于2023-04-27 12:49 被阅读0次

错误

  • 在Linux系统中,运行Shell脚本,出现了如下错误:
one-more.sh: line 1: $'\r': command not found

原因

出现这样的错误,是因为Shell脚本在Windows系统编写时,每行结尾是\r\n,而在Linux系统中行每行结尾是\n,所以在Linux系统中运行脚本时,会认为\r是一个字符,导致运行错误。

解决方法

  • 去除Shell脚本的\r字符:
  1. 方法1
sed -i 's/\r//' one-more.sh
  1. 方法2
dos2unix one-more.sh
  • 输出如下:
dos2unix: converting file one-more.sh to Unix format ...
  • 如果出现如下错误:说明dos2unix还没有安装
-bash: dos2unix: command not found
  • 运行如下命令进行安装 dos2unix
yum install -y dos2unix

相关文章

网友评论

      本文标题:CentOS 执行shell脚本报错 $‘\r‘: comma

      本文链接:https://www.haomeiwen.com/subject/ihnbjdtx.html