CMSC 15400: Shell basics lab (Apr 2)

References

As you go through the exercises today, reference the following for help
  • the MacLab's online Unix tutorial,
  • the man pages for details on specific commands, and
  • the vi and emacs tutorials and cheat-sheets linked from Ken Harris's CMSC152 page,
along with these more obvious sources of information (in no particular order):
  • the web,
  • the TA, and
  • your classmates.

Exercises

The sections below contain short exercises intended to familiarize you with working in a Unix environment. Your goal is to refresh the Unix know-how you gained in CMSC 152/162, and maybe pick up some new tricks.

Create a document (plain-text, PDF, or Postscript) containing your solutions, using either the vi or emacs editor, and email it to clklein@cs.uchicago.edu, with subject line CMSC 15400: Lab 1, by 5:00pm.

Files and Directories

As you follow the directions below, copy the shell command(s) which accomplish(es) each bulleted task (but not the output) into your solutions file.
  • Within your home directory, create a directory for your work in this course, using the mkdir program. Choose an appropriate name, such as cs154.
  • Create a subdirectory for today's lab. Choose an appropriate name, such as lab1. cd to this directory.
  • List the contents of the directory ~clklein/html/15400-2008-spring/lab1/ by size, using the program ls.
  • Use find to locate all .tar and .tar.gz files within that directory. Next, copy them to the subdirectory you created for this lab.
  • Extract the files from these archives using tar and/or gunzip.
  • With a single command, delete all files with extension .del from the extracted directories. Hint: first use the find command to identify them. Next, extend this command by applying the -execdir (or -exec) flag, which runs the specified program on each file it finds. (See the EXAMPLES section of find(1), the find man page, if you have trouble with the syntax.)
  • Make a directory named tmp in the directory you created for this lab. Copy all .sh files from the root of either extracted directory to this tmp directory.
  • Execute one such .sh file, rename it (to anything), change its permissions so any user can write to it, and finally delete it.
  • Create a copy of the (entire) tmp directory then delete the original.
  • Archive either extracted directory, creating the tarfile mytar.tar then compress it using gzip. Alternatively, create a compressed tarfile directly with a single tar command by also including the -z flag.

Processes, Pipes, and Redirection

Again, copy the commands which accomplish these tasks to your solutions file.
  • Use head and tail to view the first and last eleven lines of a file.
  • Pipes, a mechanism for interprocess communication, can be used to feed the output of one program into another program. For example, the command history | grep chmod feeds the output of the history command to grep, which here searches for uses of chmod. Use a pipe, head, and tail to read the first five of the last ten lines of a file.
  • Direct a command's output to a file, rather than to the terminal or to another process, by appending > $filename to the command (replace $filename with the path of the file that should collect the output).
  • bc is an interactive calculator, which by default reads its input from the terminal. Instruct bc to read instead from the file ~clklein/html/15400-2008-spring/lab1/bc_input by appending the token < then that path to the command that invokes bc.
  • Many common C header files reside in the directory /usr/include/ and its descendent directories. With a single command, produce a file containing all the #include lines that appear in the header files in or below /usr/include/sys/. The lines of this file should be sorted and have no duplicates. Hints:
    1. Use find to identify these header files.
    2. The command grep ^\#include /usr/include/stdio.h outputs the lines of interest for stdio.h.
    3. Use sort and uniq.
  • Start a long-running command (e.g., top), suspend its execution with control-z, resume its execution with the command fg, and finally terminate the process with control-c. (Don't bother include this in your solutions file.)
  • The program sleep does nothing for the specified number of seconds before terminating. Execute sleep in your session's background by appending & to the command. Note that you may enter new commands while some others execute in the background.
  • Execute sleep 1000 &, noting the process id assigned to the background process. Execute kill $pid to kill this background process.
  • ssh to your neighbor's machine. Pipe the output of ps aux to grep to check that your neighbor is not running pico/nano.
  • To execute shell commands sequentially, separate them with a semicolon, e.g., do_one_thing; then_another. To execute the second command only if the first succeeds, instead separate them with the token &&, e.g., mkdir foo && rmdir foo.

    Write a single shell command to download, compile, and execute this program, deleting the executable created when it finishes. Hints:

    1. Use wget -q -O - to download the program and write it to standard output.
    2. Instruct gcc to read from standard input using the flags -x c -.
    3. Use a pipe to communicate between these processes.
    4. Don't forget to set the executable bit on the binary gcc produces.
  • Environment variables are one way in which Unix programs read configuration options. The shell built-in set displays the current environment configuration. Example environment variables include PATH, the search path for commands, and PRINTER which specifies the default printer for many programs. Extend your PATH variable to include the current working directory (identified by the dot character) using the export command: export PATH=$PATH:.. Show that with this extended path it's not necessary to prefix programs in the current directory with ./ to execute them. (Don't bother including this in your solutions file.)

Finished early?

Consider reading a Sed and Awk Micro-Primer .

Credits

This lab's original incarnation was inspired by Dan Grossman's CSE 303 course website.