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

秒速五厘米 2022-10-27 02:50 154阅读 0赞

缘起

在Linux系统中,运行Shell脚本,出现了如下错误:

  1. 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

方法2

  1. dos2unix one-more.sh

输出如下:

  1. dos2unix: converting file one-more.sh to Unix format ...

如果出现如下错误:

  1. -bash: dos2unix: command not found

说明dos2unix还没有安装,运行如下命令进行安装:

  1. yum install -y dos2unix

输出如下:

  1. Loaded plugins: fastestmirror, langpacks
  2. Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
  3. base | 3.6 kB 00:00:00
  4. extras | 2.9 kB 00:00:00
  5. salt | 2.9 kB 00:00:00
  6. updates | 2.9 kB 00:00:00
  7. Determining fastest mirrors
  8. Resolving Dependencies
  9. --> Running transaction check
  10. ---> Package dos2unix.x86_64 0:6.0.3-7.el7 will be installed
  11. --> Finished Dependency Resolution
  12. Dependencies Resolved
  13. ===========================================================================================================================================================================================================================================
  14. Package Arch Version Repository Size
  15. ===========================================================================================================================================================================================================================================
  16. Installing:
  17. dos2unix x86_64 6.0.3-7.el7 base 74 k
  18. Transaction Summary
  19. ===========================================================================================================================================================================================================================================
  20. Install 1 Package
  21. Total download size: 74 k
  22. Installed size: 190 k
  23. Downloading packages:
  24. dos2unix-6.0.3-7.el7.x86_64.rpm | 74 kB 00:00:00
  25. Running transaction check
  26. Running transaction test
  27. Transaction test succeeded
  28. Running transaction
  29. Installing : dos2unix-6.0.3-7.el7.x86_64 1/1
  30. Verifying : dos2unix-6.0.3-7.el7.x86_64 1/1
  31. Installed:
  32. dos2unix.x86_64 0:6.0.3-7.el7
  33. Complete!

发表评论

表情:
评论列表 (有 0 条评论,154人围观)

还没有评论,来说两句吧...

相关阅读