1

Suppose that I want to execute the command ls ten times (despite of it being purposeless since it will only display the directory contents 10 times), is there an equivalent to writing the following:

for (i = 0, i < 10, i++) {

     ls

}

Now, suppose that I want to execute a Java program ten times, each time with a different input file. Is there an equivalent to writing the following:

fileList = [input1, input2, ....., input10]

for (i = 0, i < 10, i++) {

     java myProgram fileList[i]

}

I am guessing that there is some sort of language where I could write this kind of scripts. I tried looking online but my knowledge is basic and I could not find anything that I understood.

1
  • Something that doesn't exactly answer your question but is along the same lines and is also useful: executing foo bar baz and then running ^bar^qux will execute foo qux baz. This is Quick Substitution; you can read more about it on StackOverflow here, or in the bash manual here.
    – wchargin
    Jun 22, 2014 at 1:57

2 Answers 2

4

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
2

This language is the very program where you're tying the command.

The command line prompt is provided by a shell. Ubuntu's default shell is Bash (you should start with tutorials and consult the manual for reference).

To execute multiple related commands successively, you can use a loop. You can store a list of file names in an array variable.

file_list=(input1 input2 … input10)
for file in "${file_list[@]}"; do
  java myProgram "$file"
done

The syntax elements I've used:

  • Array variable assignment: variable=(element1 element2 element3) (no spaces around =)
  • Using an array variable: "${variable[@]}" (yes, it's weird, for historical reasons)
  • Using a string variable: "$variable"
  • For loop: for variable in list; do instructions; done (; is equivalent to a newline)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .