Yes there is! You can use BASH which is interpreting the commands you type into your terminal.
For example, try this:
for file in {input1,input2,input3}; do java myprogram $file; done
This one-liner is a simple loop which will run myprogram
with java on the list of files.
This is 100% equivalent to running:
java myprogram input1
java myprogram input2
java myprogram input3
You can use this syntax with any command you like. For example, you can list all files in your home directory with:
for file in ~/*; do echo $file; done
~/*
will expand into a list of all files within ~
directory (except for the hidden files).
Another example: if you wanted to run your program on 100 files, you do not need to list them by hand. Try this:
for i in $(seq 1 100); do java myprogram input$i; done;
$(seq 1 100)
will expand into the output of seq 1 100
command, which provides a list of numbers from 1 to 100. This way your program will be run on all 100 "inputN" files.
It is also fine to use multiple command within the loop, but then it becomes much more convenient to use multiple lines. The following example will run your program on 10 test inputs and will print differences from reference outputs:
for i in $(seq 1 10); do
java myprogram input$i > temp_out
diff temp_out output$i
done
As your script grows, it will soon become convenient to save it to a file. Write down your script (typically bash scripts will have .sh
extension), and run it with:
sh myscript.sh
or, if you set +x flag on myscript file, and placed #!\bin\sh
at the top of your script (which states that this file should be run with bash):
./myscript.sh
foo bar baz
and then running^bar^qux
will executefoo qux baz
. This is Quick Substitution; you can read more about it on StackOverflow here, or in thebash
manual here.