- Linux、Shell
- Linux下编写deploy.sh文件,内容是用docker部署微服务
- 执行脚本:出错,错误信息如下
[root@coder-oldgeek]# sh deploy.sh No such command: stop Commands: build Build or rebuild services config Validate and view the Compose file create Create services down Stop and remove containers, networks, images, and volumes events Receive real time events from containers exec Execute a command in a running container help Get help on a command images List images kill Kill containers logs View output from containers pause Pause services port Print the public port for a port binding ps List containers pull Pull service images push Push service images restart Restart services rm Remove stopped containers run Run a one-off command scale Set number of containers for a service start Start services stop Stop services top Display the running processes unpause Unpause services up Create and start containers version Show version information and quit ^CRemoves stopped service containers.问题定位 查看文件内容
-
查看: sh文件内容
cat deploy.sh
-
文件内容
#!/bin/bash docker-compose stop docker-compose rm -f docker rmi oldgeek-springcloud-examples docker build -t oldgeek-springcloud-examples . docker-compose up -d docker-compose ps
-
-
看sh内容,没啥问题
- 经验分析:这种多半是因为文本编码导致
-
查看文件信息:file命令
[root@coder-oldgeek]# file deploy.sh deploy.sh: Bourne-Again shell script, ASCII text executable, with CRLF line terminators
-
看上面信息【with CRLF line terminators】,大概能定位到问题
-
windows下的文本:默认换行符是,称为CR与LF两个符号(rn);
-
Linux下的问题本:LF结尾(n)
-
-
【注意点】在windows下编辑的shell脚本只看文件内容是没问题,但一旦到linux下可能就运行挂了
-
查找换行符号:find命令
find . -type f -name '*.sh' | xargs file |grep CRLF
-
替换换行符号
find . -name '*.sh' | xargs -I {} perl -pi -e 's/r//g' {} -
查看有问题文件是否去掉DOS格式下换行符号
[root@coder-oldgeek]# file deploy.sh deploy.sh: Bourne-Again shell script, ASCII text executable
- 发现:with CRLF line terminators已经消失了
-
继续执行脚本deploy.sh,一切正常



