免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2408 | 回复: 5

[已解决]SHELL 第一行sha-bang (#!) 问题 [复制链接]

论坛徽章:
0
发表于 2012-01-04 21:13 |显示全部楼层
本帖最后由 可可火山 于 2012-01-05 00:10 编辑

这个问题困扰我一段时间了,我是Solaris刚一年左右,平时习惯bash,所以如果默认帐号是ksh的话我都.profile里会加个exec bash进bash。
我写的一些shell脚本在加了sha-bang 第一行#!/usr/bin/sh这种后有些test 语句不能解释。

系统信息和sh、bash信息
  1. [user@workspace (0)]$uname -a
  2. SunOS solarishost 5.10 Generic_144488-17 sun4u sparc SUNW,SPARC-Enterprise Solaris
  3. [user@workspace (0)]$echo $SHELL
  4. bash
  5. [user@workspace (0)]$ls -l /usr/bin/sh
  6. lrwxrwxrwx 1 root root 13 Apr  1  2008 /usr/bin/sh -> ../../sbin/sh
  7. [user@workspace (0)]$ls -l /sbin/sh
  8. -r-xr-xr-x 1 root root 95492 Sep 22  2010 /sbin/sh
  9. [user@workspace (0)]$ls -l /usr/xpg4/bin/sh
  10. -r-xr-xr-x 1 root bin 209288 Mar  9  2011 /usr/xpg4/bin/sh
  11. [user@workspace (0)]$ls -l /usr/bin/bash
  12. -r-xr-xr-x 1 root bin 794124 Feb  2  2011 /usr/bin/bash
复制代码
测试脚本,我的问题是好像solaris上的sh解释器不像Linux/AIX,对于设置SHELL Sha-Bang为sh后,解释 -e 这种文件条件就有问题 (-r -d等其他文件条件还是正常的)。
  1. [user@workspace (0)]$cat shscript.sh
  2. #!/usr/bin/sh

  3. if [ -e tmp.file ]
  4. then
  5.     echo "tmp.file is exist"
  6. else
  7.     echo "tmp.file isn't exist"
  8. fi
  9. [user@workspace (0)]$cat sbinshscript.sh
  10. #!/sbin/sh

  11. if [ -e tmp.file ]
  12. then
  13.     echo "tmp.file is exist"
  14. else
  15.     echo "tmp.file isn't exist"
  16. fi
  17. [user@workspace (0)]$cat shscript_noshabang.sh
  18. if [ -e tmp.file ]
  19. then
  20.     echo "tmp.file is exist"
  21. else
  22.     echo "tmp.file isn't exist"
  23. fi
  24. [user@workspace (0)]$cat bashscript.sh
  25. #!/usr/bin/bash

  26. if [ -e tmp.file ]
  27. then
  28.     echo "tmp.file is exist"
  29. else
  30.     echo "tmp.file isn't exist"
  31. fi
  32. [user@workspace (0)]$./shscript.sh
  33. ./shscript.sh: test: argument expected
  34. [user@workspace (0)]$./sbinshscript.sh
  35. ./sbinshscript.sh: test: argument expected
  36. [user@workspace (0)]$./shscript_noshabang.sh
  37. tmp.file isn't exist
  38. [user@workspace (0)]$./bashscript.sh
  39. tmp.file isn't exist
复制代码
难道这是Solaris上sh的问题?也许这就是一般solaris脚本要不是ksh写的,要不就是bash的。

论坛徽章:
0
发表于 2012-01-04 21:49 |显示全部楼层
进solaris看了下,不管/bin/sh还是/usr/bin/sh都是/sbin/sh的软链接
lrwxrwxrwx 1 root 13 2007-03-05 03:30 /usr/bin/sh -> ../../sbin/sh
lrwxrwxrwx 1 root 13 2007-03-05 03:30 /bin/sh -> ../../sbin/sh

查看了下solaris下的man test,里面有
     -e file  True if file exists. (Not  available in sh.)

所以sh里面test不支持-e参数
还有这么一段话
Using -e option in sh
     Example 6: Using /usr/bin/test for the -e option

     If one really  wants  to  use  the  -e  option  in  sh,  use
     /usr/bin/test, as in the following:

     if [ ! -h $PKG_INSTALL_ROOT$rLink ] && /usr/bin/test -e
     $PKG_INSTALL_ROOT/usr/bin/$rFile ; then
         ln -s $rFile $PKG_INSTALL_ROOT$rLink
     fi

也就是/bin/test是不支持-e参数,但是/usr/bin/test可以支持
  1. $ touch sdfas
  2. $ test -e sdfas && echo 111
  3. test: 需要参数
  4. $ /usr/bin/test -e sdfas && echo 111
  5. 111
复制代码
所以用/usr/bin/test,要用[的话,也许将/bin/test做个软链接到/usr/bin/test可以

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
发表于 2012-01-04 21:57 |显示全部楼层
我一般都是直接#!/bin/bash

论坛徽章:
0
发表于 2012-01-04 23:48 |显示全部楼层
回复 2# yangkyo821

谢谢找来这么全的资料,现在我明白了。
我的 -e不能用 我用 -f / -d 等代替了。

   

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
发表于 2012-01-04 23:52 |显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
13
双鱼座
日期:2013-10-23 09:30:05数据库技术版块每日发帖之星
日期:2016-04-20 06:20:00程序设计版块每日发帖之星
日期:2016-03-09 06:20:002015亚冠之塔什干火车头
日期:2015-11-02 10:07:452015亚冠之德黑兰石油
日期:2015-08-30 10:07:07数据库技术版块每日发帖之星
日期:2015-08-28 06:20:00数据库技术版块每日发帖之星
日期:2015-08-05 06:20:002015年迎新春徽章
日期:2015-03-04 09:57:09辰龙
日期:2014-12-03 14:45:52酉鸡
日期:2014-07-23 09:46:23亥猪
日期:2014-03-13 08:46:22金牛座
日期:2014-02-11 09:36:21
发表于 2012-01-05 09:00 |显示全部楼层
恩,直接/bin/bash然后给执行权限,以前没注意,Ubuntu下 /bin/sh指向的是/bin/dash
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP