Unix cat and tail for output of java program - bash

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.

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

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

Related

AFL-Fuzz - Odd, Check Syntax! - How to add command line arguments to binary?

I am attempting to fuzz a proprietary binary with no source code that accepts a config file. So the typical use case would be:
./File --config file.config
The config is a bunch of different parameters that are required to run the rest of the program, and runs fine if I run it by itself. Additionally, the config file is within the input directory.
I am attempting to fuzz it utilizing the following command with AFL:
./afl-fuzz -Q -i input/ -o output/ -m 400 ./File --configfile
However, once I run the command, everything looks fine, but as soon as I get to the first iteration of 'havoc', I get an 'odd, check syntax!' error. If I add a ## at the end, the afl will give me a timeout error. I'm assuming that once afl-fuzz starts to mutate that input file, it breaks the binary, but I'm not sure and I'm not sure what else to try - any ideas? Thanks!

Get log entries written while executing command

I have a service that writes to a file in /var/log. For testing purposes, I am looking for a way to extract just the log lines that are written while executing a command against the service. I know I could do it with a C program using fseek/ftell, but that would require extra tooling in the VM. I would prefer a pure bash solution (bash 4.4, Ubuntu 18.04). I thought maybe something about using tail -f might work, but I can't figure out exactly how to work that.
You can use diff command. It takes 2 files as input and prints differing lines. You can copy the log file before execution of the service and compare it to the original file afterwords.
$ cat > logfile
line 1
line 2 asdf
$ cp logfile logfile-old
$ cat >> logfile
Third one.
Oups. Error occured.
$ diff logfile logfile-old
3,4d2
< Third one.
< Oups. Error occured.

What `<` is for in bash

So I'm taking a course at a university and the professor tells us to run our program like this java -cp classes cst420.Waypoint < samples.txt. What exactly is the < doing in this command? I know it's taking the samples.txt as input but I'm curious on exactly what's going on here.
It grabs input from the standard input, which becomes samples.txt, on most systems. If you're on unix, you can also do cat samples.txt | "java -cp classes cst420.Waypoint - to achieve a similar effect.
The difference being you can't seek on piped input and can on feeding it into the JVM (h/t #CharlesDuffy).

How do I write a shell script that repeats a java program a specific number of times?

Essentially I am looking to write a shell script, likely using a for loop, that would allow me to repeat a program call multiple times without having to do it by hand (I don't know exactly how to explain this, but i want to perform the java TestFile.java command in the cmd window multiple times without doing it by hand).
I am trying to write it for the UNIX shell in bash, if that helps at all.
My program outputs a set of numbers that I want to look at to analyze end behavior, so I need to perform many tests for many different inputs and I want to streamline the process. I have a pretty basic understanding of shell scripting - i tried to teach myself today but I couldn't really understand the syntax of the for loop or the syntax of how to write a .java file call, but I would be able to write them in shell script with a little help.
This will do:
#!/bin/bash
javac Testfile.java # compile the program
for((i=1;i<=50;i++))
do
echo "Output of Iteration $i" >> outfile
java Testfile >> outfile
done
This will compile your java program and run it for 50 times and store the output in a file named outfile. Likewise, you can change the 50 for the number of iterations you want.
#!/bin/bash
for i in {1..10}
do
#insert file run command here
done
#!/bin/bash
LOOPS=50
for i IN {1 .. LOOPS}
do
java TestFile >> out.log
done

Help with aliases in shell scripts

I have the following code, which is intended to run a java program on some input, and test that input against a results file for verification.
#!/bin/bash
java Program ../tests/test"$#".tst > test"$#".asm
spim -f test"$#".asm > temp
diff temp ../results/test"$#".out
The gist of the above code is to:
Run Program on a test file in another directory, and pipe the output into an assembly file.
Run a MIPS processor on that program's output, piping that into a file called temp.
Run diff on the output I generated and some expected output.
I made this shell script to help me automate checking of my homework assignment for class. I didn't feel like manually checking things anymore.
I must be doing something wrong, as although this program works with one argument, it fails with more than one. The output I get if I use the $# is:
./test.sh: line 2: test"$#".asm: ambiguous redirect
Cannot open file: `test0'
EDIT:
Ah, I figured it out. This code fixed the problem:
#!/bin/bash
for arg in $#
do
java Parser ../tests/test"$arg".tst > test"$arg".asm
spim -f test"$arg".asm > temp
diff temp ../results/test"$arg".out
done
It turns out that bash must have interpreted a different cmd arg for each time I was invoking $#.
enter code here
If you provide multiple command-line arguments, then clearly $# will expand to a list of multiple arguments, which means that all your commands will be nonsense.
What do you expect to happen for multiple arguments?

Resources