0.前言

通常我们在编辑 Linux 服务器上的文件时,直接在 Linux 环境比较麻烦(当然熟练使用 VIM 的程序员除外哈哈),有时我们会使用 Windows 将文件编辑好再上传到服务器端,我用的是 Sublime text 。

1.问题描述与记录

编辑完脚本上传到服务器运行时,会出现语法错误,以下面的一小段脚本为例

#!/bin/bash
echo "updatedb..."
sudo updatedb BASE_PATH=$(dirname $(locate $0))
echo ${BASE_PATH} if [ $BASE_PATH == "TODO" ]
then
echo "please modify this script with the base path of your bundler installation";
exit;
fi EXTRACT_FOCAL=${BASE_PATH}/bin/extract_focal.pl
echo ${EXTRACT_FOCAL} echo "[- Done -]"
cv@cv: ~/bundler$ bash runbundler.sh
runbundler.sh: line 4: $'\r': command not found
runbundler.sh: line 7: $'\r': command not found
runbundler.sh: line 13: $'\r': command not found
runbundler.sh: line 16: $'\r': command not found
runbundler.sh: line 17: syntax error: unexpected end of file

2. 问题分析与解决

这里显示的两个问题都是因为我们的.sh文件为dos格式,在 dos/windows 系统中按一次回车键实际上输入的是CRLF ,即回车+换行。

而 Linux 系统一般只能执行 unix 格式的脚本,在 Linux/Unix 系统中按一次回车键实际上输入的是 LF ,即只有换行。

所以这里在 Windows 系统编辑的 sh 脚本文件每行都多了一个回车,当在 Linux 系统中运行时就会报错提示找不到相关命令。

我们可以查看该脚本的格式,在命令行使用 vim 打开脚本, ESC 进入命令输入模式。

输入 :set ff ,查看输出结果,比如我得到的是 fileformat=dos

此时我们可以使用:set ff=unix将 dos 格式更改为 unix 格式。

fffileformat的缩写,因此也可以输入 :set fileformat=unix

这时 :set ff 查看会得到修改后的结果 fileformat=unix

然后再执行脚本就不会出现上面的错误了!

(全文完)


Windows登录服务器CLI运行脚本出现 syntax error: unexpected end of file 错误的解决的更多相关文章

  1. Shell脚本:“syntax error:unexpected end of file”

    这种错误只能说是坑,如果没有见到过,很可能就要摔里头.解决问题是重要的,但弄明白问题的来源,往往更为重要. 所以要先扯一下,换行和回车的历史遗留问题. 在计算机出现之前,有个玩意叫电传打字机.每秒钟可 ...

  2. Parse error: syntax error, unexpected end of file in * 的解决办法

    这个原因很简单,就是你的php语法错误. 在你的php代码种出现了<?  ?>  标准的是<?php ?>

  3. SHELL syntax error:unexpected end of file 提示错误

    SHELL syntax error:unexpected end of file 提示错误 if [ -n "$1" ] then " else " fi e ...

  4. 执行shell脚本报错:syntax error near unexpected token `或syntax error: unexpected end of file

    引起此问题最可能的原因是: 在windows下编写的文件上传到linux执行. 我是在notepad++上编写的代码,之后上传到linux执行,报此错误.仔细检查,语法方面没有错误.上网查了一下,发现 ...

  5. win-Linux文件脚本迁移过程中的问题 syntax error: unexpected end of file

    问题:  在win下写好的shell脚本,放到Linux上测试sh -n报错如下 ORA_check.sh: line 251: syntax error: unexpected end of fil ...

  6. shell 报错:syntax error: unexpected end of file

    有时执行脚本时会报错: [root@host1 shell]# sh -x test.sh + $'\r' : command not found test.: syntax error: unexp ...

  7. 【问题解决】syntax error: unexpected end of file或-bash: ./full_build.sh: /bin/bash^M: bad interpreter: No

    在阅读的过程中有不论什么问题,欢迎一起交流 邮箱:1494713801@qq.com    QQ:1494713801 运行一个脚本full_build.sh 时, 一直是提示我: -bash: ./ ...

  8. 报错解决——linux下执行sh出现异常"syntax error: unexpected end of file"

    有时我们在linux下执行一个sh文件,会报错“SYNTAX ERROR:UNEXPECTED END OF FILE”,这个现象主要是工作的系统环境改变造成的. 若最初脚本中是在windows下,使 ...

  9. shell:syntax error:unexpected end of file/Starting proxy www-balancer: cannot bind socket--转载

    src:http://www.2cto.com/os/201308/238962.html   执行某bash脚本是发生: syntax error: unexpected end of file 主 ...

随机推荐

  1. JMeter线程组编辑区揭秘

    线程组编辑区如下: 有点复杂,但是慢慢看下来,还是比较容易理解. Name 带有业务含义的名字. Comments 线程组的备注说明. Action to be taken after a Sampl ...

  2. JDBC_05_ResorceBundle(资源绑定器) 绑定配置文件

    ResorceBundle(资源绑定器) 绑定配置文件 jdbc.proprtise 需要在src目录下新建一个文件夹然后将jdbc.proprtise放在文件中然后右键该文件夹选择 Rebuild ...

  3. 图解高性能网络架构:Reactor 和 Proactor

    小林,来了. 这次就来图解 Reactor 和 Proactor 这两个高性能网络模式. 别小看这两个东西,特别是 Reactor 模式,市面上常见的开源软件很多都采用了这个方案,比如 Redis.N ...

  4. 1014 Waiting in Line

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...

  5. 【C++】从零开始,只使用FFmpeg,Win32 API,实现一个播放器(一)

    前言 起初只是想做一个直接读取视频文件然后播放字符动画的程序.我的设想很简单,只要有现成的库,帮我把视频文件解析成一帧一帧的原始画面信息,那么我只需要读取里面的每一个像素的RGB数值,计算出亮度,然后 ...

  6. Spring Security极简入门三部曲(中篇)

    目录 Spring Security极简入门三部曲(中篇) 验证流程 Authentication接口 过滤器链 AuthenticationProvider接口: demo时刻 代码讲解 小结 Sp ...

  7. Typora+PicGo+cos图床打造开发者文档神器

    一.Typora简介 markdown简单.高效的语法,被每一个开发者所喜爱.Typora又是一款简约.强悍的实时渲染markdown编辑器.本文将介绍Typora搭配PicGo与腾讯cos对象存储( ...

  8. Windows Server系统加固

    目录 账户管理.认证授权 管理账户 管理口令 授权 审核策略 IP协议安全配置 设备其他配置操作 账户管理.认证授权 管理账户 说明: 对于管理员账号,要求更改缺省账户名称,并且禁用 guest (来 ...

  9. ES常用查询语法

    1.query string search GET /index/type/_search?q=name:zhangsan&sort=age:desc 2.query DSL(Domain S ...

  10. Spring MVC工作原理及源码解析(二)DispatcherServlet实现原理及源码解析

    1.DispatcherServlet 处理流程 从上一篇文章中Spring MVC原理图中我们可以看出:DispatcherServlet 在 Spring MVC框架 中处于核心位置,它负责协调和 ...