i. Exercises II

Doing a Dry Run

A loop is a way to do many things at once — or to make many mistakes at once if it does the wrong thing. One way to check what a loop would do is to echo the commands it would run instead of actually running them.

Suppose we want to preview the commands the following loop will execute without actually running those commands:

$ for datafile in *.pdb
do
    cat $datafile >> all.pdb
done

What is the difference between the two loops below, and which one would we want to run?

# Version 1
$ for datafile in *.pdb
do
    echo cat $datafile >> all.pdb
done
# Version 2
$ for datafile in *.pdb
do
    echo "cat $datafile >> all.pdb"
done
Solution

Nested Loops

Suppose we want to set up a directory structure to organize some experiments measuring reaction rate constants with different compounds and different temperatures. What would be the result of the following code:

$ for species in cubane ethane methane
do
    for temperature in 25 30 37 40
    do
        mkdir $species-$temperature
    done
done
Solution