请教 Java Runtime.exec(“sh -x xx.sh”)无效的问题
資深大佬 : duanguyuan 0
我们经常会用如下代码在 Java 里调用系统命令:
Process proc =Runtime.getRuntime().exec("sh xxx.sh");
但是,今天为了在调试模式下运行脚本,加了 -x 参数,发现没有效果,得到的脚本的输出流与没有 -x 参数一样。直接在shell里执行sh -x xxx.sh,是能出现调试效果的。 但在 jvm 里面,就无效了。另外测试了-n和-v参数,-n有效,-v无效。 请问这是什么原因呢?
参考资料:
[[email protected] ~]$ sh [-nvx] scripts.sh 选项与参数: -n :不要执行 script,仅查询语法的问题; -v :再执行 sccript 前,先将 scripts 的内容输出到屏幕上; -x :将使用到的 script 内容显示到屏幕上,这是很有用的参数! 范例一:测试 dir_perm.sh 有无语法的问题? [[email protected] ~]$ sh -n dir_perm.sh # 若语法没有问题,则不会显示任何信息! 范例二:将 show_animal.sh 的执行过程全部列出来~ [[email protected] ~]$ 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....
大佬有話說 (9)