免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3665 | 回复: 5
打印 上一主题 下一主题

[学习分享] ./*.sh和sh *.sh执行结果不同 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-08-14 15:52 |只看该作者 |倒序浏览

测试脚本文件
windeal@ubuntu:~/Windeal/shell$ cat test.sh
#!/bin/bash

echo "hello\n"
echo -e "hello\n"
windeal@ubuntu:~/Windeal/shell$
复制代码
使用./*.sh执行结果为:
windeal@ubuntu:~/Windeal/shell$ ./test.sh
hello\n
hello

windeal@ubuntu:~/Windeal/shell$
复制代码
使用sh *.sh执行结果为
windeal@ubuntu:~/Windeal/shell$ sh test.sh
hello

-e hello

windeal@ubuntu:~/Windeal/shell$
复制代码
另一个错误的例子

判断符 []在sh *.sh方式执行中不能成功
测试脚本如下
windeal@ubuntu:~/Windeal/shell$ cat sh06.sh
#!/bin/bash
#Program
# "test the func of []"
#History:
#2014-08-14        Windeal
#version        1
#All rights reserved;

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please enter (y/n):" yn
[ "$yn" == "Y" -o "$yn" == "y" ] && echo "your input is y" && exit 0
[ "$yn" == "N" -o "$yn" == "n" ] && echo "your input is n" && exit 0
echo "I don't know what you choice\n"
windeal@ubuntu:~/Windeal/shell$
复制代码
使用./*.sh方式执行没问题:
windeal@ubuntu:~/Windeal/shell$ ./sh06.sh
Please enter (y/n):y
your input is y
windeal@ubuntu:~/Windeal/shell$
复制代码
使用sh *.sh执行报错
windeal@ubuntu:~/Windeal/shell$ sh sh06.sh
Please enter (y/n):y
sh06.sh: 12: [: y: unexpected operator
sh06.sh: 13: [: y: unexpected operator
I don't know what you choice

windeal@ubuntu:~/Windeal/shell$
复制代码

论坛徽章:
39
双子座
日期:2014-08-06 17:37:19极客徽章
日期:2016-12-07 14:03:402017金鸡报晓
日期:2017-01-10 15:13:292017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之新疆
日期:2017-03-24 16:36:1915-16赛季CBA联赛之江苏
日期:2017-04-26 17:19:08黑曼巴
日期:2018-03-07 18:56:5615-16赛季CBA联赛之八一
日期:2018-03-09 10:44:1015-16赛季CBA联赛之江苏
日期:2018-03-12 15:12:1915-16赛季CBA联赛之青岛
日期:2018-03-16 09:13:0515-16赛季CBA联赛之山东
日期:2018-04-27 18:23:0515-16赛季CBA联赛之新疆
日期:2018-05-04 11:29:30
2 [报告]
发表于 2014-08-15 09:26 |只看该作者
bash版本的问题,我这环境执行的都是一样的结果
你可以选择换个环境测试一下

论坛徽章:
0
3 [报告]
发表于 2014-08-15 14:07 |只看该作者
sh是用默认的shell执行的,而直接运行是用bash运行的。

0)~/img$ ls -l $(which sh)
lrwxrwxrwx 1 root root 4  3月 30  2012 /bin/sh -> dash

我系统里默认的sh就是dash而不是bash,所以得出不一样的结果是很正常的。

我们通常写shell都是默认用bash的语法,毕竟最常用,虽然不是最强的。

论坛徽章:
154
2022北京冬奥会纪念版徽章
日期:2015-08-07 17:10:5720周年集字徽章-年
日期:2022-10-26 16:44:2015-16赛季CBA联赛之深圳
日期:2022-11-02 14:02:4515-16赛季CBA联赛之八一
日期:2022-11-28 12:07:4820周年集字徽章-20	
日期:2023-07-19 08:49:4515-16赛季CBA联赛之八一
日期:2023-11-04 19:23:5115-16赛季CBA联赛之广夏
日期:2023-12-13 18:09:34
4 [报告]
发表于 2014-08-17 18:30 |只看该作者
sh就是默认的,也是跨平台的

论坛徽章:
0
5 [报告]
发表于 2014-08-21 09:57 |只看该作者
回复 4# shang2010
sh应该只是一个别名。一些系统中sh使用bash,一些系统中sh使用dash(比如我的Ubuntu)


   

论坛徽章:
0
6 [报告]
发表于 2014-08-21 10:49 |只看该作者
上面已经回答提到了,主要是 shell 不同,支持的语法有点区别。
楼主机器上,sh 应该使用的是 dash.
可以运行下面命令看看:
   #   whereis sh
sh: /bin/sh /bin/sh.distrib /usr/share/man/man1/sh.1.gz
  # ls -la /bin/sh
/bin/sh -> dash


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP