c. Exercises I

Variables in Loops

This exercise refers to the shell-lesson-data/molecules directory. ls gives the following output:

cubane.pdb  ethane.pdb  methane.pdb  octane.pdb  pentane.pdb  propane.pdb

What is the output of the following code?

$ for datafile in *.pdb
do
	ls *.pdb
done

Now, what is the output of the following code?

$ for datafile in *.pdb
do
ls $datafile
done

Why do these two loops give different outputs?

Solution

Limiting Sets of Files

What would be the output of running the following loop in the shell-lesson-data/molecules directory?

$ for filename in c*
do
	ls $filename
done
  1. No files are listed.
  2. All files are listed.
  3. Only cubane.pdb, octane.pdb and pentane.pdb are listed.
  4. Only cubane.pdb is listed.
Solution

How would the output differ from using this command instead?

$ for filename in *c*
do
	ls $filename
done
  1. The same files would be listed.
  2. All the files are listed this time.
  3. No files are listed this time.
  4. The files cubane.pdb and octane.pdb will be listed.
  5. Only the file octane.pdb will be listed.
Solution