Wrecked

Holding You Back As I Breath You In


  • 首页

  • 关于

  • 标签26

  • 分类15

  • 归档34

  • 搜索

Java 执行Linux Command

发表于 2020-05-21 | 分类于 Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


@RestController
public class ExecuteController {
/**
* 执行指定命令
*/
@RequestMapping(value = "/executeMult", method = RequestMethod.GET)
public Object executeMult(String command) throws IOException {
if (StringUtils.isEmpty(command))
return "Error Command ";
String[] strings = new String[3];
strings[0] = "/bin/sh";
strings[1] = "-c";
strings[2] = command;
StringBuffer stringBuffer = new StringBuffer();
int exitValue = -1;
BufferedReader bufferedReader = null;
try {
// command process
Process process = Runtime.getRuntime().exec(strings);
BufferedInputStream bufferedInputStream = new BufferedInputStream(process.getInputStream());
bufferedReader = new BufferedReader(new InputStreamReader(bufferedInputStream));
// command log
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append(System.getProperty("line.separator"));
System.out.println(line);
}
// command exit
process.waitFor();
exitValue = process.exitValue();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
if (exitValue == 0) {
return stringBuffer.toString();
} else {
return "command exit value(" + exitValue + ") is failed";
}
}
}

快速查询日志命令

发表于 2020-05-20 | 更新于 2020-05-21 | 分类于 Linux |
./tail.sh filename linenumber seratchText
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#字体颜色
blue_font="\E[34m\033[01m"
green_font="\033[1;32m"
yellow_font="\033[33m"
light_green_font_withbackcolor="\033[42;30m\033[01m"
light_green_font="\033[32m\033[01m"
red_font="\033[1;31m"

color_end="\033[0m"



#参数校验
if [ ! -n "$1" ] ;then
echo -e "${blue_font} 请指定日志文件目录 ! ${color_end}"
exit 1;
fi

line=$2

if [ ! -n "$2" ] ;then
line=20
fi

if [ ! -n "$3" ] ;then
echo -e "${yellow_font} 执行:tail -"$line"f $1 ${color_end}"
tail -"$line"f $1

else
echo -e "${yellow_font} 执行:tail -"$line"f $1 |grep "$3" ${color_end}"

tail -"$line"f $1 |grep "$3"
fi

正则表达式案例

发表于 2019-12-18 | 分类于 Java |

替换文本格式的数据为 hashmap.put(“a1”,”b1”);

1
2
3
4
5
6
7
a1     b1 
23g sdfs


正则表达式:([A-Z0-9]+)[ ]+(.*)

hashmap.put("$1","$2")
123…12
Wrecked

Wrecked

34 日志
15 分类
26 标签
RSS
GitHub E-Mail
Links
  • WeChat
  • Java2s
© 2020 本博客所有文章除特别声明外。转载请注明出处!