Homework 1 (Due 6 pm, Friday, 10/13/2017)ΒΆ

Please submit your homework (parts 5, 6, 7, 8 and 9) to your git repo by 6 pm, Friday, 10/13/2017. Although there is no need to commit any proofs of your work on parts 1 ~ 4, you need to make sure you complete the assignments.

  1. Please make either a Linux OS or Mac OS system available on your machine (see Computing platforms and Installing Linux on Your PC).

  2. Please make sure you practice the basic Linux commands (see Basic Unix/Linux Commands) in the lecture note, as well as read and watch the recommended articles and visual tutorials.

  3. There are a handful of Linux/Unix commands that are frequently used but haven’t been covered in the lecture note. Some examples include: apropos, awk, diff, chmod, chown, ln, uniq, du, etc. Study these commands using the Linux man page (e.g., man apropos) as well as reading/watching online resources (e.g., Google searches, youtube videos, etc.). Check what kinds of optional flags (i.e., letters followed by the minux sign, - on man pages) are available for each of those commands and try (at least some of) them.

  4. Setup an account on BSOE and clone the course repo from the remote course repository on riverdance BSOE server to your local machine (see the sections in Distributed systems (e.g., Git)).

  5. Setup an account on Bitbucket to create your own repository hosted on Bitbucket. Follow the instructions below to practice creating a file and modify it:

    1. Generate /homework and /homework/hw1 directories on your local master branch and create/edit a file named bio.txt under homework/hw1. This file includes the following information:
    • name, your major, email
    • shool year
    • research interests
    • advisor’s name (if any)
    • experience in scientific computing: (1. a lot, 2. somewhat, 3. none)
    • experience in Linux/Unix (or Mac OS X): (1. a lot, 2. somewhat, 3. none)
    1. Check-in (or make a commit) bio.txt with some check-in comments such as:

      “my first check-in to my own repo on mm/dd/yy as part of homework 1”.

    2. After a successful check-in of bio.txt, modify it by adding a new line on your OS and computer:

    • types of OS and machine for the class
    1. Check-in the updated bio.txt to the repo again.
    2. Check your commit history on your Bitbucket website account to see if the history shows up correctly. Also use git commands (e.g., git status, git log, git log --graph, etc.) to monitor your commit history. To see what kinds of options available for each git command, for example git log, try git log --help to see various options.
  6. Find roster.txt in the hw/hw1 directory in the course repo you cloned from riverdance BSOE server. Check what kind of information the file has using proper linux commands. What you want is to extract information of the class members. When you do this, you are only allowed to use combinations of linux commands to obtain what you want to know.

    Write a set of linux commands, all combined in one single line for each problem using pipes, redirections, etc.) in order to do the following tasks, using roster.txt as an input file for all cases. In each, you need to provide a single line command and your output as your answer.

    1. Produce a file roster_sort_firstName.txt that lists the class members in alphabetical order of their first names.

    2. Produce a file roster_sort_lastName.txt that lists the class members in alphabetical order of their last names.

    3. Produce a file roster_sort_lastName_noDuplicate.txt where all the duplicated names are removed by ignoring case differences (i.e., lee and Lee should be counted as same). Hint: use sort and uniq commands with options to ignore case sensitivity.

    4. Write a one-line Linux command that counts the number of enrolled students, using roster_sort_lastName_noDuplicate.txt as input.

    5. The names in the file you produced in Part c may look like:

      BenezraLauren
      CatelaniSierra
      ChenJianhong
      Dadgarparvin
      .
      .
      .
      

      Produce a list of names with , and a space between the last and first names that looks like:

      Benezra, Lauren
      Catelani, Sierra
      Chen, Jianhong
      Dadgar, parvin
       .
       .
       .
      

      Save your output to a file named roster_sort_lastNameSpace_noDuplicate.txt.

    6. Count how many PhD students are enrolled in the class. Repeat the same to count MS and undergraduate students too.

    7. Produce a list of SCAMMS students’ last names only in reverse alphabetical order.

  7. Include the following two tasks in your .bash_profile or .bashrc:

    1. Choose a directory location you most often visit (e.g. /Users/dongwook/Repos/ucsc/soe/teaching/2017-2018/Fall/AMS209/ams209Git/). Add an export command to set a variable called MY_PATH equal to the location of this directory (see the .bashrc examples in Basic Unix/Linux Commands). Open a new terminal window and query the MY_PATH using:

      $ echo $MY_PATH
      

      and try:

      $ cd $MY_PATH
      

      Learn from this and see this is an easy way that can make navigating your files easier.

    2. Include a new alias command for ls with an option either --color or -G whichever available on your system. You can name your new alias command little bit differently from the standard ls by appending c at the end, for instance:

      alias lsc='ls --color'
      

      or:

      alias lsc='ls -G'
      

      Open a new terminal window (or execute source ~./bashrc or source ~./bash_profile on the current terminal without needing to open a new terminal windows), and see the difference between the standard command, ls, and the new customized command, lsc.

  8. One day, you wonder if there is any convenient way to move N levels of directories up from the current directory location while navigating Linux directory trees on your computer. After some internet search, you found one good approach:

    Make alias in your .bash_profile or .bashrc:

    function cd_up() {
    cd $(printf "%0.0s../" $(seq 1 $1));
    }
    alias 'cd..'='cd_up'
    

    and use:

    $ cd.. 3
    

    This little hack will make your navigation easier from one location to another upper directory where there are N directory levels between the two locations using a single command. Otherwise, you need to do:

    $ cd ../../../
    

    With this little example, you want to write a similar routine that tells you the information on N levels upper directory location (i.e., the information you get from cd ../../../ && pwd when considering N=3), but keep you staying in the current directory rather than ending up with moving N levels up as in the case with cd.. 3. Write a simple function routine called pwd_up(), similar to cd_up(), for this purpose and include it in your .bash_profile (or .bashrc) with an alias:

    alias 'pwd..'='pwd_up'
    
  9. You heard from your computer gru friend that there is a cool way of writing a scipt called shell script. As your first attempt, you decided to convert a typical README file into an executable by modifying it into a shell script. The README file you want to convert is below:

    # Any statements followed by "#" are comments.
    
    #/Users/dongwook/Repos/ucsc/soe/teaching/2017-2018/Fall/AMS209/ams209Git/testFiles
    
    This is a README file. The main purpose of this file is to
    provide information about files and subdirectories under a
    directory.
    
    The first homework set is fun. I want to try harder problems!
    LOL!!!
    

    After some online search (article 1, article 2, article 3), you found that you can easily convert it to a self-displaying document by adding:

    #!/usr/bin/more
    

    on top of the file in the first line.

    The next step to convert it to an executable file. To do this, you run:

    $ mv README README.sh
    $ chmod a+x README.sh
    

    Now, the README.sh became an executable shell script (how do you tell it? You can run ls -al README.sh before and after chmod a+x README.sh. Compare what kind of changes you see before and after. (Study and explain what chmod a+x README.sh means.)

    You can execute README.sh by running:

    $ ./README.sh
    

    You also learned that you can write a shell script including a set of Linux commands by starting the first line:

    #!/bin/bash
    

    For example, a simple shell script with echo commands may look like:

    #!/bin/bash
    # This line is a comment!
    echo Hello AMS 209!
    echo $USER
    

    The first line tells Linux/Unix that the file is to be executed by /bin/bash. In case your shell is bash by default, you don’t really need #! in the first line. However, it’s always safe to include that because you (or someone else) may launch the script from a non-bash shell such as tcsh, zch, etc. (see the end of article 2).

    You are now ready to write a bit more complicated shell script. Write a shell script called check_dirSize.sh that first checks the disk usage (e.g., du) under a directory (e.g., the local ams209 directory you cloned the course material from the riverdance server), sort the contents by size in decreasing order, and print the top 3 largest ones (directories or files) to a file output.txt and the entire list to screen. Your output.txt should look like:

    401504  ./chapters
    79880   ./_build
    376     ./baskin-logo-banner.jpg
    

    if you run the shell script in your course Git directory after executing:

    $ make clean && make html
    

    Note: The above make html will build the course Sphinx directory to produce the lecture note in html. You can locally open the latest html build on your computer locally. On Mac:

    $ open _build/html/index.html
    

    On Linux, you can choose your web browser and open _build/html/index.html by using a drop-down menu.

    Unless you have Sphinx installed and running on your computer, you are not ready to execute make html yet. Since your solution should be able to work on any directory, you can test your solution anywhere under your home directory.