{"id":1821,"date":"2024-07-08T18:33:37","date_gmt":"2024-07-08T10:33:37","guid":{"rendered":"https:\/\/diuut.com\/?p=1821"},"modified":"2024-07-08T18:34:45","modified_gmt":"2024-07-08T10:34:45","slug":"java%e4%b8%ad%e4%b8%8a%e4%bc%a0%e5%b9%b6%e8%b0%83%e7%94%a8shell%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=1821","title":{"rendered":"Java\u4e2d\u4e0a\u4f20\u5e76\u8c03\u7528Shell\u811a\u672c"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>\u9700\u6c42<\/p><cite>\u9700\u8981\u4e0a\u4f20\u8c03\u7528shell\u811a\u672c\uff0c\u611f\u89c9\u5f88\u591a\u573a\u666f\u90fd\u4f1a\u7528\u5230\uff0c\u8bb0\u5f55\u4e0b\u81ea\u5df1\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u65b9\u4fbf\u540e\u9762\u53d6\u7528<\/cite><\/blockquote>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_66_1 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\"><p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<\/div><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/diuut.com\/?p=1821\/#%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0\" title=\"\u6587\u4ef6\u4e0a\u4f20\">\u6587\u4ef6\u4e0a\u4f20<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/diuut.com\/?p=1821\/#%E8%B0%83%E7%94%A8%E8%A7%A6%E5%8F%91\" title=\"\u8c03\u7528\u89e6\u53d1\">\u8c03\u7528\u89e6\u53d1<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/diuut.com\/?p=1821\/#%E6%B5%8B%E8%AF%95%E8%84%9A%E6%9C%AC\" title=\"\u6d4b\u8bd5\u811a\u672c\">\u6d4b\u8bd5\u811a\u672c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/diuut.com\/?p=1821\/#%E8%84%9A%E6%9C%AC%E4%BF%AE%E6%94%B9\" title=\"\u811a\u672c\u4fee\u6539\">\u811a\u672c\u4fee\u6539<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0\"><\/span>\u6587\u4ef6\u4e0a\u4f20<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>java\u4e0a\u4f20sh\u6587\u4ef6<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Value(&quot;${file.shell.path}&quot;)\n    private String shellPath;\n\n@PostMapping(&quot;\/upload&quot;)\n    @ApiOperation(value = &quot;\u4e0a\u4f20\u6587\u4ef6&quot;, httpMethod = &quot;GET&quot;)\n    public R&lt;String&gt; uploadFile(@RequestParam(&quot;file&quot;) MultipartFile file) {\n        try {\n            \/\/ \u521b\u5efa\u4e0a\u4f20\u8def\u5f84\n            File uploadDir = new File(shellPath);\n            if (!uploadDir.exists()) {\n                uploadDir.mkdirs();\n            }\n\n            \/\/ \u751f\u6210\u5e26\u6709\u65f6\u95f4\u6233\u7684\u6587\u4ef6\u540d\n            String originalFileName = file.getOriginalFilename();\n            String extension = &quot;&quot;;\n            int dotIndex = originalFileName.lastIndexOf('.');\n            if (dotIndex &gt; 0) {\n                extension = originalFileName.substring(dotIndex);\n                originalFileName = originalFileName.substring(0, dotIndex);\n            }\n            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);\n            String timestamp = dateFormat.format(new Date());\n            String newFileName = originalFileName + &quot;_&quot; + timestamp + extension;\n\n            File destFile = new File(shellPath, newFileName);\n            @Cleanup\n            FileOutputStream fileOutputStream = new FileOutputStream(destFile);\n            StreamUtils.copy(file.getBytes(), fileOutputStream);\n\n            return R.ok(destFile.getPath().replaceAll(&quot;\\\\\\\\&quot;, &quot;\/&quot;),&quot;File upload success.&quot;);\n        } catch (IOException e) {\n            return R.fail(&quot;File upload failed: &quot; + e.getMessage());\n        }\n    }\n<\/pre><\/div>\n\n\n<h3><span class=\"ez-toc-section\" id=\"%E8%B0%83%E7%94%A8%E8%A7%A6%E5%8F%91\"><\/span>\u8c03\u7528\u89e6\u53d1<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u4e0a\u4f20\u540e\u89e6\u53d1\u6267\u884c\uff0c\u89e6\u53d1\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u5982xxljob\u8fd9\u4e9b\u8c03\u5ea6\u5668\uff0c\u8fd9\u91cc\u4e0d\u8d58\u8ff0\uff0c\u53ea\u8bb0\u5f55\u8c03\u7528\u7684\u90a3\u5757\u903b\u8f91\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n    public void execute() throws Exception {\n        StopWatch stopWatch = new StopWatch();\n        stopWatch.start();\n        \/\/ \u83b7\u53d6shell\u811a\u672c\n        String command = &quot;\/home\/shell\/test.sh&quot;\n        String&#91;] commandList = {command ,&quot;value1&quot;,&quot;value2&quot; ,&quot;value3&quot;,&quot;value4&quot;}\n        int exitValue = -1;\n        BufferedReader bufferedReader = null;\n        try {\n            \/\/ command process\n            ProcessBuilder processBuilder = new ProcessBuilder();\n            processBuilder.command(commandList);\n            processBuilder.redirectErrorStream(true);\n\n            Process process = processBuilder.start();\n\n            BufferedInputStream bufferedInputStream = new BufferedInputStream(process.getInputStream());\n            bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream));\n\n            \/\/ command log\n            String line;\n            while ((line = bufferedReader.readLine()) != null) {\n                log.info(line);\n            }\n\n            \/\/ command exit\n            process.waitFor();\n            exitValue = process.exitValue();\n        } catch (Exception e) {\n            log.error(e);\n        } finally {\n            if (bufferedReader != null) {\n                bufferedReader.close();\n            }\n        }\n\n        if (exitValue == 0) {\n            \/\/ default success \u6267\u884c\u6210\u529f\n        } else {\n           \/\/  return fail msg \u6267\u884c\u5931\u8d25\uff0c\u5931\u8d25\u6709\u5f88\u591a\u539f\u56e0\uff0c\u53ef\u4ee5\u4ece\u8fd9\u4e2aexitValue\u7801\u503c\u786e\u8ba4\n          \/\/  \u6bd4\u5982\u4e3a13  \u5c31\u662f\u6ca1\u7ed9777\u6267\u884c\u6743\u9650\uff0c2\u7684\u8bdd\uff0c\u5982\u679c\u8def\u5f84\u6ca1\u9519\uff0c\u5c31\u662f\u6587\u4ef6\u7c7b\u578b\u6ca1\u6539unix\n        }\n    }\n<\/pre><\/div>\n\n\n<h3><span class=\"ez-toc-section\" id=\"%E6%B5%8B%E8%AF%95%E8%84%9A%E6%9C%AC\"><\/span>\u6d4b\u8bd5\u811a\u672c<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u4e0a\u4f20\u4e00\u4e2a\u6d4b\u8bd5\u7528\u7684shell\u811a\u672c  test.sh,\u53ef\u4ee5\u6536\u53c2\u6570\u6765\u6267\u884c\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\necho &quot;First argument: $1&quot;\necho &quot;Second argument: $2&quot;\necho &quot;Third argument: $3&quot;\necho &quot;Fourth argument: $4&quot;\n<\/pre><\/div>\n\n\n<h3><span class=\"ez-toc-section\" id=\"%E8%84%9A%E6%9C%AC%E4%BF%AE%E6%94%B9\"><\/span>\u811a\u672c\u4fee\u6539<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>shell\u6587\u4ef6\u4e0a\u4f20\u540e\u9700\u8981\u4fee\u6539\u6743\u9650\u4ee5\u53ca\u5c06\u6587\u4ef6\u7684\u7c7b\u578b\u8bbe\u7f6e\u4e3aunix\uff0c\u5e76\u4e14\u66ff\u6362\u6362\u884c\u7b26\u3002\u8fd9\u91cc\u643a\u7a0b\u4e86\u4e00\u4e2a\u811a\u672c\uff0c\u65b9\u4fbf\u6279\u91cf\u4fee\u6539\u6743\u9650\u4ee5\u53ca\u8bbe\u7f6e\u6587\u4ef6\u7c7b\u578b\u3002batch_fix_shell.sh<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n#!\/bin\/bash\n\n# \u6307\u5b9a\u76ee\u5f55\nDIRECTORY=&quot;\/home\/shell&quot;\n\n# \u9012\u5f52\u5730\u7ed9\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u8bbe\u7f6e\u6743\u9650\nfind &quot;$DIRECTORY&quot; -type f -exec chmod 777 {} \\;\n\n# \u904d\u5386\u76ee\u5f55\u4e2d\u7684\u6240\u6709\u6587\u4ef6\nfor file in &quot;$DIRECTORY&quot;\/*; do\n    if &#91; -f &quot;$file&quot; ]; then\n        # \u4f7f\u7528vim\u547d\u4ee4\u884c\u6a21\u5f0f\u6765\u8bbe\u7f6e\u6587\u4ef6\u7c7b\u578b\uff0c\u6dfb\u52a0 -E \u9009\u9879\n        vim -E -c &quot;:set ff=unix&quot; -E -c &quot;:%s\/\\r$\/\/g&quot; -c &quot;:wq&quot; &quot;$file&quot;\n    fi\ndone\n\necho &quot;Permissions have been set to 777 for all files in $DIRECTORY&quot;\n<\/pre><\/div>\n\n\n<p>\u4ee5\u4e0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42 \u9700\u8981\u4e0a\u4f20\u8c03\u7528shell\u811a\u672c\uff0c\u611f\u89c9\u5f88\u591a\u573a\u666f\u90fd\u4f1a\u7528\u5230\uff0c\u8bb0\u5f55\u4e0b\u81ea\u5df1\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u65b9\u4fbf\u540e\u9762\u53d6\u7528 \u6587\u4ef6\u4e0a\u4f20 java<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=1821\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">Java\u4e2d\u4e0a\u4f20\u5e76\u8c03\u7528Shell\u811a\u672c<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":657,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[37,46,70],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1821"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1821"}],"version-history":[{"count":3,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1821\/revisions"}],"predecessor-version":[{"id":1824,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1821\/revisions\/1824"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/657"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}