gpt4 book ai didi

java - 在服务器上运行 Shell 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 08:10:54 26 4
gpt4 key购买 nike

我有一个独立的应用程序,它在 Ubuntu 中运行 Shell 脚本(带参数)。

ProcessBuilder pb1 = new ProcessBuilder("sh","deltapackage_app.sh","part_value","pathtofile"); 
Process process1 = pb1.start();

我正在通过 GUI 获取参数。现在我想在 Web 应用程序中实现同样的事情,我可以从网页中获取输入并将其发送到服务器,然后服务器将使用参数执行 shell 脚本。

任何人都可以建议我最好的方法吗?我应该用什么东西来做到这一点。

我知道我必须学习很多有关服务器的知识。或者我可以对基于浏览器的应用程序使用相同的代码。

最佳答案

Consider the following line of code:

Process p = Runtime.getRuntime().exec("/bin/sh -c /bin/ls > ls.out");

This is intended to execute a Bourne shell and have the shell execute the ls command, redirecting the output of ls to the file ls.out. The reason for using /bin/sh is to get around the problem of having stdout redirected by the Java internals. Unfortunately, if you try this nothing will happen. When this command string is passed to the exec() method it will be broken into an array of Strings with the elements being "/bin/sh", "-c", "/bin/ls", ">", and "ls.out". This will fail, as sh expects only a single argument to the "-c" switch. To make this work try:

    String[] cmd = {"/bin/sh", "-c", "/bin/ls > out.dat"};
Process p = Runtime.getRuntime().exec(cmd);

Since the command line is already a series of Strings, the strings will simply be loaded into the command array by the exec() method and passed to the new process as is. Thus the shell will see a "-c" and the command "/bin/ls > ls.out" and execute correctly.

http://www.ensta-paristech.fr/~diam/java/online/io/javazine.html

关于java - 在服务器上运行 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11240504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com