Write the following bash scripts. You may use the Internet as a
reference for bash script syntax and as a reference for 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.
Here is a link to the O'Reilly's Learning Bash. Note: this link only works from computers in the csuchico.edu domain and only works sometimes.
Unless explicitly specified, for all
of these problems you may only use shell programming constructs (e.g.
if, for, etc), and basic commands like (e.g. echo, tar, chmod), you may
not call Linux commands that solve these problems for you.
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 (IFS) 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.
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 Linux find command.