免费注册 查看新帖 |

Chinaunix

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

./*.sh 和 sh *.sh执行的结果不同 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-08-14 15:49 |只看该作者 |倒序浏览
测试脚本文件
  1. windeal@ubuntu:~/Windeal/shell$ cat test.sh
  2. #!/bin/bash

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

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

  3. -e hello

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

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

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

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

论坛徽章:
20
申猴
日期:2013-09-12 19:39:05狮子座
日期:2014-07-20 21:19:51寅虎
日期:2014-08-16 18:37:47水瓶座
日期:2014-10-15 18:58:25天蝎座
日期:2015-01-22 18:19:15NBA常规赛纪念章
日期:2015-05-04 22:32:032015亚冠之胡齐斯坦钢铁
日期:2015-06-03 11:28:502015亚冠之吉达阿赫利
日期:2015-09-19 12:41:47午马
日期:2013-09-18 14:36:40戌狗
日期:2013-09-18 14:44:39处女座
日期:2013-09-24 17:46:41CU十二周年纪念徽章
日期:2013-10-24 15:41:34
2 [报告]
发表于 2014-08-14 20:08 |只看该作者
不明觉厉              

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2014-08-14 21:40 |只看该作者
./*.sh 中echo应该 是调用的/bin/echo
sh *.sh中的echo应该是调用的shell内置的命令
嗯,应该是这样,你照这个方向查一下.

论坛徽章:
7
荣誉会员
日期:2011-11-23 16:44:17水瓶座
日期:2013-08-28 21:20:16丑牛
日期:2013-10-02 21:01:462015年迎新春徽章
日期:2015-03-04 09:54:45操作系统版块每日发帖之星
日期:2016-06-05 06:20:0015-16赛季CBA联赛之吉林
日期:2016-06-20 08:24:0515-16赛季CBA联赛之四川
日期:2016-08-18 15:02:02
4 [报告]
发表于 2014-08-15 11:31 |只看该作者
q1208c 说
  1. sh != bash
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP