您的位置:首页 > 运维架构 > Shell

java调用shell命令的两种方法

2014-03-27 20:14 609 查看
运行环境:centos6.5 eclipse

可以借鉴转码译文分析

public static void insert(){

String pathshell="/home/wjx/spider/baidu/java.sh";

//String cmd="sort /home/wjx/spider/baidu/SearchUrl_CSDN.txt | uniq >/home/wjx/spider/baidu/SearchUrl_CSDN_b.txt;sort /home/wjx/spider/baidu/SearchUrl_OSchina.txt | uniq >/home/wjx/spider/baidu/SearchUrl_OSchina_b.txt";

Process ps;

try {

//ps = Runtime.getRuntime().exec(new String[]{"sh","-c",cmd});

ps = Runtime.getRuntime().exec(pathshell);

ps.waitFor();

String path = "/home/wjx/spider/baidu/";

File f = new File(path);

String[] names = f.list(new FilenameFilter() {

public boolean accept(File dir, String name) {

return name.indexOf(".txt") != -1;// 等于-1,表示不存在

}

});

for (String str : names) {

if (str.endsWith("_b.txt"))

//insert_b(path + str);

if (str.endsWith("_v.txt"))

;

}} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

"/home/wjx/spider/baidu/java.sh" 中的shell脚本:

sort /home/wjx/spider/baidu/SearchUrl_CSDN.txt | uniq >/home/wjx/spider/baidu/SearchUrl_CSDN_b.txt

sort /home/wjx/spider/baidu/SearchUrl_OSchina.txt | uniq >/home/wjx/spider/baidu/SearchUrl_OSchina_b.txt
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: