Math 300 - Homework Assignment #1 (back to Math 300 main page)

Suggetsed solution could be found by this link (without "for" loop) or this link (using "for" loop).

The assignment is due Friday, June 30, 11:59pm.

Use the same server as we used in class, i.e. adams.math.wsu.edu and your individual account (the one you got from me on the small piece of paper).

Assignment is worth 50 points. You can get extra 10 points for implementing approach with "for" loop (see below text in red).

Don't be scared of the size of this assigment! Half of the commands dupliacte preceeding ones but with different parameters. So copying and pasting may be useful here. Here is the link explaining how to do this in nano editor (see the top and the most highly rated answer). Also use Ctrl+Shift+V (not just Ctrl+V) to paste text you copied outside PuTTY (or Terminal) window (e.g. some command from slides).

Using "for" loop (10 extra points): a smarter way to implement these steps is to use "for" loop for all repeated commands (take a look at the example in the slides). This approach is harder to implement, but it will make your script almost two times shorter. If you implement this approach, it will probably happen that the messages for user will be dsiplayed twice instead of just once - that's fine (you can also avoid displaying the same messages twice by displaying a message saying what exact folder was currently process, e.g. instead of displaying "Copied archives." twice you can display "Copied archives for yums." and "Copied archives for zips."

Also if you try to implement the above approach the variable you are using as an iterator ($VAR variable in the slides whcih loops over the list of values) will probably loop over two values: "yum" and "zip". In turn, the directories you need to create are called "yums" and "zips", so you will need to append "s" to the value of your variable. But if you do something like "mkdir ~/mymans/comressed/$VARs", you won't achieve desired result, since the shell will look up for variable called VARs, but not VAR. To tell the shell to look up for VAR variable you will need to surround your variable name with curely brackets, so your command will look like "mkdir ~/mymans/comressed/${VAR}s".

You have to create an executable shell script file named "getmans.sh", which, once executed, will do the following:
  1. Change current working directory to current user's home directory.
  2. Delete directory named "mymans" if any exists in current user's home directory with one command.
    Note 1: since you don't know whether "mymans" directory is empty or not and the command used to delete non-empty directory I showed you in class also works fine for empty directory, you probably should you this command.
    Note 2: if currently there is no "mymans" directory in your home directory and you use the command discussed above, you will see an error message saying "cannot remove `mymans': No such file or directory". It won't interrupt your script, but it would be better to suppress/hide this message. To do this add "f" option to the command discuessed above.
    Note 3: recall that if you have a command named, lets say, command1 and two options, lets say, a and b. If you want to use them at the same time your syntax should look like this: command1 -ab or command1 -ba
  3. Create a directory named "mymans" in current user's home directory.
  4. Create two subdirectories of above-mentioned "mymans" directory named "compressed" and "decompressed".
  5. Create two subdirectories in both above-mentioned "compressed" and "decompressed" directories named "yums" and "zips".
    Hence, your directories structure should look like this:
    ~/mymans:
       ~/mymans/compressed:
          ~/mymans/compressed/yums
          ~/mymans/compressed/zips
       ~/mymans/decompressed:
          ~/mymans/decompressed/yums
          ~/mymans/decompressed/zips
  6. Display the following message to user: "Created directories."
  7. Copy every file starting with "yum" and ending with ".1.gz" from "/usr/share/man/man1" directory to newly created "yums" directory located under "compressed" directory.
  8. Copy every file starting with "zip" and ending with ".1.gz" from "/usr/share/man/man1" directory to newly created "zips" directory located under "compressed" directory.
  9. Display the following message to user: "Copied archives."
  10. Change current working directory to "yums" (the one where you copied the files) and execute the following command: for i in *.gz; do zcat "$i" > "${i%.*}"; done (this command extracts contents of all archives you just copied to the current directory).
  11. Move all the files ending with ".1" from current directory to another "yums" directory which you created under "decompressed" directory. Change current working directory back to current user's home directory.
  12. Change current working directory to "zips" (the one where you copied the files) and execute the following command: for i in *.gz; do zcat "$i" > "${i%.*}"; done (this command extracts contents of all archives you just copied to the current directory).
  13. Move all the files ending with ".1" from current directory to another "zips" directory which you created under "decompressed" directory. Change current working directory back to current user's home directory.
  14. Display the following message to user: "Decompressed files."
  15. If environment variable named $KEEP_COMRPESSED equals "yes" display "Compressed files are retained." message to user; otherwise, delete "compressed" directory and all of its contents with one command and then display "Removed compressed files." message to user.
  16. Assume that there is another environment variable named $MANS_TO_SHOW which can take one of two values: either "yums" or "zips". If variable is equal to "yums" display "Here are your manuals for yums:" to user and list contents of "~/mymans/decompressed/yums" directory. If variable is equal to "zips" display "Here are your manuals for zips:" to user and list conents of "~/mymans/decompressed/zips" directory. You shouldn't use 'if' construct for this step (check out the example from silde 31)!
Once you are done with the script do the following:
  1. There is a direcotry named "public" in your home direcotry. Create a directory named "homeworks" under above mentioned "public" direcotry.
  2. Create a dirctory named "hw01" under newly created "homeworks" direcotry.
  3. Put your script to newly created "hw01" dirctory.
  4. Give every user on this machine permissions to execute your script (if you haven't done it yet).