【转帖】/bin/sh与/bin/bash的细微区别 上海嵌入式家园 贺工
2011-08-13 01:30
756 查看
SeaRiver 的 Linux sh与bash 微秒差别
在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://www.embedclub.com/
淘宝商城网址:http://embedclub.taobao.com/
定位嵌入式服务提供商,专业嵌入式ARM开发板商城,承接各种嵌入式外包项目,提供嵌入式培训,提供方案设计,项目预研指导,提供技术支持服务,涉及操作系统平台包括Linux、Android、Windows CE 5.0/6.0、Windows Mobile 5.0/6.0等;硬件平台:S3C6410/S3C2440/S3C2410、TI OMAP 3530、PXA270、EP9315、i.MX21、i.MX31等。
在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://www.embedclub.com/
淘宝商城网址:http://embedclub.taobao.com/
定位嵌入式服务提供商,专业嵌入式ARM开发板商城,承接各种嵌入式外包项目,提供嵌入式培训,提供方案设计,项目预研指导,提供技术支持服务,涉及操作系统平台包括Linux、Android、Windows CE 5.0/6.0、Windows Mobile 5.0/6.0等;硬件平台:S3C6410/S3C2440/S3C2410、TI OMAP 3530、PXA270、EP9315、i.MX21、i.MX31等。
相关文章推荐
- 简易JTAG调试小板与JLINK和OpenJTAG的区别 上海嵌入式家园-开发板代理商城 贺工
- OpenJTAG与JLINK有什么区别? 上海嵌入式家园-开发板代理商城 贺工
- 嵌入式开发导读 - 上海嵌入式家园 贺工
- COM在WinCE串口通信中的应用 - 上海嵌入式家园 贺工
- H-JTAG V1.0 Release.EXE新版本的CPU detect步骤 上海嵌入式家园贺工
- RHEL5/Fedora下RPM的使用 - 上海嵌入式家园 贺工
- linux下export无效的原因与对策 - 上海嵌入式家园 贺工
- 基于CE的嵌入式软件开发程序入门 - 上海嵌入式家园 贺工
- 【Freescale i.MX21项目】系列之--Eboot, OAL - 上海嵌入式家园 贺工
- ARM9 S3C2440/mini2440开发板570元套餐与520套餐的区别 嵌入式家园贺工
- rpm包管理详解(结合rpm,rpm2cpio,cpio提取rpm包的特定文件) - 上海嵌入式家园 贺工
- Windows CE 编程的十点忠告 - 上海嵌入式家园 贺工
- 【Freescale i.MX21项目】系列之二-DISPLAY - 上海嵌入式家园 贺工
- 【申嵌视频-mini2440专题篇之ARM微处理器开发基础班】下载地址 - 上海嵌入式家园贺工
- 最全资料mini2440/S3C2440开发板570元套餐与经济型499元套餐的区别 嵌入式家园贺工
- Fedora 9 下tftp的安装与配置步骤 - 上海嵌入式家园 贺工
- RHEL5下TFTP服务器配置步骤 - 上海嵌入式家园-开发板商城 贺工
- 嵌入式学习步骤 上海嵌入式家园-开发板代理商城贺工
- Fedora 9下VMWareTools安装步骤 - 上海嵌入式家园 贺工
- 我是使用笔记本的,请问贺工还有什么配件需要购买吗?如何实现在线仿真调试呢? - 上海嵌入式家园 贺工