免费注册 查看新帖 |

Chinaunix

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

脚本里不能用built-in shell命令吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-11 09:49 |只看该作者 |倒序浏览

  1. #!/bin/msh
  2. # boaconfg.sh
  3. # run it: sh boaconfig.sh

  4. #
  5. # download files
  6. #
  7. /bin/cd /tmp
  8. /bin/tftp -r boa.conf -g 192.168.10.226
  9. /bin/tftp -r index.html -g 192.168.10.226

  10. #
  11. # setup for boa server
  12. #
  13. mkdir /etc/boa

  14. #echo "cp /tmp/boa.conf /etc/boa/"
  15. cp /tmp/boa.conf /etc/boa/

  16. mkdir -p /var/log/boa

  17. mkdir /home/www

  18. cp /tmp/index.html /home/www/

复制代码


执行后的错误如下:

  1. root:/tmp> sh -n boaconfg.sh
  2. root:/tmp> sh -x boaconfg.sh
  3. +
  4. : not found
  5. +/bin/cd /tmp
  6. /bin/cd: not found
  7. +/bin/tftp -r boa.conf -g 192.168.10.226
  8. +/bin/tftp -r index.html -g 192.168.10.226
  9. +
  10. : not found
  11. +mkdir /etc/boa
  12. ': File existscreate directory '/etc/boa
  13. +
  14. : not found
  15. +cp /tmp/boa.conf /etc/boa/
  16. +
  17. : not found
  18. +mkdir -p /var/log/boa
  19. +
  20. : not found
  21. +mkdir /home/www
  22. ': File existscreate directory '/home/www
  23. +
  24. : not found
  25. +cp /tmp/index.html /home/www/
  26. +
  27. : not found
  28. +
  29. : not found
  30. +
  31. : not found
复制代码


  1. root:/tmp> help

  2. Built-in commands:
  3. -------------------
  4.         . : break cd continue eval exec exit export help login newgrp
  5.         read readonly set shift times trap umask wait
复制代码


自己编写的脚本里不能有SHELL内置命令吗?
我该怎么去写呢?

论坛徽章:
0
2 [报告]
发表于 2008-06-11 09:54 |只看该作者
cd命令是built-in直接用就行了
/bin下面根本就没有cd

论坛徽章:
0
3 [报告]
发表于 2008-06-11 10:06 |只看该作者
  1. #!/bin/msh
  2. # boaconfg.sh
  3. # run it: sh boaconfig.sh

  4. #
  5. # download files
  6. #
  7. cd /tmp
  8. /bin/tftp -r boa.conf -g 192.168.10.226
  9. /bin/tftp -r index.html -g 192.168.10.226

  10. #
  11. # setup for boa server
  12. #
  13. /bin/mkdir /etc/boa

  14. #echo "cp /tmp/boa.conf /etc/boa/"
  15. /bin/cp /tmp/boa.conf /etc/boa/

  16. /bin/mkdir -p /var/log/boa

  17. /bin/mkdir /home/www

  18. /bin/cp /tmp/index.html /home/www/
复制代码


我改成以上这样,结果还是错误:出现Not found等。而且除了TFTP执行成功外,其余的都失败了。

请问正确的写法该是怎么写的?

比如在用mkdir xxx时,我如何在脚本里用if来判断xxx是否已存在?

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
4 [报告]
发表于 2008-06-11 10:36 |只看该作者
which mkdir
which cp
看看命令的路径对不对。

论坛徽章:
0
5 [报告]
发表于 2008-06-11 11:11 |只看该作者
  1. root:/tmp> which mkdir
  2. /bin/mkdir
  3. root:/tmp> which cp
  4. /bin/cp
复制代码


我的文件系统里只有/bin目录下有命令,其他的像/sbin和/usr/bin的都是指向/bin的

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
6 [报告]
发表于 2008-06-11 11:16 |只看该作者
除了上述原因, 不会跟跟楼主用metashell 有关吧!

论坛徽章:
0
7 [报告]
发表于 2008-06-11 11:26 |只看该作者
原帖由 寂寞烈火 于 2008-6-11 11:16 发表
除了上述原因, 不会跟跟楼主用metashell 有关吧!


你指跟msh有关?

请问我写的这个简单的脚本是否有什么错误?

sh -n boaconfg.sh是没有任何问题的!就是执行的时候出错!

论坛徽章:
0
8 [报告]
发表于 2008-06-11 14:00 |只看该作者
我顶你个肺!!!

论坛徽章:
0
9 [报告]
发表于 2008-06-11 16:13 |只看该作者
刚才试验了下,发现新现象了,大家帮忙看下。


  1. #!/bin/sh
  2. echo "tt.sh ..."
  3. pwd
  4. cd /tmp
  5. date > ttime
  6. cat ttime
复制代码


以上脚本代码test.sh

1)如果在本地PC创建,并tftp下载到开发板上,
然后执行脚本sh test.sh
会出现错误:

  1. root:/tmp> sh test.sh
  2. test.sh ...
  3. : not found
  4. : bad directory
  5. root:/tmp>
复制代码


2)如果在开发板上创建test.sh
然后执行脚本sh test.sh
则成功:

  1. root:/tmp> sh test.sh
  2. test.sh ...
  3. /tmp
  4. Mon Jan  1 00:11:42 UTC 2007
  5. root:/tmp>
复制代码

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
10 [报告]
发表于 2008-06-11 16:47 |只看该作者
dos2unix ur-script
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP