Saturday, November 14, 2015

Foreach loop is a feature that has been present in many languages like Perl, Ruby etc for some time. Java finally implemented it in Java 5. The official documents refer to this new loop as the "Enhanced for Loop", the "For-Each Loop" and the "foreach statement".

To ensure that existing code is not broken, it was decided that a new keyword will not be introduced. The changes were done in the syntax of the for keyword.

This syntax makes sense because the primary purpose of this loop is to make it easy to iterate through a collection. In such a case, declaring the variable in this place makes the most sense.

Caveat 2. What does the normal for loop provide that the foreach loop does not provide?
Answer: The index. While for loop provides an index which can also be used in the statements enclosed within the loop, the index cannot be obtained directly in the foreach loop.

Notice that the exit value of the command is a non-zero value.
However, if we use the -p option, then we can get the following result:

$ ls -d myblog
myblog
$ mkdir -p myblog
$ echo $?
0

As you can see, there is no error thrown in this case and the command exits with a 0 in this case. When you are writing a script and you want to ensure that a directory is present, then this command can be used to confirm the same.
This option is also used for creating a whole directory structure with one command. For example, to create a directory called myblog and a directory under it called bash and one more under it called basic_commands, you would need to do the following if you don't use the -p option:

In other words, you need to know if any part of the directory structure is already present. If so, you need to avoid creating it again. Also, the creation of the subdirectories requires you to type in the full path till that point again and again. This is a hassle and is error prone too.
The -p option helps here also:

$ ls myblog
$ mkdir -p myblog/bash/basic_commands
$ echo $?
0

There is one additional trick that you may not find in your man pages. If you want to create the following directory structure:

$ ls myblog myscript
myblog:
bash python
myscript:
bash python
$

You would need to execute the following command:

$ mkdir -p myblog/bash myblog/python myscript/bash myscript/python

Of course, you need to be careful to avoid typos while doing this. A better way to do this is using the following command:

$ mkdir -p {myblog,myscript}/{bash,python}

A more verbose way of specifying -p is --parents:

$ mkdir --parents {myblog,myscript}/{bash,python}

Talking of verbose, to see, what is happening when you use any of the commands above, add the -v or --verbose option: