写shell,运行出错:syntax error near unexpected token `$’do\r”
cygwin下面写shell,运行出错:syntax error near unexpected token `$’do\r”
写shell,运行出错:syntax error near unexpected token `$’do\r”
【问题】
在cygwin下,用notepad++写了个简单的shell脚本,内容如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/bin/bash #remove *.bak in subfolder files echo 'remove *.bak' sub_folders=`ls` echo 'sub_folders='$sub_folders for each_item in $sub_folders; do if [ -d $each_item ]; then echo $each_item else echo $each_item "is file ------" fi done exit 0 |
但是运行出错:
|
1 |
line 6: syntax error near unexpected token `$'do\r'' |
【解决过程】
1.网上搜了下,参考:linux shell脚本报错:syntax error near unexpected token `
然后去notepad++中,视图->显示符号->显示所有符号,然后就看到了预料到的,所有的回车换行都是windows的CR LF:

得知是Linux和windows下的回车换行符不兼容的问题。
所以,剩下的就是去将windows下面的CR LF,转换为Linux下面的LF,即可。
此处,不需要再麻烦地去用其他dos2unix等工具了,notepad++就可以帮忙搞定:
编辑->档案格式转换->转换为UNIX格式:

即可将所有的 CR LF,转换为LF:

所以,越加发现notepad++的强大之处了。
然后再运行此shell脚本,即可正常运行了。
【总结】
linux/cygwin下面的shell脚本出现错误:
syntax error near unexpected token `$’do\r”
是由于该脚本文件是在windows环境下创建编辑的,其默认是将所有的回车变成:
CR加上 LF,而Linux/Unix中的回车只是LF,所以导致解释器不认,出现此错误。
解决办法就是,将所有的CRLF转换为LF即可。
转换的方法有多种,比如用dos2unix,或用此处的notepad++帮我们转换,都可以。
写shell,运行出错:syntax error near unexpected token `$’do\r”的更多相关文章
- 使用cygwin出现syntax error near unexpected token'$'do\r
直接从csdn复制粘贴的.sh代码,放到cygwin下运行sh的时候出错syntax error near unexpected token'$'do\r 解决方法: 1.下载notepad++ 2. ...
- shell编程报错:“syntax error near unexpected token `”
今天写了个shell脚本,在自己机器上运行正常,给同事,运行报错syntax error near unexpected token `,左看右看shell脚本没有问题,没有办法google搜索,发现 ...
- 执行shell脚本提示“syntax error near unexpected token for((i=0;i<$length;i++))”
sh脚本例如以下: #!/usr/bin/env bash county="3 4 5 6 7 8 9 10 11 12 16 29 39 44 53 62 72 84 97 115 128 ...
- linux后台执行./run.py提示python syntax error near unexpected token `('
python脚本中的#!/usr/bin/python 估计有不少人注意过一些python脚本开头有这么行东东: #!/usr/bin/python 它是用来干嘛的?貌似没有它对脚本功能也没啥 ...
- -bash:syntax error near unexpected token '('
在Xshell5中编写int main(int argc,char** argv)时, 出现-bash:syntax error near unexpected token '(' : 可是我是按照 ...
- Linux 中出现的-bash: syntax error near unexpected token `
版权声明:本文为博主原创文章,未经博主允许不得转载. 在Linux 5中导入数据时,出现下面的错误. -bash: syntax error near unexpected token `(' 检查了 ...
- libtool: syntax error near unexpected token `]*
../libtool: line 543: syntax error near unexpected token `]*' ../libtool: line 543: ` *[\[\~\#\ ...
- shell脚本启动语法错误syntax error near unexpected token '{
执行shell脚本时失败,报语法错误,但脚本内容检查正常 原因为该脚本是在非Linux系统下编辑之后放到系统执行的,文件模式类型非Linux系统匹配的模式类型. 查看文件的模式类型 显示文件的模式类型 ...
- syntax error near unexpected token `then'
#!/bin/bashclearfunction test{ if[$1 -eq "root"]&&[$2 -eq "123456"] ...
随机推荐
- C++学习笔记之迭代器
模板是的算法独立于存储的数据类型,而迭代器使算法独立于使用的容器类型.理解迭代器是理解STL的关键. 迭代器应该具备的特征: (1)应该能够对迭代器进行解除引用的操作,以便能够访问它引用的值.即如果P ...
- UVALive 4192 Close Enough Computations 水题
Close Enough Computations 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge& ...
- Codeforces Gym 100015G Guessing Game 差分约束
Guessing Game 题目连接: http://codeforces.com/gym/100015/attachments Description Jaehyun has two lists o ...
- MeetMe
- Flex4+Spring3+Hibernate3+BlazeDS整合笔记
普通Java Web工程流行使用ssh框架,而当前台使用Flex制作的时候,后台就不需要用Struts了,通过使用BlazeDS远程方法调用即可. 首先,新建Java Web工程,然后添加Flex项目 ...
- cisco路由基于策略的路由选择
cisco路由基于策略的路由选择 基于策略的路由选择是一种手段,通过它管理员可以在基于目的地的路由选择协议中实现偏离标准路由的路由选择.基于目的地的路由选择协议将根据到一个目的地的最短路径选择路由,基 ...
- Linux服务器集群系统(四)--转
引用地址:http://www.linuxvirtualserver.org/zh/lvs4.html LVS集群的负载调度 章文嵩 (wensong@linux-vs.org) 2002 年 5 月 ...
- redis 简介
Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(diff ...
- 搭建本地Git服务器6步走
1. 在服务器上安装git和ssh 2. 在服务器上新建一个用户,比如就叫git sudo adduser git 3. 在服务器上新建一个目录来放置git仓库 mkdir gitrepo git i ...
- Jqeury获取table当前行与指定列
今天遇到了一个Jqeury获取table当前行与指定列的问题: 大概的实现要求是一个页面中,上面有几个input输入框,下面有一个table,当在输入框中输入内容的时候,点击添加按钮的时候,在下面ta ...