Readings

The focus of this week's readings is shell scripting. Now that we've discused a variety of Unix commands, we will start combining them to produce new tools and new programs via shell scripts.

The readings for Monday, February 15 are:

  1. Writing Shell Scripts

  2. Unix / Linux Bourne / Bash Shell Scripting Stuff

Optional resources:

  1. Bash Academy

  2. Bash Guide

    Additionally, Bashism.

  3. Shell programming with bash: by example, by counter-example

  4. Rich's sh (POSIX shell) tricks

#!/bin/sh

Regardless of which interactive shell you use, for the purposes of this class, we will use the bourne shell (ie. /bin/sh), which is the most basic and most portable Unix shell, as our scripting language.

Note, that the bourne shell is different from the bourne again shell (ie. /bin/bash). The latter can be considered a superset of the original bourne shell and thus backwards compatible with it. Although many of readings will discuss things in terms of the bourne again shell, we will only focus on the bourne shell subset.

Questions

In your reading05 folder, write the following shell scripts:

  1. environment.sh: Print the following environment variables: USER, HOME, SHELL, TERM, EDITOR, HOSTNAME, PATH, LD_LIBRARY_PATH, PWD.

    # Example
    $ ./environment.sh
    ./environment.sh
    USER            is pbui
    HOME            is /afs/nd.edu/user15/pbui
    SHELL           is /bin/bash
    TERM            is rxvt
    EDITOR          is vim
    HOSTNAME        is student00
    PATH            is /afs/nd.edu/user15/pbui/bin:/usr/local/games:/usr/games:/afs/nd.edu/user15/pbui/pub/anaconda-2.3.0/bin:/afs/nd.edu/user15/pbui/pub/bin:/afs/nd.edu/user14/csesoft/new/bin:/afs/nd.edu/user37/condor/software/sbin:/afs/nd.edu/user37/condor/software/bin:/usr/lib64/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
    LD_LIBRARY_PATH is /usr/local/lib:/usr/lib:/afs/nd.edu/user14/csesoft/new/lib64
    PWD             is /afs/nd.edu/user15/pbui
    
  2. hello.sh: Print Hello, {argument}! for each command line argument passed to the script:

    # Example
    $ ./hello.sh NEO AGENT_SMITH CYPHER
    Hello, NEO!
    Hello, AGENT_SMITH!
    Hello, CYPHER!
    
  3. mascot.sh: This checks the output of uname, and prints out Tux for Linux, Hexley for Darwin, and Beastie for FreeBSD, NetBSD, and OpenBSD.

    # Example
    $ hostname
    student00
    
    $ ./mascot.sh
    Tux
    
  4. resolve_links.sh: For each symbolic link in the specified directory, this script prints out what the link resolves to:

    # Example
    $ ./resolve_links.sh /usr/share/pixmaps
    /usr/share/pixmaps/eclipse.png links to /usr/share/icons/hicolor/48x48/apps/eclipse.png
    /usr/share/pixmaps/esc.png links to /usr/share/icons/hicolor/48x48/apps/esc.png
    /usr/share/pixmaps/redhat-accessories.png links to /usr/share/icons/System/48x48/apps/redhat-accessories.png
    /usr/share/pixmaps/redhat-applications.png links to /usr/share/icons/System/48x48/apps/redhat-applications.png
    /usr/share/pixmaps/redhat-email.png links to /usr/share/icons/System/48x48/apps/redhat-email.png
    /usr/share/pixmaps/redhat-extras.png links to /usr/share/icons/System/48x48/apps/redhat-extras.png
    /usr/share/pixmaps/redhat-games.png links to /usr/share/icons/System/48x48/apps/redhat-games.png
    /usr/share/pixmaps/redhat-graphics.png links to /usr/share/icons/System/48x48/apps/redhat-graphics.png
    /usr/share/pixmaps/redhat-home.png links to /usr/share/icons/System/48x48/apps/redhat-home.png
    /usr/share/pixmaps/redhat-internet.png links to /usr/share/icons/System/48x48/apps/redhat-internet.png
    /usr/share/pixmaps/redhat-main-menu.png links to /usr/share/icons/System/48x48/apps/redhat-main-menu.png
    /usr/share/pixmaps/redhat-office.png links to /usr/share/icons/System/48x48/apps/redhat-office.png
    /usr/share/pixmaps/redhat-preferences.png links to /usr/share/icons/System/48x48/apps/redhat-preferences.png
    /usr/share/pixmaps/redhat-presentations.png links to /usr/share/icons/System/48x48/apps/redhat-presentations.png
    /usr/share/pixmaps/redhat-programming.png links to /usr/share/icons/System/48x48/apps/redhat-programming.png
    /usr/share/pixmaps/redhat-server_settings.png links to /usr/share/icons/System/48x48/apps/redhat-server_settings.png
    /usr/share/pixmaps/redhat-sound_video.png links to /usr/share/icons/System/48x48/apps/redhat-sound_video.png
    /usr/share/pixmaps/redhat-spreadsheet.png links to /usr/share/icons/System/48x48/apps/redhat-spreadsheet.png
    /usr/share/pixmaps/redhat-starthere.png links to /usr/share/icons/System/48x48/apps/redhat-starthere.png
    /usr/share/pixmaps/redhat-system_settings.png links to /usr/share/icons/System/48x48/apps/redhat-system_settings.png
    /usr/share/pixmaps/redhat-system_tools.png links to /usr/share/icons/System/48x48/apps/redhat-system_tools.png
    /usr/share/pixmaps/redhat-tools.png links to /usr/share/icons/System/48x48/apps/redhat-tools.png
    /usr/share/pixmaps/redhat-web-browser.png links to /usr/share/icons/System/48x48/apps/redhat-web-browser.png
    /usr/share/pixmaps/redhat-word-processor.png links to /usr/share/icons/System/48x48/apps/redhat-word-processor.png
    /usr/share/pixmaps/temp-home.png links to /usr/share/icons/System/48x48/apps/temp-home.png
    

    Hint: you will need to use test and readlink.

Commands

In the reading05 folder, write one summary page called sh.md that contains examples of the following:

  1. variables

  2. capturing STDOUT

  3. if statement

  4. case statement

  5. for loop

  6. while loop

  7. function

  8. trap

Summaries

Note, your summaries should be in your own words and not simply copy and pasted from the manual pages. They should be short and concise and only include common use cases.

Feedback

If you have any questions, comments, or concerns regarding the course, please provide your feedback at the end of your response.

Submission

To submit your assignment, please commit your work to the reading05 folder in your Assignments Bitbucket repository by the beginning of class on Monday, February 15.