修复Mac下执行sh脚本报错Command Not Found

情景回顾

在win10系统里写了一个自动部署调试的sh脚本,拷贝到mac的hugo目录下,试图运行一下,发现报command not found的错误,即便通过chmod增大权限也无济于事:

$:hugo root# chmod -R 777 *
$:hugo root# ./server.sh
: command not found: hugo server
$:hugo root# sudo chmod +x ./server.sh
$:hugo root# ./server.sh
: command not found: hugo server

问题原因

mac下执行.sh脚本错误很多情况只是.sh脚本在windows系统与Unix不同系统的编码格式引起的。

解决方案

转行文件的编码格式,具体操作如下:

确保当前用户有足够权限

$:hugo root# chmod a+x server.sh

修改文件格式

(1)使用vi工具

$:hugo root# vi server.sh

(2)利用如下命令查看文件格式

 :set ff
or
 :set fileformat

可以看到如下信息

fileformat=dosfileformat=unix

(3) 利用如下命令修改文件格式

:set ff=unix
or
:set fileformat=unix

接下来,键入:wq来存盘退出。

重新执行脚本

$:hugo root# ./server.sh
                   | EN
+------------------+-----+
  Pages            | 898
  Paginator pages  |   2
  Non-page files   |   0
  Static files     |  45
  Processed images |   0
  Aliases          |   1
  Sitemaps         |   1
  Cleaned          |   0

Total in 696 ms

收工。