我正在通过Shell脚本运行一个命令,因此无论Shell脚本提供的数据如何,我都需要以Java代码打印。要实现这一点,我要通过Java在Shell脚本下执行。但是,从shell脚本获取的 $ data 都是以Java打印的。谁能帮我吗?
下面是我的adb.sh
#!/bin/sh
REPO_DIR=/home/jagadeesh/android-sdk-linux/platform-tools/
cd ${REPO_DIR}
data= adb shell logcat | grep testing_aws
echo $data
下面是我的Java文件:这里我正在执行Shell脚本
String[] cmdScript = new String[]{"/bin/bash", "-C", "/home/jagadeesh/Desktop/automation/Adb_logs/adb.sh"};
StringBuilder output2 = new StringBuilder();
try {
Process procScript = Runtime.getRuntime().exec(cmdScript);
procScript.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(procScript.getInputStream()));
// System.out.println(reader);
String line = "";
while ((line = reader.readLine())!= null) {
output2.append(line + "\n");
System.out.println(output2);
}
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:0)
在abd.sh
中将data= adb shell logcat | grep testing_aws
更改为
data=`adb shell logcat | grep testing_aws`
请找到对我有用的代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Script {
public static void main(String[] args) {
// TODO Auto-generated method stub
String cmdScript = "/home/abd.sh";
StringBuilder output2 = new StringBuilder();
try {
Process procScript = Runtime.getRuntime().exec(cmdScript);
procScript.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(procScript.getInputStream()));
// System.out.println(reader);
String line = "";
while ((line = reader.readLine())!= null) {
output2.append(line + "\n");
System.out.println(output2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
abd.sh
#!/bin/sh
data=`cat /home/new.ldif|grep dn`
echo $data
输出
bash-3.00$ java Script
dn: dc=opensso,dc=java,dc=net