2 * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 package org
.lttng
.tools
.utils
;
21 import java
.io
.IOException
;
22 import java
.lang
.ProcessBuilder
.Redirect
;
23 import java
.nio
.file
.Files
;
24 import java
.nio
.file
.Path
;
25 import java
.util
.Arrays
;
26 import java
.util
.List
;
27 import java
.util
.StringJoiner
;
30 * Utility methods to execute commands on the command line.
32 * @author Alexandre Montplaisir
34 public final class ShellUtils
{
36 private ShellUtils() {}
39 * Simple command to test that the environment / stdout are working
43 * Command-line arguments
45 public static void main(String
[] args
) {
46 List
<String
> command
= Arrays
.asList("ls", "-l");
47 executeCommand(command
);
51 * Execute a shell command and retrieve its return value.
54 * The command to execute, as a list of individual arguments (do
56 * @return If the command returned successfully (ret code = 0)
58 public static boolean executeCommand(List
<String
> command
) {
60 /* "echo" the command to stdout */
61 StringJoiner sj
= new StringJoiner(" ", "$ ", "");
62 command
.stream().forEach(sj
::add
);
63 System
.out
.println(sj
.toString());
65 ProcessBuilder builder
= new ProcessBuilder(command
);
66 builder
.redirectErrorStream(true);
67 builder
.redirectOutput(Redirect
.INHERIT
);
69 Process p
= builder
.start();
70 int ret
= p
.waitFor();
72 System
.out
.println("(returned from command)");
76 } catch (IOException
| InterruptedException e
) {
82 * Execute a shell command and retrieve its output.
85 * Should the output also be printed to stdout as usual
87 * The command to execute, as a list of individual arguments (do
89 * @return The output of the command, as one list element per line
91 public static List
<String
> getOutputFromCommand(boolean print
, List
<String
> command
) {
93 /* "echo" the command to stdout */
94 StringJoiner sj
= new StringJoiner(" ", "$ ", "");
95 command
.stream().forEach(sj
::add
);
96 System
.out
.println(sj
.toString());
98 Path tempFile
= Files
.createTempFile("test-output", null);
100 ProcessBuilder builder
= new ProcessBuilder(command
);
101 builder
.redirectErrorStream(true);
102 builder
.redirectOutput(Redirect
.to(tempFile
.toFile()));
104 Process p
= builder
.start();
107 List
<String
> lines
= Files
.readAllLines(tempFile
);
108 Files
.delete(tempFile
);
111 /* Also print the output to the console */
112 lines
.stream().forEach(System
.out
::println
);
114 System
.out
.println("(output silenced)");
117 System
.out
.println("(returned from command)");
120 } catch (IOException
| InterruptedException e
) {
This page took 0.036031 seconds and 5 git commands to generate.