Yagyavalk Bhatt
Yagyavalk Bhatt

Reputation: 342

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

Upvotes: 2

Views: 1251

Answers (4)

PhiLho
PhiLho

Reputation: 41132

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.

Upvotes: 0

Bitmap
Bitmap

Reputation: 12538

  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();}

Upvotes: 3

Mark Bramnik
Mark Bramnik

Reputation: 42441

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

Upvotes: 0

Related Questions