在shell脚本的开头往往有一句话来定义使用哪种sh解释器来解释脚本。
目前研发送测的shell脚本中主要有以下两种方式:
(1) #!/bin/sh
(2) #!/bin/bash
在这里求教同福客栈的各位大侠们一个问题:
以上两种方式有什么区别?对于脚本的实际运行会产生什么不同的影响吗?

脚本test.sh内容:
#!/bin/sh
source pcy.sh #pcy.sh并不存在
echo hello
执行./test.sh,屏幕输出为:
./test.sh: line 2: pcy.sh: No such file or directory
由此可见,在#!/bin/sh的情况下,source不成功,不会运行source后面的代码。
修改test.sh脚本的第一行,变为#!/bin/bash,再次执行./test.sh,屏幕输出为:
./test.sh: line 2: pcy.sh: No such file or directory
hello
由此可见,在#!/bin/bash的情况下,虽然source不成功,但是还是运行了source后面的echo语句。
但是紧接着我又试着运行了一下sh ./test.sh,这次屏幕输出为:
./test.sh: line 2: pcy.sh: No such file or directory
表示虽然脚本中指定了#!/bin/bash,但是如果使用sh 方式运行,如果source不成功,也不会运行source后面的代码。

为什么会有这样的区别呢?

junru同学作了解释

1. sh一般设成bash的软链
[work@zjm-testing-app46 cy]$ ll /bin/sh
lrwxrwxrwx 1 root root 4 Nov 13 2006 /bin/sh -> bash
2. 在一般的linux系统当中(如redhat),使用sh调用执行脚本相当于打开了bash的POSIX标准模式
3. 也就是说 /bin/sh 相当于 /bin/bash --posix

所以,sh跟bash的区别,实际上就是bash有没有开启posix模式的区别

so,可以预想的是,如果第一行写成 #!/bin/bash --posix,那么脚本执行效果跟#!/bin/sh是一样的(遵循posix的特定规范,有可能就包括这样的规范:“当某行代码出错时,不继续往下解释”)

例如:
[root@localhost yuhj]# head -n1 x.sh 
#!/bin/sh
[root@localhost yuhj]# ./x.sh

./x.sh: line 8: syntax error near unexpected token `<'
./x.sh: line 8: ` while read line; do { echo $line;((Lines++)); } ; done < <(route -n)'
[root@localhost yuhj]#

[root@localhost yuhj]# head -n1 x.sh 
#!/bin/bash
[root@localhost yuhj]#./x.sh

Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.202.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
0.0.0.0 192.168.202.2 0.0.0.0 UG 0 0 0 eth0
Number of lines read = 4
[root@localhost yuhj]#

[root@localhost yuhj]# head -n1 x.sh 
#!/bin/bash --posix
[root@localhost yuhj]# 
[root@localhost yuhj]# ./x.sh

./x.sh: line 8: syntax error near unexpected token `<'
./x.sh: line 8: ` while read line; do { echo $line;((Lines++)); } ; done < <(route -n)'

[root@localhost yuhj]# whereis sh bash
sh: /bin/sh /usr/share/man/man1/sh.1.gz /usr/share/man/man1p/sh.1p.gz
bash: /bin/bash /usr/share/man/man1/bash.1.gz

[root@localhost yuhj]# ll /bin/sh /bin/bash
-rwxr-xr-x 1 root root 735004 May 25 2008 /bin/bash
lrwxrwxrwx 1 root root 4 Jan 29 00:39 /bin/sh -> bash
[root@localhost yuhj]#

更多:http://hi.baidu.com/aaronike/item/08cfca8ab2ca145d850fabd3

最新文章

  1. Concurrency != Parallelism
  2. JAVA自定义注解
  3. ASP.NET-Web项目运行缓慢-AjaxMin在搞鬼
  4. Redis - hash类型操作
  5. struts2值栈分析
  6. EF中使用SQL函数
  7. iOS 如何做才安全--逆向工程 - Reveal、IDA、Hopper、https抓包 等
  8. [置顶] android AIDL 进程间通信
  9. Git学习笔记(一)创建版本库并添加文件
  10. android 模拟器对应键盘快捷键
  11. bootstrap之daterangepicker---汉化以及扩展
  12. ubuntu输入某个目录测试该目录里.C文件里代码行数shell脚本
  13. MySQL 存储过程的简单使用
  14. 【English】20190429
  15. 一、Java和JavaScript
  16. 一台电脑安装两个JDK
  17. SpringBoot入门篇--关于properties和yml两种配置文件的一些事情
  18. GO语言的进阶之路-Golang字符串处理以及文件操作
  19. SVN被锁定的几种解决方法
  20. Content-Type:几种常用数据编码格式

热门文章

  1. POJ1741--Tree (树的点分治) 求树上距离小于等于k的点对数
  2. (转)三星i9500/Galaxy S4 刷基带教程
  3. Sequence(组合数学,集合不同元素的个数)
  4. parcel write boolean值
  5. 10、Cocos2dx 3.0游戏开发找小三之容器篇:Vector、Map、Value
  6. igmpproxy源码学习——igmpProxyInit()
  7. windows 7 SDK和DDK下载地址
  8. 宙斯是一个完整的Hadoop的作业平台[转]
  9. SharePoint 要一个多行文本类型字段为特殊类型的链接
  10. 解决从github下载web的源代码部署到eclipse的问题