编写一个 bash 脚本来运行一个 java 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38064801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing a bash script to run a java program
提问by vikas ray
I'm rank new to bash thus the question. I've a java program that I've exported as a .jar. This file when run as
我是 bash 的新手,因此这个问题。我有一个以 .jar 格式导出的 Java 程序。运行时此文件
java -jar somefile.jar
goes into an infinite loop and awaits a file name. Based on the correct file path it generates an output.
进入无限循环并等待文件名。它根据正确的文件路径生成输出。
How do I write a bash script to do automated testing of this project. I need the scrip to do the following -
我如何编写一个 bash 脚本来对这个项目进行自动化测试。我需要该脚本来执行以下操作 -
Run the program, which is run the same command
provide an array of 5 files as an input to the program
For each file write the output to an log file.
回答by J. Allan
This should do it.
这应该做。
#!/bin/bash
files="$@"
for i in $files;
do
echo "Doing $i"
java -jar somefile.jar <<< "$i"
done
Make sure you chmod u+x filename
it first. Then call it like this:
确保你chmod u+x filename
先。然后像这样调用它:
./filename firstfile secondfile thirdfile etc.
Other:
其他:
As sjsam pointed out, the use of <<<
is a strictly bash thing. You are apparently using bash ("I'm rank new to bash..."), but if you weren't, this would not work.
正如 sjsam 指出的那样,使用<<<
是严格的 bash 事情。您显然正在使用 bash(“我是 bash 的新人...”),但如果您没有使用,这将不起作用。
回答by vikas ray
Suppose my java program is HelloWorld.java. We can run it in 2 ways:
假设我的 java 程序是 HelloWorld.java。我们可以通过两种方式运行它:
1st using executable jar
第一次使用可执行jar
2nd by running java class from terminal
第二,从终端运行java类
create a new text file and name it hello.sh
创建一个新的文本文件并命名 hello.sh
In hello.sh
在 hello.sh
!/bin/bash
clear
java -jar HelloWorld.jar
Save it and open terminal:
保存并打开终端:
1 navigate to directory where your HelloWorld.jar
is present
1个导航到目录中的HelloWorld.jar
存在
2 give permission to terminal to run the bash script by using the following command
2 使用以下命令授予终端运行 bash 脚本的权限
sudo chmod 754 hello.sh
3 run you script by running the following command
3 通过运行以下命令来运行您的脚本
./hello.sh