for


Bash: Problem with reading files with spaces in the name using a for loop

Recently we were working on a bash script that was supposed to find and process some files that matched certain criteria. The script would process the files one by one and the criteria would be matched using the find command. To implement our solution, we returned the results of the find back to the for loop in an attempt to keep it simple and human readable.

Our original code was the following:
(do not use it, see explanation below)

for file in `find $search_path -type f -name '*.kml'`; do
  # Formatting KML file to be human friendly.
  xmllint --format "$file" > "$output_path/$file";
done

Soon we realized that we had a very nasty bug, the way we formatted the command it would break filenames that had spaces in them into multiple for loop entries and thus we would get incorrect filenames back to process.

To solve this issue we needed a way to force our loop to read the results of find one line at a time instead of one word at a time. The solution we used in the end was fairly different than the original code as it had the following significant changes:

  • the results of the find command were piped into the loop
  • the loop was not longer a for loop and a while loop was used instead
  • it used the read command that reads one line at a time to fill in the filename variable
    (the -r parameter does not allow backslashes to escape any characters)

Solution

find $search_path -type f -name '*.kml' | 
while read -r file; do
  # Formatting KML file to be human friendly.
  xmllint --format "$file" > "$output_path/$file";
done


Java: Methods on how to iterate over a List

Following we present a few methods on how to iterate over a List in Java.

Currently we present:

  1. Using a standard for loop
  2. Using an iterator to loop
  3. Using a For-Each loop
  4. Using Streams

[download id=”2234″]

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ListLooper {

    public static void main(final String[] argv) {

        final String elementsArray[] = new String[] { "First Element", "Second Element", "Third Element" };
        // First of all we convert an array of Strings to a list of Strings, we do this to avoid adding each element to the list we will use by using the add() method.
        final List<String> elementsList = Arrays.asList(elementsArray);

        // Method 1: Using a standard for loop
        System.out.println("Method 1: Using a standard for loop");
        // We will loop N times, where N is the size of the list.
        // Since the first element of the list is on position 0, we start from that and finish at position N-1.
        for (int i = 0; i < elementsList.size(); i++) {

            // Using get() we retrieve the element at position.
            System.out.println(elementsList.get(i));
        }

        // Method 2: Using an iterator to loop
        System.out.println("Method 2: Using an iterator to loop");
        // The Java iterator is an interface that belongs to the collection framework and allows us to traverse a collection and access the data element of the collection without bothering the user about the implementation details of that collection. 
        final Iterator<String> iterator = elementsList.iterator();
        while (iterator.hasNext()) {

            // next() returns the next element in the collection until the hasNext() method returns false.
            System.out.println(iterator.next());
        }

        // Method 3: Using a For-Each loop
        System.out.println("Method 3: Using a For-Each loop");
        // This code works for any object that implements the Iterable interface.
        for (final String element : elementsList) {

            System.out.println(element);
        }

        // Method 4: Using Streams
        System.out.println("Method 4: Using Streams");
        // This code will not work for Java versions earlier than Java 8.
        elementsList.forEach((element) -> {

            System.out.println(element);
        });
     }
}

[download id=”2234″]

Compilation and execution are presented in the next section.


javac ListLooper.java;

java ListLooper;
Method 1: Using a standard for loop
First Element
Second Element
Third Element
Method 2: Using an iterator to loop
First Element
Second Element
Third Element
Method 3: Using a For-Each loop
First Element
Second Element
Third Element
Method 4: Using Streams
First Element
Second Element
Third Element

Following is the Java version used in this article

java -version
openjdk version "1.8.0_111"
OpenJDK Runtime Environment (build 1.8.0_111-b16)
OpenJDK 64-Bit Server VM (build 25.111-b16, mixed mode)