简体   繁体   中英

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

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 . 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

  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. Here Runtime example java you can find an example of how to do that

The newer approach would be using ProcessBuilder class though

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. One advantage of the port can be portability.

I think you want JavaMail to do the mail access. I am not a specialist of this API, but I used it in a small Processing program to show its usage: Email sketch .

Then you need to filter out the results and to isolate the part you want, which isn't very hard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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