KSH中变量的范围 [英] Scope of variables in KSH

查看:154
本文介绍了KSH中变量的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个示例KornShell函数来拆分String,将其放入数组中,然后打印出值. 代码如下

I have written a sample KornShell function to split a String, put it in an array and then print out the values. The code is as below

#!/usr/bin/ksh

splitString() {

    string="abc@hotmail.com;xyz@gmail.com;uvw@yahoo.com"

    oIFS="$IFS"; 
    IFS=';' 
    set -A str $string
    IFS="$oIFS"
}

splitString
echo "strings count = ${#str[@]}"
echo "first : ${str[0]}";
echo "second: ${str[1]}";
echo "third : ${str[2]}";

现在echo不会打印出数组的值,因此我认为它与定义的数组范围有关.

Now the echo does not print out the values of the array, so I assume it has something to do with the scope of the array defined.

我是Shell脚本的新手,有人可以帮助我理解上面示例中的变量范围吗?

I am new to Shell scripting, can anybody help me out with understanding the scope of variables in the example above?

推荐答案

变量的默认范围是整个脚本.

The default scope of a variable is the whole script.

但是,当您在函数内部声明变量时,该变量将成为声明该函数的函数的局部变量. Ksh具有动态范围,因此该变量也可以在该函数调用的函数中访问声明变量.这在本手册中有关函数的部分中得到了简要记录.请注意,在AT& T ksh(与pdksh和派生类以及bash和zsh的相似功能相反)中,这仅适用于用function关键字定义的函数,不适用于用传统f () { … }语法定义的函数.在AT& T ksh93中,使用传统语法定义的函数中声明的所有变量都是全局变量.

However, when you declare a variable inside a function, the variable becomes local to the function that declares it. Ksh has dynamic scoping, so the variable is also accessible in functions that are invoked by the function that declares the variable. This is tersely documented in the section on functions in the manual. Note that in AT&T ksh (as opposed to pdksh and derivatives, and the similar features of bash and zsh), this only applies to functions defined with the function keyword, not to functions defined with the traditional f () { … } syntax. In AT&T ksh93, all variables declared in functions defined with the traditional syntax are global.

声明变量的主要方法是使用typeset

The main way of declaring a variable is with the typeset builtin. It always makes a variable local (in AT&T ksh, only in functions declared with function). If you assign to a variable without having declared it with typeset, it's global.

ksh文档未指定set -A是将变量设置为局部变量还是全局变量,并且版本不同.在ksh 93u,pdksh或mksh下,该变量是全局变量,您的脚本会打印出该值.您似乎具有ksh88或较旧版本的ksh,其作用域是本地的.我认为在函数外部初始化str会创建一个全局变量,但是我不确定.

The ksh documentation does not specify whether set -A makes a variable local or global, and different versions make it either. Under ksh 93u, pdksh or mksh, the variable is global and your script does print out the value. You appear to have ksh88 or an older version of ksh where the scope is local. I think that initializing str outside the function would create a global variable, but I'm not sure.

请注意,您应该使用局部变量来覆盖IFS的值:保存到另一个变量不仅笨拙,而且还很脆弱,因为如果未设置IFS,则无法正确还原.此外,您应该关闭Globing,因为否则,如果字符串包含Shell Globing字符?*\[,并且其中一个单词恰好与系统上的一个或多个文件匹配,则它将被扩展,例如. set -A $string其中stringa;*的结果将导致str包含当前目录中的文件名列表.

Note that you should use a local variable to override the value of IFS: saving to another variable is not only clumsy, it's also brittle because it doesn't restore IFS properly if it was unset. Furthermore, you should turn off globbing, because otherwise if the string contains shell globbing characters ?*\[ and one of the words happens to match one or more file on your system it will be expanded, e.g. set -A $string where string is a;* will result in str containing the list of file names in the current directory.

set -A str
function splitString {
  typeset IFS=';' globbing=1
  case $- in *f*) globbing=;; esac
  set -f
  set -A str $string
  if [ -n "$globbing" ]; then set +f; fi
}
splitString "$string"

这篇关于KSH中变量的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆