shell - KornShell - 全局设置 "-x"(调试)标志?

标签 shell debugging scripting ksh script-debugging

有没有办法在 KornShell (ksh) 脚本上全局设置 Debug模式 ( set -x )?目前,我似乎做了以下事情:

a(){
   set -x
   #commands
}

b(){
   set -x
   #more commands
}

set-x 
a
#commands
b
我真的很想只需要调用set -x在一处指挥。
注:这一切都在 AIX 上的 KSH88 中。
例子:
#!/bin/ksh
set -x

a(){
   echo "This is A!"
}

b(){
   echo "This is B!"
}

a
echo "Outside"
b
dev2:/home/me-> ./testSetX
+ a
This is A!
+ echo Outside
Outside
+ b
This is B!
dev2:/home/me->

最佳答案

这是 HP-UX 机器上的 ksh88:

me@host ..dev/
$ cat ./test/verbose
#!/bin/ksh
set -x

hello() {
  print $1
}

hello kapow!
exit

me@host..dev/
$ ./test/verbose    
+ hello kapow!
+ print kapow!
kapow!
+ exit

看起来确实很好用。我验证了它也可以在第一次函数调用之前的任何地方使用“set -x”。

我搬到了一个AIX系统,遇到了你描述的问题。当函数定义为 function a {a() {在 AIX ksh88 中,set -x似乎没有延续到函数局部作用域。在同一个 AIX 机器上切换到 ksh93,使用新 function a { 声明的函数语法也不带有外部 set -x进入内部范围。但是,ksh93 的行为类似于过去的 POSIX sh(以及其他平台上的 ksh88),带有 set -x当函数在旧a(){ 中定义时通过到函数方法。这可能是由于 ksh93 中的向后兼容性,当以旧方式定义函数时,它会尝试模拟旧行为。

因此,为了调试目的,您可以暂时将解释器切换到 ksh93,如果您不喜欢更长的数组、关联数组、浮点数学、命名空间支持和大约 10 倍的改进,然后切换回 ksh88 ksh93带来的执行速度。 ;) 因为在 AIX 上使用 ksh88 看起来答案是“不,你不能那样做”。 :(

关于shell - KornShell - 全局设置 "-x"(调试)标志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2273474/

相关文章:

linux - Linux 中的 atq 作业数量有限制吗?

c - && 和 || 的区别在 c 和 unix shell 中?

linux - Grep:列出 'per-file' 结果

android - 在调试期间防止 ApplicationNotResponding

MySQL C pdb 文件

windows - 解析和重命名文本文件

linux - 在 bash 脚本中一致运行

linux - 当需要用户输入时,如何在启动时自动运行应用程序?

linux - 有人能解释一下为什么 awk 的 sub()/gsub() 是这样工作的吗?

c++ - Visual Studio - 如何在显示消息框后捕获调试(中断)