我们经常会用如下代码在 Java 里调用系统命令:
Process proc =Runtime.getRuntime().exec("sh xxx.sh");
但是,今天为了在调试模式下运行脚本,加了 -x
参数,发现没有效果,得到的脚本的输出流与没有 -x
参数一样。直接在shell
里执行sh -x xxx.sh
,是能出现调试效果的。
但在 jvm 里面,就无效了。另外测试了 -n
和 -v
参数, -n
有效,-v
无效。
请问这是什么原因呢?
参考资料:
[dmtsai@study ~]$ sh [-nvx] scripts.sh
选项与参数:
-n :不要执行 script,仅查询语法的问题;
-v :再执行 sccript 前,先将 scripts 的内容输出到屏幕上;
-x :将使用到的 script 内容显示到屏幕上,这是很有用的参数!
范例一:测试 dir_perm.sh 有无语法的问题?
[dmtsai@study ~]$ sh -n dir_perm.sh
# 若语法没有问题,则不会显示任何信息!
范例二:将 show_animal.sh 的执行过程全部列出来~
[dmtsai@study ~]$ sh -x show_animal.sh
+ PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/root/bin
+ export PATH
+ for animal in dog cat elephant
+ echo 'There are dogs.... '
There are dogs....
+ for animal in dog cat elephant
+ echo 'There are cats.... '
There are cats....
+ for animal in dog cat elephant
+ echo 'There are elephants.... '
There are elephants....
隔壁的帖子也是楼主发的吧
四楼说的很清楚了,
-x
的输出是到stderr
的,用process.getErrorStream()
拿到的流可以读到