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”的更多相关文章

  1. 使用cygwin出现syntax error near unexpected token'$'do\r

    直接从csdn复制粘贴的.sh代码,放到cygwin下运行sh的时候出错syntax error near unexpected token'$'do\r 解决方法: 1.下载notepad++ 2. ...

  2. shell编程报错:“syntax error near unexpected token `”

    今天写了个shell脚本,在自己机器上运行正常,给同事,运行报错syntax error near unexpected token `,左看右看shell脚本没有问题,没有办法google搜索,发现 ...

  3. 执行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 ...

  4. linux后台执行./run.py提示python syntax error near unexpected token `('

    python脚本中的#!/usr/bin/python     估计有不少人注意过一些python脚本开头有这么行东东: #!/usr/bin/python 它是用来干嘛的?貌似没有它对脚本功能也没啥 ...

  5. -bash:syntax error near unexpected token '('

    在Xshell5中编写int main(int argc,char** argv)时, 出现-bash:syntax error near unexpected token '('  : 可是我是按照 ...

  6. Linux 中出现的-bash: syntax error near unexpected token `

    版权声明:本文为博主原创文章,未经博主允许不得转载. 在Linux 5中导入数据时,出现下面的错误. -bash: syntax error near unexpected token `(' 检查了 ...

  7. libtool: syntax error near unexpected token `]*

    ../libtool: line 543: syntax error near unexpected token `]*' ../libtool: line 543: `      *[\[\~\#\ ...

  8. shell脚本启动语法错误syntax error near unexpected token '{

    执行shell脚本时失败,报语法错误,但脚本内容检查正常 原因为该脚本是在非Linux系统下编辑之后放到系统执行的,文件模式类型非Linux系统匹配的模式类型. 查看文件的模式类型 显示文件的模式类型 ...

  9. syntax error near unexpected token `then'

    #!/bin/bashclearfunction test{   if[$1 -eq "root"]&&[$2 -eq "123456"]    ...

随机推荐

  1. 读loadBalance技术的一些笔记

    以前知道loadbalance的原理,但是仅仅是浅浅的了解过,今天看了一篇 10多年前 一位大神级别人物 写的文章 顿时学习了 http://www.linuxvirtualserver.org/zh ...

  2. 安装linux系统并配置那点事

    安装完成后,将看到如下控制台: 输入以上安装信息中所填写的用户名(user),随后输入密码(user),即可登录 Ubuntu. 随时可使用 Ctrl + Alt 快捷键可离开虚拟机. 3 配置 Ub ...

  3. [Angular 2] Move and Delete Angular 2 Components After Creation

    After the original order is set, you can still leverage methods on the Angular 2 ViewContainer to re ...

  4. ccConfig(设置一些底层接口状态:是否支持动作叠加 设置fps更新间隔和位置 是否画边框等。。)

    #ifndef __CCCONFIG_H__ #define __CCCONFIG_H__ #include "platform/CCPlatformConfig.h" /** @ ...

  5. mysql优化之连接优化(open-files-limit与table_open_cache)

    MySQL打开的文件描述符限制 Can't open file: '.\test\mytable.frm' (errno: 24) OS error code : Too many open file ...

  6. Linux文件空洞与稀疏文件 转

      1.Linux文件空洞与稀疏文件 2.文件系统数据存储 3.文件系统调试   文件空洞   在UNIX文件操作中,文件位移量可以大于文件的当前长度在这种情况下,对该文件的下一次写将延长该文件,并在 ...

  7. 性能监视器(SSAS)

    使用性能监视器,您可以通过性能计数器监视 Microsoft SQL Server Analysis Services (SSAS) 实例的性能. 性能监视器是用于跟踪资源使用情况的 Microsof ...

  8. Booting ARM Linux

    来源:linux-2.6.30.4/Documentation/arm/Booting ARM Linux Booting ARM Linux            ================= ...

  9. LeetCode37 Sudoku Solver

    题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...

  10. LeetCode27 Remove Element

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...