Jiang LiHeng Good is good, but better carries it.

Unix
inux 执行 shell 报错:“$' ': 未找到命令” 的解决办法

2021-05-19
jiangliheng
本文 1026 字,阅读全文约需 3 分钟

原因

大多数原因是因为 shell 脚本是在 Windows 编写导致的换行问题,具体原因是 Windows 的换行符号为 CRLF\r\n),而 Unix\Linux 为 LF\n)。

名称解释

缩写 全称 ASCII转义 说明
CR Carriage Return \r 回车
LF Linefeed \n 换行,Unix\Linux 的换行符
CRLF Carriage Return & Linefeed \r\n 回车并换行,Windows 的换行符

方法一(推荐):vim 转换为 Unix 换行

# 测试脚本
$ cat windows.sh
#!/usr/bin/env bash
date

# 重现报错
$ sh windows.sh
windows.sh:行2: $'date\r': 未找到命令

# 查看文件格式信息
$ file windows.sh
windows.sh: a /usr/bin/env bash\015 script, ASCII text executable, with CRLF line terminators

# 转换为 Unix 换行
$ vim windows.sh
:set ff=unix
:wq

# 再次查看文件格式信息
$ file windows.sh
windows.sh: a /usr/bin/env bash script, ASCII text executable

方法二:dos2unix

# 安装 dos2unix
$ yum install dos2unix

# 转换为 unix 格式
$ dos2unix windows.sh
dos2unix: converting file windows.sh to Unix format ...

# 转换为 dos 格式
$ unix2dos linux.sh
unix2dos: converting file linux.sh to DOS format ...

方法三:删除掉回车(\r)符号

# tr 删除 \r 回车符号,^M 终端输入为Ctrl+V和Ctrl+M
$ cat windows.sh | tr -d "^M" > windows2unix.sh

# sed 删除 \r 回车符号,^M 终端输入为Ctrl+V和Ctrl+M
$ sed -i "s/^M//g" windows.sh

方法四:文本编辑器工具转换换行符合(如:atom、notepad++ 等)

下图为 atom 编辑器的修改换行方式: -w761

微信公众号:daodaotest


作者:Jiang LiHeng
原文链接:https://jiangliheng.github.io/2021/05/19/windows-to-linux/
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。


Similar Posts

Comments