Linux和Windows文本文件的行结束标志不同。在Linux中,文本文件用"/n"表示回车换行,而Windows用"/r/n"表示回车换行。有时候在Windows编写shell脚本时需要注意这个,否则shell脚本会报"No such file or directory"或"command not found line x"之类的错误,如果不知晓前因后果,肯定会被这个折腾得相当郁闷。如下所示test.sh

[root@DB-Server myscript]# more test.sh 

. /home/oracle/.bash_profile

echo ' '

date

echo ' '

 

sqlplus test/test @/home/oracle/scripts/test.sql

 

echo ' '

date

echo ' '

执行test.sh脚本过程中报" No such file or directory /home/oracle/.bash_profile" 和"command not found line xx". 如果你从shell脚本语法方面去检查,根本没有任何问题。不知具体原因的肯定相当郁闷。

[oracle@DB-Server myscript]$ ./test.sh

 

: No such file or directory /home/oracle/.bash_profile

 

: command not found line 2: 

 

: command not found line 4: date

 

: command not found line 6: 

 

: command not found line 7: 

如果你用file命令查看test.sh,你会发现文件编码为ASCII,with CRLF line terminators。我们用cat -v test.sh 可以看到^M (\r的十六进制为0D,对应的控制符为^M)。 cat -v可以看到文件中的非打印字符。

[root@DB-Server myscript]# 

[root@DB-Server myscript]# file test.sh 

test.sh: ASCII text, with CRLF line terminators

[root@DB-Server myscript]# cat -v test.sh 

. /home/oracle/.bash_profile^M

echo ' '^M

date^M

echo ' '^M

^M

sqlplus test/test @/home/oracle/scripts/test.sql^M

^M

echo ' '^M

date^M

echo ' '^M

[root@DB-Server myscript]# 

vi命令查看test.sh文件看不到^M, 其实这个跟vi的版本有关系。有些版本vi显示的是行尾为^M(AIX下都是显示^M)。而有些vi打开时可以看到底下有[dos]的格式提示。

我们可以用file命令对比正常的shell脚本的格式和编码。如下所示

[oracle@DB-Server myscript]$ file myshell.sh

 

myshell.sh: Bourne-Again shell script text executable

解决这个问题其实也不难,有很多方式,例如直接使用vi编辑器编写shell脚本;使用cat命令创建shell脚本,然后从Windows文本工具里面拷贝脚本过来;最方便的还是使用dos2unix将DOS格式文本文件转换成Unix格式或Linux格式。

CRLF line terminators导致shell脚本报错:command not found的更多相关文章

  1. CRLF line terminators导致shell脚本报错:command not found --转载

    Linux和Windows文本文件的行结束标志不同.在Linux中,文本文件用"/n"表示回车换行,而Windows用"/r/n"表示回车换行.有时候在Wind ...

  2. shell脚本报错:"[: =: unary operator expected"

    shell脚本报错:"[: =: unary operator expected" 在匹配字符串相等时,我用了类似这样的语句: if [ $STATUS == "OK&q ...

  3. shell脚本报错:-bash: xxx: /bin/bash^M: bad interpreter: No such file or directory

    当我们把文件从windows系统中编辑的文件拷贝到linux系统中,如果我们执行文件会保存如下的错: shell脚本报错:-bash: xxx: /bin/bash^M: bad interprete ...

  4. shell脚本报错:-bash: xxx: /bin/sh^M: bad interpreter: No such file or directory

    今天执行一个shell脚本,然后在执行的时候报错,脚本内容很简单,仅供测试: #!/bin/sh echo "test shell " 具体报错信息如下 [root@localho ...

  5. centos6环境远程执行shell脚本报错not a valid identifier的问题处理

    # 通过jenkins的apache用户rsync同步php代码到远程服务器报错如下: SSH: EXEC: STDOUT/STDERR from command [/bin/sh /usr/loca ...

  6. Shell脚本报错:-bash: ./switch.sh: /bin/bash^M: bad interpreter: No such file or directory

    在学习shell中测试case参数命令代码如下 #!/bin/bash #switch测试 case $1 in     start)         echo 'start'     ;;      ...

  7. Linux shell 脚本报错:/bin/bash^M: bad interpreter: No such file or directory

    今天遇到一个很诡异的问题,一直运行很正常的shell脚本失败了,只是昨天增加了一个参数而已. 报错信息: /bin/bash^M: bad interpreter: No such file or d ...

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

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

  9. Shell脚本报错unary operator expected

    在匹配字符串时用了类似这样的语句 if[ $timeofday = "yes"]; then echo "Good morning" exit 0 报错的原因是 ...

随机推荐

  1. IE 11 如何设置“用户代理字符串”

    "用户代理字符串"这个术语在IE11 之前被叫作“浏览器模式”(Browser Mode),在IE11下面这个特殊的选项被放在F12的“仿真”选项下面了. Figure 1通过按下 ...

  2. Generator库co4.6使用及源码分析

    原文链接 http://www.cnblogs.com/ytu2010dt/p/6043947.html co4.x已经抛弃了原来thunk转而结合promise实现. 一:promise proms ...

  3. C#解决Linq OrderBy() 失效的小技巧

    前言 前几天的一个数据列表中我用了Linq GroupBy 和OrderBy. 排序在本机正常使用,发到测试后排序死活不对,很是郁闷,总以为是程序问题.于是请教了另外一个同事.有了以下的答案. 问题原 ...

  4. iis7.0上发布mvc4.0网站

    步骤如下: 1.右击需要发布的项目,在弹出的菜单中选择“发布...”选项 2.在“发布web”对话框中进行设置,配置文件名称默认为“配置文件1”可以修改为需要的名字,以便识别,也可以不改.发布方法选择 ...

  5. TCP三次握手和四次挥手状态变迁解析

    TCP是TCP/IP的传输层控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 首先需要了解几个名词:tcp标志位,有6种分别为:SYN(synchronous建立联机) .ACK(ackn ...

  6. jQuery-1.9.1源码分析系列(二)jQuery选择器续1

    在分析之前说一点题外话. ownerDocument和 documentElement的区别 ownerDocument是Node对象的一个属性,返回的是某个元素的根节点文档对象:即document对 ...

  7. 20款jQuery 的音频和视频插件

    分享 20 款jQuery的音频和视频插件 Blueimp Gallery: DEMO || DOWNLOAD Blueimp gallery 主要为移动设备而设计,同时也支持桌面浏览器.可定制视频和 ...

  8. 创建ASP.NET Core MVC应用程序(4)-添加CRUD动作方法和视图

    创建ASP.NET Core MVC应用程序(4)-添加CRUD动作方法和视图 创建CRUD动作方法及视图 参照VS自带的基架(Scaffold)系统-MVC Controller with view ...

  9. autocad2008+C#2008开发中设置自动加载dll

    一.复制编译后的dlll路径,比如我的是D:\zjy\cad开发\学习\宗地图\bin\Debug\zd.dll 二.随便找个地方新建一个记事本,在记事本中写入以下内容: (command " ...

  10. SQLServer并发问题,先SELECT后UPDATE,避免并发脏读情况解决

    在SQL Server中,需要对数据操作进行先SELECT 之后UPDATE,对于这样的操作,如果出现高并发,可能导致脏读情况的发生.不能保证数据的同步. 解决方案是在事物中对表进行加更新锁: 事务一 ...