Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ksh和bash脚本之间的差异_Bash_Ksh - Fatal编程技术网

ksh和bash脚本之间的差异

ksh和bash脚本之间的差异,bash,ksh,Bash,Ksh,考虑以下脚本(用于local_var2的算术语法与本例无关): 运行时,它会产生以下输出: before functions: local_var1 = 0, local_var2 = 0 my_func1: local_var1 = one, local_var2 = 1 after my_func1: local_var1 = 0, local_var2 = 0 my_func2: local_var1 = two, local_var2 = 2 after my_func2: local_

考虑以下脚本(用于local_var2的算术语法与本例无关):

运行时,它会产生以下输出:

before functions: local_var1 = 0, local_var2 = 0
my_func1: local_var1 = one, local_var2 = 1
after my_func1: local_var1 = 0, local_var2 = 0
my_func2: local_var1 = two, local_var2 = 2
after my_func2: local_var1 = two, local_var2 = 2
(这不是预期的!)

如果在bash中运行相同的脚本,则输出为:

before functions: local_var1 = 0, local_var2 = 0
my_func1: local_var1 = one, local_var2 = 1
after my_func1: local_var1 = 0, local_var2 = 0
my_func2: local_var1 = two, local_var2 = 2
after my_func2: local_var1 = 0, local_var2 = 0

(这正是人们所期待的!)

这是ksh93的一个奇怪之处

typeset
t将变量的作用域定义为“仅本地”时,只能使用函数定义样式:

function func_name
{
}
func_name()
{
}
不使用函数定义样式:

function func_name
{
}
func_name()
{
}
使用
func_name()
样式,一切都是全局的。因此,
ksh
的行为符合预期

但是在这方面,
bash
显然比
ksh
更理智。因此,当使用
typeset
时,它将两个函数中的变量范围设置为本地变量

ksh文档中的条目说明了函数定义之间的差异:

Q18. What is the difference between function name and name()?

A18. In ksh88 these were the same.  However, the POSIX standard
    choose foo() for functions and defined System V Release 2
    semantics to them so that there are no local variables
    and so that traps are not scoped.  ksh93 keeps the ksh88
    semantics for functions defined as function name, and
    has changed the name() semantics to match the POSIX
    semantics.  Clearly, function name is more useful.

根据中的描述,您很可能正在运行AT&T版本的
ksh
,对于该版本,内置的
字体将变量设置为本地变量,仅在使用
函数
关键字
声明的函数中进行测试并确认输出!不过,除了说ksh很烂之外,我对此没有任何解释;-)使用代码中的
set-vx
(每个函数取决于shell),您将看到变量的内容明显不同于第一次使用第二次调用时的预期内容