| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- package au.pfip;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- public class Shell {
- /** Returns null if it failed for some reason.
- */
- private static ArrayList<String> command(final String cmdline) {
- try {
- Process process =
- new ProcessBuilder("/usr/bin/bash", "-c", cmdline)
- .redirectErrorStream(true)
- .directory(new File("."))
- .start();
- ArrayList<String> output = new ArrayList<>();
- BufferedReader br = new BufferedReader(
- new InputStreamReader(process.getInputStream()));
- String line;
- while ( (line = br.readLine()) != null )
- output.add(line);
- //There should really be a timeout here.
- if (0 != process.waitFor())
- return null;
- return output;
- } catch (Exception e) {
- logging.error("RuntimeException(e) ~ " + e);
- return null;
- }
- }
- public static boolean check(String cmd, String needle)
- {
- String cmdOutput = test(cmd);
- return cmdOutput.contains(needle);
- }
- public static void runCmd(String cmd)
- {
- test(cmd);
- }
- // --Commented out by Inspection START (4/2/25, 10:02 pm):
- // public static boolean checkMulti(String cmd, String needle)
- // {
- // String cmdOutput = test(cmd);
- // return cmdOutput.contains(needle);
- // }
- // --Commented out by Inspection STOP (4/2/25, 10:02 pm)
- private static String test(String cmdline) {
- ArrayList<String> output = command(cmdline);
- StringBuilder reply = new StringBuilder();
- if (null == output) {
- return "";
- }
- else {
- for (String line : output) {
- reply.append(line);
- }
- }
- return reply.toString();
- }
- }
|