Linux下shell不能正常执行

标签

  • 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两个符号(\r\n);

    • 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,一切正常

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小码匠和老码农

喜欢作者

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值