Functions - CSCI 330
43 points

Due 10 October 2015 Noon

For this assignment, create a set of aliases and functions that perform the tasks described below.

The easiest way to create and edit these is to put them in a text file. You can then source this file to declare or redefine the aliases and functions in the current login shell. If you named the file assn04, you can initialize them with the command sequence :

. assn04

The source command will read each line of the file and enter it into the command line buffer as if you were typing it in yourself.

The aliases and functions will now be loaded into your current login shell. Test your aliases and functions. If you find mistakes, simply edit your "assn04" file, fix your mistakes, save, and source the file again.

To prepare for this assignment, do the following.

Include comments in the assign04 file before each alias or function. Start comment lines with a #.

Give the problem number, a general description ( you don't have to copy the problem verbatim ) and any comments to help yourself.

To figure out what comments to use, pretend that you were going to be a TA next year for this course and that you were going to use your assignment as a key to counsel any students asking for help. How would you describe your answers so you could understand it after a year.

When creating a function, if possible use the environmental and local variables instead of specific values to make the functions portable.

For example, if you need to access your home directory, use the variable $HOME instead of /home/hopper/your_z9. This will allow your ta to test your functions in her/his account.

Be careful to notice whether the function or alias has caps in its name.


Part A. Aliases (16 points)

Write the following simple aliases.

Part B. Functions

1. (8 points) Write a function called findit that uses find to search from the directory specified as the 1st argument on the command line and looks for the file specified as the 2nd argument on the command line. Redirect redirect any errors to /dev/null.

Before findit runs find, have it check that the 1st argument exists on the command line.

Use either test's string check for non-zero length. Or you can check that $# variable is greater than zero. $# is the number of arguments passed to a command or function.

If no argument, print an error message.

If 1st argument exists, then check that it represents a directory. If not, print an error message.

Otherwise, run the find.

2. (7 points) Write a function called What that determines the type of file that a filename points to and if the filename itself is a symbolic link and prints the appropriate messages about what it found. Use the aliases created above to test file type.

So What should recognize regular, directory, character special, or block special.

If filetype is not one of those, What should check to see if it does exist anyway and report that. If the file does not exist, report that.

It should also recognize if the filename is a symbolic link that is not linked to an actual file. Most of the test commands will follow a symbolic link to the actual file named before testing its existence and type.

Pseudo code guideline :

You should be able use the aliases defined in 1st part.

The above logic order is a suggestion. Keep in mind that the order of the tests can simplify the logic.

To test What:

3.(12 points) Write a functions called junkit that will move file specified to the junk directory if it is a regular file. If it is a symbolic link, don't junk.

Create a directory called junk under your home directory. Make it fully accessible (rwx) only to you the user. Pseudo code guide


To junk a file, enter :

If target-file is a regular file, it should be moved to junk, looking something like target-file.2015243231407

Make a backup copy of the assn04 file and email the file to me (berezin) on hopper/turing.