CSCI 344
Beginning Bash Scripts
Lab 4

Write the following bash scripts.  You may use the Internet as a reference for bash script syntax and as a reference for UNIX/Linux commands (such as chmod and tar), but you may not search for solutions to these problems--that would completely defeat the purpose of doing these exercises.

When you complete one of the scripts, notify me and I will come watch a demo and give you credit for completing it.

Here is a link to the O'Reilly's Learning Bash.  Note: this link only works from computers in the csuchico.edu domain.

Unless explicitly specified, for all of these problems you may only use shell programming constructs (e.g. if, for, etc), you may not call any UNIX commands.


1) Write a script that looks in all the directories in your PATH and finds matches for a given command.  

$ mywhich ls
./ls
/bin/ls
$


Hints:  You need to change the internal field separator to something that makes sense for PATH

IFS=something



2) Write a recursive find command that takes a directory and a filename and reports all occurrences of the given filename in the directory tree rooted at the given directory.

$ myfind .  a.cpp 
./classes/152.f04/src/p1/t08/a.cpp
./classes/344.s07/src/script/a.cpp
./classes/340.f05/src/p1/t08/a.cpp
./tmp/a.cpp
$


3) Write a script that looks for .tar files and untars them.  Given a set of directories, if there is a .tar file in any of those directories, cd to that directory and call tar to untar the .tar file.  Below is the pseudo-code.  Use the tar command to untar the files.  Report the filenames of the files that have problems.

for all directories
for all files in this directory that have the .tar extension
    tar -xf file.tar
    if tar returned an error print
            error untarring directory/file.tar         // where directory is the actual directory and file is the actual filename


4) When copying files from cygwin to Linux, the protections often are corrupt.  Write a recursive function that takes a single directory and fixes the protection of that directory and all the files and subdirectories it contains.  

For directories, set the protection to 755.  For regular files set the protection to 644.  Use chmod to change the protection of individual files and directories.  You may not use the recursive flag of chmod, you must change the protection one file at a time.

Assume the protection is incorrect for all files and directories (it usually is).  Thus you can change the permission of files and directories w/o first checking to see if there protection is corrupt. You may use the UNIX find command.



5) When turning in assignments, students often set the protection so that I cannot read their files.  Write a script that finds all such files and generates a script that I can e-mail to the system administrator to fix the problems.  

$ fix_protection /user/projects/csci515/p3 > ~/bin/fix_protection

The output (what is redirected above) should have the form

chmod 644 /user/projects/csci515/p3/joesmith/p4.tar
chmod 644 /user/projects/csci515/p3/sallyjones/p4.tar
...