简体   繁体   English

Java中的Linux Shell脚本

[英]linux shell script in java

I am using below shell code in linux i want it to make it in Java.I want to call this command in JAVA 我在Linux中使用以下shell代码,我希望它在Java中实现。我想在JAVA中调用此命令

mailq | grep -B1 -i temporarily | grep -iv deferred | \
egrep -i 'jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec' | \
awk -F" " '{print $1}' | awk '{print  substr($0,10,14)}'

mailq command shows the queued mail messages with there respective Id and there errors . mailq命令显示排队的邮件消息,其中包含相应的ID和错误。 Like in command above i am looking for messages with temporarily disabled message in it and taking the above message where its id is present in * *09089 like this checking the month and than printing the last 5 characters of that id 像上面命令我正在寻找的信息与它暂时禁用的消息,并采取了以上的消息,其中其ID出现在* 09089这样的检查月份和比打印该ID的最后5个字符

  String cmd = "mailq | grep -B1 -i temporarily | grep -iv deferred | "
              +"egrep -i 'jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec' |" 
              + " \'{print $1}\' | awk \'{print  substr($0,10,14)}\'";
    try
    {
      ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
      Process p = pb.start();
      p.destroy();
    }
    catch (Exception e)
    {e.printStackTrace();}

In general this type of issues is handled in java by 'Runtime' class. 通常,此类问题在Java中由“运行时”类处理。 Here Runtime example java you can find an example of how to do that 运行时示例Java中,您可以找到如何执行该操作的示例

The newer approach would be using ProcessBuilder class though 较新的方法是使用ProcessBuilder类

If I understood correctly, you want to do the same thing that this shell command, but in pure Java, not by running it from Java. 如果我理解正确,那么您想要执行与该Shell命令相同的操作,但是要使用纯Java,而不是通过从Java运行它。 One advantage of the port can be portability. 端口的优点之一是可移植性。

I think you want JavaMail to do the mail access. 我认为您希望JavaMail进行邮件访问。 I am not a specialist of this API, but I used it in a small Processing program to show its usage: Email sketch . 我不是该API的专家,但是我在一个小型Processing程序中使用了它来显示其用法: Email sketch

Then you need to filter out the results and to isolate the part you want, which isn't very hard. 然后,您需要过滤出结果并隔离出想要的部分,这并不是很难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM