Using foreach


Using foreach

When you find yourself putting in commands over and over, you can use the foreach statement that executes a code block for each data item in an array. An array is a set of elements indexed sequentially that have the same type of data. Most times, each element of an array has a unique identifying index number. Changes made to one element of an array do not affect the other elements. foreach specifies that for each entry in the array of entries, Unix will output the entries to the terminal. Commonly, foreach is used in place of a standard for loop. Unlike this for loop construct, however, a foreach loop usually does not specify the order in which the items are considered.

The point of this is, if you don't want to spend a massive amount of time inputting files into a command to automate them, you can use the foreach command, which takes a list of files, and does something "for each" of them based on what you specify. The commands' use is much easier to demonstrate than to explain. If you have a list of files that you need to do something to, you can follow these steps to use foreach:

1.

Figure out what you want to do. In the following example, you've got a bunch of directories (1 through 3), and you want to create a tar file of each of them.

2.

Decide on the names of the files/directories that you want to do something to. In this case, you're going to create tar files for the directories directory1, directory2, and directory3.

3.

Pick a variable name that you want to use. For this example, you're going to use a variable named test. It doesn't matter what the variable name is, as long as it doesn't conflict with the name of the command.

4.

Issue the foreach command as foreach <variablename> (<filenames>). The foreach command then asks what you want to do for each file by displaying a question mark. Fill this in with whatever you need to do. Again, you're going to be taring files in the example. After giving foreach the command for whatever it is you want to do, finish it by putting the command end on a line by itself.

This can be seen as

 > foreach test (directory1 directory2 directory3) ? tar -cvf $test.tar $test ? end 

Your machine responds by running the tar command for each file you gave it to work with. Inside the foreach command loop, note the use of the expression $test. foreach goes through the list of filenames you gave it and puts each one sequentially in the test variable. To use the contents of a variable in the shell, put a $ sign before it. For example, foreach first puts mydirectory1 in the variable test. It then runs the tar command, and the shell expands the test variable to directory1. The tar command that gets executed actually looks like the following: tar -cvf mydirectory.tar mydirectory. The next time through the loop, foreach puts directory2 in the variable test, and the process is repeated.

You can use REs in the shell instead of enumerating the filenames to use the foreach command. If you notice in the previous example, all the directories you want to tar actually have part of their names in common directory. If you wanted to produce the same results without having to enumerate all the directory names to foreach, you might issue the preceding foreach command as follows: foreach test (*directory).

What Shell Should I Use? In some examples, you may see the C shell used (csh). It is the opinion of this author that csh is a tool not adequate for programming and learning shell scripting. Also, it is better for you to learn on the shell you know, or a commonly used and supported shell, especially if you are a new learner to Unix. The use of csh to create shell scripts should be avoided if you can. The use of csh to build simple scripts is cumbersome, problematic, and sometimes next to impossible.


It's easy to see how you can use Unix to control process, and now, you should start to see the granular level of control Unix can give you over what it is you do work on. You can fine-tune, tweak, and customize your system once you know how to master these fundamentals. Now that you are familiar with the foreach command, let's look at conditional statements such as while and if.



    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net