免费注册 查看新帖 |

Chinaunix

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

求助 ~~~ shell脚本 函数调用的问题 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2008-12-18 10:15 |显示全部楼层
abc 2
if [[ $? -eq 333 ]];then
   echo "test is success"
fi

为什么一定要放在[] 里面呢?
我在之前说的那样不好吗?
调用完立马用的话,$?就是返回值;
如果要用不止一次,或不是马上用,用个东东记住它吧
PING_STATUS $IP
return_v=$?

[ 本帖最后由 haimming 于 2008-12-18 11:53 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-12-18 11:00 |显示全部楼层
if [ `PING_STATUS $IP` = 0 ];
用这种方法判断不行

  1. abc(){
  2. if [[ $1 = 1 ]]
  3. then
  4.    return 22
  5. else
  6.     return 33
  7. fi
  8. }

  9. abc 1
  10. echo $?
  11. abc 2
  12. echo $?

  13. if [[ `abc 1` = "22" ]]
  14. then
  15. echo 22
  16. else
  17. echo 33
  18. fi

  19. echo `abc 1`


  20. abc 1
  21. a=$?
  22. if [[ $a = 22 ]]
  23. then
  24. echo 22
  25. else
  26. echo 33
  27. fi
复制代码

[rockfall@localhost mytest]$ ./kkkkkkkk
22
33
33

22
[rockfall@localhost mytest]$

[ 本帖最后由 haimming 于 2008-12-18 11:34 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2008-12-18 11:21 |显示全部楼层
if [ `PING_STATUS $IP` = 0 ]
这用用,直接给你报错
line 18: [: -eq: unary operator expected
因为是空的,echo `PING_STATUS $IP` 就知道,`PING_STATUS $IP`并不是返回值

论坛徽章:
0
4 [报告]
发表于 2008-12-18 12:03 |显示全部楼层
255 以内可以

原帖由 ynchnluiti 于 2008-12-18 11:52 发表
最大值为255


#!/bin/sh
function abc()
{
    if [ $1 -eq 1 ];then
        return 2
    else
        return 255
        fi
}
abc 2
if [[ $? -eq 255 ]];then
echo "test is success"
fi ...

[ 本帖最后由 haimming 于 2008-12-18 12:06 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-12-18 12:13 |显示全部楼层
如果想返回大于255的值,参如下
The 0 - 255 range for function return values is a severe limitation. Global variables and other
workarounds are often problematic. An alternative method for a function to communicate a value
back to the main body of the script is to have the function write to stdout (usually with echo) the
"return value," and assign this to a variable.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP