POSIX 兼容的 shell 脚本中是否允许条件函数定义?
Is a conditional function definition allowed in POSIX compliant shell scripts?
在 Bash 和 KornShell (ksh) 中,我看到以下脚本工作正常。
if [ -n "foo" ]
then
foo()
{
echo function foo is defined
}
else
bar()
{
echo function bar is defined
}
fi
foo
bar
它在执行时也会生成预期的输出。
$ bash scr.sh
function foo is defined
scr.sh: line 15: bar: command not found
$ ksh scr.sh
function foo is defined
scr.sh: line 15: bar: not found
我想知道这个脚本是否会 运行 并在任何 POSIX 一致性 shell 上生成这个输出。
我同意你对语法的阅读。函数定义可能出现在 if
语句的主体中,使其执行有条件。
在 Bash 和 KornShell (ksh) 中,我看到以下脚本工作正常。
if [ -n "foo" ]
then
foo()
{
echo function foo is defined
}
else
bar()
{
echo function bar is defined
}
fi
foo
bar
它在执行时也会生成预期的输出。
$ bash scr.sh
function foo is defined
scr.sh: line 15: bar: command not found
$ ksh scr.sh
function foo is defined
scr.sh: line 15: bar: not found
我想知道这个脚本是否会 运行 并在任何 POSIX 一致性 shell 上生成这个输出。
我同意你对语法的阅读。函数定义可能出现在 if
语句的主体中,使其执行有条件。