1

I have a java program that takes time to execute and I have to make 10 runs of it and I am interested in only last 5 lines of the output, the actual output runs in hundreds of lines. Since I want to average the output I want tail -5 for run into a file. Also one of the parameters (--random) in my arguments keep changing in each run.

I am doing the following:

for i in {1..10} ; do cat output| tail -5 | java -cp src.Tagger.java --random $1; done

Sorry I am really bad at bash.

4
  • 3
    Don't use cat to pipe into tail. Just say tail -5 output. Commented Mar 1, 2013 at 17:44
  • Where does the value for --random come from? The code you have looks correct syntactically, but $1 may not be the value you want to supply to --random. Commented Mar 1, 2013 at 17:54
  • Your script does not match your explanation of what you want. Your script reads the last 5 lines of the file output and feeds that as input to your java process. Your description seems to say you want the last 5 lines of the output of your java process, which would be java -cp .... | tail -5. Commented Mar 1, 2013 at 18:08
  • Yes I want to know how I put random values , random is actually any integer Commented Mar 1, 2013 at 18:56

2 Answers 2

1

You should make sure you can execute your java program at all first. You can't execute a .java file directly, it has to be compiled.

If you have the file src/Tagger.java, you can try to compile it with

javac -cp src src/Tagger.java

but if it requires other libraries or build systems, it might be completely different. If you downloaded this app, see the project documentation.

This should silently produce a src/Tagger.class. Once you have this, you can try to run it with

java -cp src Tagger --random 1234

though again, if it has dependencies on libraries, it'll be different.

If that works, you can finally start trying to run it in a loop:

for i in {1..10}
do 
    cat output| tail -5 | java -cp src Tagger --random 1234
done
Sign up to request clarification or add additional context in comments.

Comments

1

You want the output of your java program to go to output first, then you need to tail the file. It looks like you are currently feeding output into your java program as input. I don't think that is what you want. Try this instead:

for i in {1..10}
do 
    java -cp src.Tagger.java --random $1 > output;
    tail -5 output;
done

I also have my doubts that you have the java command correct. You shouldn't specify .java for the file name when running the java file. It needs to run from the compiled .class file and the java command assumes .class, so it isn't needed on the command line. You also are using -cp (classpath) but don't appear to be giving it an argument. I'd expect the java command to be more like:

java -cp classesdir com.mydomain.myapp.Tagger

1 Comment

yeh sorry I put .java by mistake I am not actually using it while running

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.