Skip to content

Commit 275a44b

Browse files
authored
Merge pull request Blankj#1445 from yuruxuan/master
add environment variable
2 parents 10ae070 + fd2d59a commit 275a44b

File tree

1 file changed

+87
-1
lines changed

1 file changed

+87
-1
lines changed

lib/utilcode/src/main/java/com/blankj/utilcode/util/ShellUtils.java

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ public static CommandResult execCmd(final String command, final boolean isRooted
133133
return execCmd(new String[]{command}, isRooted, true);
134134
}
135135

136+
/**
137+
* Execute the command.
138+
*
139+
* @param command The command.
140+
* @param envp The environment variable settings.
141+
* @param isRooted True to use root, false otherwise.
142+
* @return the single {@link CommandResult} instance
143+
*/
144+
public static CommandResult execCmd(final String command, final List<String> envp, final boolean isRooted) {
145+
return execCmd(new String[]{command},
146+
envp == null ? null : envp.toArray(new String[]{}),
147+
isRooted,
148+
true);
149+
}
150+
136151
/**
137152
* Execute the command.
138153
*
@@ -144,6 +159,23 @@ public static CommandResult execCmd(final List<String> commands, final boolean i
144159
return execCmd(commands == null ? null : commands.toArray(new String[]{}), isRooted, true);
145160
}
146161

162+
/**
163+
* Execute the command.
164+
*
165+
* @param commands The commands.
166+
* @param envp The environment variable settings.
167+
* @param isRooted True to use root, false otherwise.
168+
* @return the single {@link CommandResult} instance
169+
*/
170+
public static CommandResult execCmd(final List<String> commands,
171+
final List<String> envp,
172+
final boolean isRooted) {
173+
return execCmd(commands == null ? null : commands.toArray(new String[]{}),
174+
envp == null ? null : envp.toArray(new String[]{}),
175+
isRooted,
176+
true);
177+
}
178+
147179
/**
148180
* Execute the command.
149181
*
@@ -169,6 +201,40 @@ public static CommandResult execCmd(final String command,
169201
return execCmd(new String[]{command}, isRooted, isNeedResultMsg);
170202
}
171203

204+
/**
205+
* Execute the command.
206+
*
207+
* @param command The command.
208+
* @param envp The environment variable settings.
209+
* @param isRooted True to use root, false otherwise.
210+
* @param isNeedResultMsg True to return the message of result, false otherwise.
211+
* @return the single {@link CommandResult} instance
212+
*/
213+
public static CommandResult execCmd(final String command,
214+
final List<String> envp,
215+
final boolean isRooted,
216+
final boolean isNeedResultMsg) {
217+
return execCmd(new String[]{command}, envp == null ? null : envp.toArray(new String[]{}),
218+
isRooted,
219+
isNeedResultMsg);
220+
}
221+
222+
/**
223+
* Execute the command.
224+
*
225+
* @param command The command.
226+
* @param envp The environment variable settings array.
227+
* @param isRooted True to use root, false otherwise.
228+
* @param isNeedResultMsg True to return the message of result, false otherwise.
229+
* @return the single {@link CommandResult} instance
230+
*/
231+
public static CommandResult execCmd(final String command,
232+
final String[] envp,
233+
final boolean isRooted,
234+
final boolean isNeedResultMsg) {
235+
return execCmd(new String[]{command}, envp, isRooted, isNeedResultMsg);
236+
}
237+
172238
/**
173239
* Execute the command.
174240
*
@@ -196,6 +262,26 @@ public static CommandResult execCmd(final List<String> commands,
196262
public static CommandResult execCmd(final String[] commands,
197263
final boolean isRooted,
198264
final boolean isNeedResultMsg) {
265+
return execCmd(commands, null, isRooted, isNeedResultMsg);
266+
}
267+
268+
/**
269+
* Execute the command.
270+
*
271+
* @param commands The commands.
272+
* @param envp Array of strings, each element of which
273+
* has environment variable settings in the format
274+
* <i>name</i>=<i>value</i>, or
275+
* <tt>null</tt> if the subprocess should inherit
276+
* the environment of the current process.
277+
* @param isRooted True to use root, false otherwise.
278+
* @param isNeedResultMsg True to return the message of result, false otherwise.
279+
* @return the single {@link CommandResult} instance
280+
*/
281+
public static CommandResult execCmd(final String[] commands,
282+
final String[] envp,
283+
final boolean isRooted,
284+
final boolean isNeedResultMsg) {
199285
int result = -1;
200286
if (commands == null || commands.length == 0) {
201287
return new CommandResult(result, "", "");
@@ -207,7 +293,7 @@ public static CommandResult execCmd(final String[] commands,
207293
StringBuilder errorMsg = null;
208294
DataOutputStream os = null;
209295
try {
210-
process = Runtime.getRuntime().exec(isRooted ? "su" : "sh");
296+
process = Runtime.getRuntime().exec(isRooted ? "su" : "sh", envp, null);
211297
os = new DataOutputStream(process.getOutputStream());
212298
for (String command : commands) {
213299
if (command == null) continue;

0 commit comments

Comments
 (0)