2

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

2
  • Perhaps you can explain what this command is supposed to do. On Ubuntu, I typed man mailq and got No manual entry for mailq. I know I can google it, but easing the work of those wanting to help can be a good idea... Here, you expect people to both know well mailq, awk, and Java (not JAVA, BTW). I am sure there are plenty of those people, but are they reading this question? Commented May 3, 2012 at 9:11
  • Thanks Philho thanks for pointing that out highly apprecited Commented May 3, 2012 at 9:23

4 Answers 4

3
  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();}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Bitmap Is there any other way to call above command in JAVA ,since mailq command exists in Linux is there a command in JAVA equivalent to mailq command in Linux so that i can write this shell code in JAVA directly
There's no library for mailq Java.
3

here http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.