{"id":2242,"date":"2023-05-07T18:17:23","date_gmt":"2023-05-07T10:17:23","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2242"},"modified":"2023-05-07T18:17:31","modified_gmt":"2023-05-07T10:17:31","slug":"groovy-code-example-execute-shell-commands","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/05\/07\/groovy-code-example-execute-shell-commands\/","title":{"rendered":"Groovy\u4ee3\u7801\u793a\u4f8b &#8211; groovy\u4e2d\u5982\u4f55\u6267\u884cshell\u547d\u4ee4\uff1f"},"content":{"rendered":"<p>groovy\u5199shell\u4e5f\u662f\u5f88\u65b9\u4fbf\uff0c\u5177\u4f53\u5199\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/\u5b9a\u4e49\u8f93\u51fa\u6d41\u548c\u9519\u8bef\u6d41\ndef sout = new StringBuilder(), serr = new StringBuilder()\n\/\/\u5b9a\u4e49shell\ndef proc = &#039;ls \/badDir&#039;.execute()\n\/\/\u6267\u884cshell\u5e76\u83b7\u53d6\u8f93\u51fa\u548c\u5f02\u5e38\nproc.consumeProcessOutput(sout, serr)\n\/\/\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\nproc.waitForOrKill(1000)\n\/\/\u6253\u5370\u8f93\u51fa\u548c\u9519\u8bef\u4fe1\u606f\uff08\u5047\u5982\u6709\u7684\u8bdd\uff09\nprintln &quot;out&gt; $sout err&gt; $serr&quot;<\/code><\/pre>\n<p>\u8f93\u51fa\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">out&gt;  err&gt; ls: cannot access \/badDir: No such file or directory<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>groovy\u5199shell\u4e5f\u662f\u5f88\u65b9\u4fbf\uff0c\u5177\u4f53\u5199\u6cd5\u5982\u4e0b\uff1a \/\/\u5b9a\u4e49\u8f93\u51fa\u6d41\u548c\u9519\u8bef\u6d41 def sout = new St [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[568],"tags":[],"class_list":["post-2242","post","type-post","status-publish","format-standard","hentry","category-groovy"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2242"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2242\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}