{"id":108872,"date":"2020-05-30T10:22:50","date_gmt":"2020-05-30T02:22:50","guid":{"rendered":"http:\/\/4563.org\/?p=108872"},"modified":"2020-05-30T10:22:50","modified_gmt":"2020-05-30T02:22:50","slug":"%e8%af%b7%e6%95%99-java-runtime-execsh-x-xx-sh%e6%97%a0%e6%95%88%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=108872","title":{"rendered":"\u8bf7\u6559 Java Runtime.exec(&#8220;sh -x xx.sh&#8221;)\u65e0\u6548\u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559 Java Runtime.exec(&#8220;sh -x xx.sh&#8221;)\u65e0\u6548\u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : duanguyuan <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6211\u4eec\u7ecf\u5e38\u4f1a\u7528\u5982\u4e0b\u4ee3\u7801\u5728 Java \u91cc\u8c03\u7528\u7cfb\u7edf\u547d\u4ee4\uff1a<\/p>\n<pre><code>Process proc =Runtime.getRuntime().exec(\"sh xxx.sh\"); <\/code><\/pre>\n<p>\u4f46\u662f\uff0c\u4eca\u5929\u4e3a\u4e86\u5728\u8c03\u8bd5\u6a21\u5f0f\u4e0b\u8fd0\u884c\u811a\u672c\uff0c\u52a0\u4e86 <code>-x<\/code> \u53c2\u6570\uff0c\u53d1\u73b0\u6ca1\u6709\u6548\u679c\uff0c\u5f97\u5230\u7684\u811a\u672c\u7684\u8f93\u51fa\u6d41\u4e0e\u6ca1\u6709 <code>-x<\/code> \u53c2\u6570\u4e00\u6837\u3002\u76f4\u63a5\u5728<code>shell<\/code>\u91cc\u6267\u884c<code>sh -x xxx.sh<\/code>\uff0c\u662f\u80fd\u51fa\u73b0\u8c03\u8bd5\u6548\u679c\u7684\u3002 \u4f46\u5728 jvm \u91cc\u9762\uff0c\u5c31\u65e0\u6548\u4e86\u3002\u53e6\u5916\u6d4b\u8bd5\u4e86<code>-n<\/code>\u548c<code>-v<\/code>\u53c2\u6570\uff0c<code>-n<\/code>\u6709\u6548\uff0c<code>-v<\/code>\u65e0\u6548\u3002 \u8bf7\u95ee\u8fd9\u662f\u4ec0\u4e48\u539f\u56e0\u5462\uff1f<\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n<pre><code>[[email&#160;protected] ~]$ sh [-nvx] scripts.sh \u9009\u9879\u4e0e\u53c2\u6570\uff1a -n  \uff1a\u4e0d\u8981\u6267\u884c script\uff0c\u4ec5\u67e5\u8be2\u8bed\u6cd5\u7684\u95ee\u9898\uff1b -v  \uff1a\u518d\u6267\u884c sccript \u524d\uff0c\u5148\u5c06 scripts \u7684\u5185\u5bb9\u8f93\u51fa\u5230\u5c4f\u5e55\u4e0a\uff1b -x  \uff1a\u5c06\u4f7f\u7528\u5230\u7684 script \u5185\u5bb9\u663e\u793a\u5230\u5c4f\u5e55\u4e0a\uff0c\u8fd9\u662f\u5f88\u6709\u7528\u7684\u53c2\u6570\uff01  \u8303\u4f8b\u4e00\uff1a\u6d4b\u8bd5 dir_perm.sh \u6709\u65e0\u8bed\u6cd5\u7684\u95ee\u9898\uff1f [[email&#160;protected] ~]$ sh -n dir_perm.sh  # \u82e5\u8bed\u6cd5\u6ca1\u6709\u95ee\u9898\uff0c\u5219\u4e0d\u4f1a\u663e\u793a\u4efb\u4f55\u4fe1\u606f\uff01  \u8303\u4f8b\u4e8c\uff1a\u5c06 show_animal.sh \u7684\u6267\u884c\u8fc7\u7a0b\u5168\u90e8\u5217\u51fa\u6765\uff5e [[email&#160;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.... <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1810177\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Hieast <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u5199\u8fc7 Java\uff0c\u5355\u5c31\u95ee\u9898\u6765\u770b\u5efa\u8bae\u67e5\u4e00\u4e0b\u6587\u6863\u770b Runtime.getRuntime().exec \u8fd9\u79cd\u65b9\u5f0f\u8fd0\u884c\u65f6\u6807\u51c6\u8f93\u51fa\u8ddf\u5f53\u524d\u8fdb\u7a0b\u662f\u5426\u4e00\u81f4\u3002\u53ef\u4ee5\u5c1d\u8bd5 Process proc =Runtime.getRuntime().exec(&#8220;sh -x xxx.sh &gt; x.log&#8221;); \u628a\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\uff0c\u770b\u6587\u4ef6\u91cc\u6709\u6ca1\u6709\u8f93\u5165\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1810178\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : oneisall8955 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528\u6570\u7ec4\u6765\u88c5\u547d\u4ee4\u548c\u53c2\u6570\u8bd5\u8bd5\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1810179\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : binux <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             sh \u4e0d\u662f\u540c\u4e00\u4e2a sh                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1810180\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : momocraft <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             sh -x \u7684\u8c03\u8bd5\u4fe1\u606f\u7684\u8f93\u51fa\u662f stderr                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1810181\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fmumu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u628a process \u7684\u9519\u8bef\u8f93\u51fa\u62ff\u51fa\u6765\u770b\u770b\uff0cexec \u6709\u51e0\u4e2a\u91cd\u8f7d\u65b9\u6cd5\u90fd\u770b\u770b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1810182\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : whoami9894 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;`<br \/>[email&#160;protected]:\/tmp# cat 1.sh<br \/>echo 123<br \/>[email&#160;protected]:\/tmp# sh 1.sh<br \/>123<br \/>[email&#160;protected]:\/tmp# sh -x 1.sh<br \/>+ echo 123<br \/>123<br \/>[email&#160;protected]:\/tmp# sh -x 1.sh &gt; 1<br \/>+ echo 123<br \/>[email&#160;protected]:\/tmp# cat 1<br \/>123<br \/>[email&#160;protected]:\/tmp# sh -x 1.sh &gt; 1 2&gt;&amp;1<br \/>[email&#160;protected]:\/tmp# cat 1<br \/>+ echo 123<br \/>123<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1810183\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Infernalzero <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 ProcessBuilder\uff0c\u628a sh \u548c xxx.sh \u4f5c\u4e3a\u4e24\u4e2a\u53c2\u6570\u4f20\u5165\u5373\u53ef<br \/>\u6216\u8005\u8fd8\u662f RunTime \u7684\u8bdd\u6539\u6210&#8221;\/bin\/bash&#8221;, &#8220;-c&#8221;, &#8220;xxx.sh&#8221;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1810184\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : huaouo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             ProcessBuilder redirectOutput(ProcessBuilder.Redirect.INHERIT)<br \/>redirectError(ProcessBuilder.Redirect.INHERIT)<br \/>\u8bd5\u8bd5\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1810185\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : duanguyuan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u95ee\u9898\u89e3\u51b3\u5566\uff01\u8c22\u8c22\u5927\u5bb6\u3002<br \/>\u6211\u5728 github \u4e0a\u627e\u5230\u4e00\u6bb5\u6bd4\u8f83\u5b8c\u6574\u7684\u4ee3\u7801\uff1a<br \/>https:\/\/github.com\/chenerzhu\/quartz-console\/blob\/master\/src\/main\/java\/com\/chenerzhu\/quartz\/executor\/impl\/ShellExecutor.java                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559 Java Runtime.e&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/108872"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=108872"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/108872\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=108872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=108872"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=108872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}