免费注册 查看新帖 |

Chinaunix

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

关于shell函数的几个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-02-15 13:55 |只看该作者 |倒序浏览
5可用积分
现在正编点脚本,忽然越编越迷糊。

在用到shell函数的时候有几点问题请教各位大虾。

1:shell函数只能返回数字的返回值是吗?

2:shell函数及变量有局部性吗?

示例代码如下:

  1. #!/bin/sh

  2. #test


  3. check_outputpath()
  4. {
  5.         _OUTPUTPATH=$1
  6.         _DIRNAME=`dirname $_OUTPUTPATH`
  7.         _BASENAME=`basename $_OUTPUTPATH`
  8.         _CURDIR=`pwd`
  9.         if [ "$_DIRNAME" = "." ]
  10.         then
  11.                  _OUTPUTFILE=$_CURDIR/$_BASENAME
  12.         elif [ "$_DIRNAME" != "." ]
  13.         then
  14.                  _OUTPUTFILE=$_OUTPUTPATH
  15.         fi
  16.         OUTPUTFILE=$_OUTPUTFILE
  17. }

  18. localfunc()
  19. {
  20.         OUTPUTFILE=""
  21.         check_outputpath ./mytest.txt
  22.         echo "In localfunc :"
  23.         echo "$OUTPUTFILE"
  24. }


  25. OUTPUTFILE=""

  26. localfunc ./mytest.txt

  27. echo $OUTPUTFILE

  28. echo "############################"

  29. check_outputpath ./mytest.txt

  30. echo $OUTPUTFILE
复制代码


输出如下:
In localfunc :
/home/harry_Workbench/mytools/mytest.txt
/home/harry_Workbench/mytools/mytest.txt
############################
/home/harry_Workbench/mytools/mytest.txt


我理想的输出是第三行没有。

[ 本帖最后由 cymx2002 于 2008-2-15 14:07 编辑 ]

最佳答案

查看完整内容

1, 不行,自己试试不就知道了。2, 默认是全局的,可以用关键字 "local"

论坛徽章:
0
2 [报告]
发表于 2008-02-15 13:55 |只看该作者
1, 不行,自己试试不就知道了。

2, 默认是全局的,可以用关键字 "local"

       local [option] [name[=value] ...]                                                                                    
              For each argument, a local variable named name is  created,  and                                               
              assigned  value.   The option can be any of the options accepted                                               
              by declare.  When local is used within a function, it causes the                                               
              variable  name  to have a visible scope restricted to that func-                                               
              tion and its children.  With no operands, local writes a list of                                               
              local  variables  to the standard output.  It is an error to use                                               
              local when not within a function.  The return status is 0 unless                                               
              local  is  used outside a function, an invalid name is supplied,                                               
              or name is a readonly variable.         

论坛徽章:
0
3 [报告]
发表于 2008-02-15 14:09 |只看该作者
你在函数localfunc里有了
echo $OUTPUTFILE
在外面还有句
echo $OUTPUTFILE
当然输出2遍了

论坛徽章:
0
4 [报告]
发表于 2008-02-15 14:09 |只看该作者
localfunc()
{
        OUTPUTFILE=""
        check_outputpath ./mytest.txt
        echo "In localfunc :"
        echo "$OUTPUTFILE"  ------这行去掉不就可以了吗?
}

论坛徽章:
0
5 [报告]
发表于 2008-02-15 14:14 |只看该作者
你们没有理解我的意思,十分遗憾,请你们仔细一点。

所答非所问。

论坛徽章:
0
6 [报告]
发表于 2008-02-15 14:26 |只看该作者
。。。。。。你直接问变量的作用域不就得了,看你题目看得也累

论坛徽章:
0
7 [报告]
发表于 2008-02-15 17:15 |只看该作者
谢谢。问题已经解决了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP