Windows登录服务器CLI运行脚本出现 syntax error: unexpected end of file 错误的解决
2024-09-01 15:40:21
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 格式。
ff
是fileformat
的缩写,因此也可以输入 :set fileformat=unix
。
这时 :set ff
查看会得到修改后的结果 fileformat=unix
。
然后再执行脚本就不会出现上面的错误了!
(全文完)
最新文章
- [No00006A]Js的addEventListener()及attachEvent()区别分析【js中的事件监听】
- DBCC SHOW_STATISTICS 查看统计信息
- 装完Centos7提示Initial setup of CentOS Linux 7 (core)(转载)
- 【转载】C++中public,protected,private访问
- Mybatis知识点总结
- Erwin 生成 mysql 带注释(comment )的脚本
- Mac 加密软件
- 暑假集训(4)第三弹 -----递推(Hdu1799)
- openCV 2.4.13 iOS background_segm.hpp 'list' file not found
- linux下实现自己的shell解释器
- 信息处理,分而治之-- ESFramework 使用技巧
- Java泛型范例
- P1251 餐巾计划问题
- scrapy中的response
- C++解析头文件-Qt自动生成信号声明
- Django 笔记分享
- {";error";:";Content-Type header [application/x-www-form-urlencoded] is not supported";,";status";:406}
- [Codeforces896C] Willem, Chtholly and Seniorious (ODT-珂朵莉树)
- Java 缓存技术之 ehcache
- SQL Server分页进化
热门文章
- JavaScript设计模式(一):单例模式
- 四两拨千斤——你不知道的VScode编码TypeScript的技巧
- Java设计模式(一):设计模式概述、UML图、设计原则
- OpenCV 之 空间刚体变换
- shellcode隐写到像素RGB免杀上线到CS
- 码农飞升记-03-OpenJDK是什么?
- The content of element type ";web-app"; must match ";(icon?,display- name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,servlet- mapping*,session-config?
- Servlet三大域对象
- layui中流加载layui.flow
- python爬虫——《瓜子网》的广州二手车市场信息