CSC 161 Grinnell College Spring, 2015
 
Imperative Problem Solving and Data Structures
 
 

Linux Basics

The original Unix operating system and its successor Linux were designed to support the common tasks and needs of computer users. This laboratory exercise focuses on some commands to set up your account to utilize Scribbler 2 robots and to establish subdirectories for your CSC 161 activities.

Class Preparation before Class

Work Started in Class

The Terminal Window

Most C-based work involves use of a terminal window — not a graphical user interface (e.g., point-and-click) that may be used in other contexts. Using a terminal window allows a user to take advantage of many powerful capabilities of the Linux operating system. However, for many CSC 161 students, use of a terminal window may be a new and different experience. This lab starts with some basics.

Opening the Terminal Window

  1. Open a terminal window.

    1. You may already have a terminal window on screen. If not, start one by moving the pointer onto the small monitor icon on the bottom row of the front panel, and click with the left mouse button. If you do not have the terminal icon, speak with the instructor.
    2. Move your mouse to the top border of the terminal window, click the left mouse. With the left mouse button depressed, move the terminal window to the upper left of your screen. (If you move the terminal window too far, it may go into another "workspace", and the system may display only material in this new "workspace". If this happens and you want all material together, just move the terminal window back in the opposite direction from before, so the terminal window will be put back into the old workspace.)

Changing Your Password

  1. Choose a new password. Make it something that you can easily remember, but not an English word or a name, since it is easy for system crackers to break in by guessing your password if you choose it from one of those categories. Using a multi-word phrase may be helpful.

    Use the /net/bin/passphrase program within a terminal window to change your password.

Cutting and Pasting Between Windows

Linux allows you to cut and paste between windows without using the control Ctrl key. Linux does not require Ctrl c and Ctrl v — just use the left and middle mouse buttons.

  1. Consider the following script on my account that gives information related to your being logged into this workstation.

    /home/walker/public_html/courses/161.sp15/modules/getting-started/hello-script
    

    To use this script, you could type the entire line into a workstation, but that would be tedious and error prone. Instead, try the following:

    • Select this line from this lab exercise in your browser as follows: move the cursor to the beginning of the line and push down the left mouse button. Then, holding the button down, move the mouse to the end of the line. (The entire line now should be highlighted.) When the desired line is highlighted, stop pressing on the left mouse button — the line should stay highlighted.

    • Move the mouse to the terminal window, and click the middle mouse button to paste the line into the terminal.

    Note: In this process do not use the control Ctrl key.

Editing

For most of this course, you will need to use a text editor that does not add behind-the-scenes formatting details (e.g., fonts, paragraph styles, heading styles, etc.). I suggest that you use emacs (e.g., vi is another option), since emacs is quite powerful, has capabilities useful for C programming, and provides buttons and other features that may seem similar to other environments.

emacs can be opened from the Xfce menu (with the picture of a mouse) at the bottom of screen (look under the "Development" section for "GNU Emacs". However, much of the work we will be doing all semester will be based in a terminal window, so it may be helpful to gain experience with a terminal environment now.

Although the following instructions may seem confusing to you right now, it will make more sense as the semester progresses. These next steps are necessary for you to be able to program with the Scribbler 2 throughout the course. When you log into MathLAN, various preliminaries are set up for you in a file called .bashrc in your home directory. (Note the period at the start of the file name.)

  1. Edit your .bashrc file:

    1. Open a terminal window. Prepare to edit .bashrc; in your home directory, type:

      emacs .bashrc &
      
    2. Add the following lines to the bottom of your .bashrc file:

      # add the current directory to the search path
         PATH=$PATH:.
      
      # make the libraries known to the execution environment 
      LD_LIBRARY_PATH="$LS_LIBRARY_PATH:/home/walker/Myro/lib"
      
      export LD_LIBRARY_PATH
      

      To accomplish this, you can copy between Linux windows:

      • Place your mouse at the top left of the above region.
      • Hold the left mouse button down and move the mouse to highlight the entire section.
      • When you stop pressing the left mouse button, the region should stay highlighted.
      • Move the mouse to the bottom of the emacs window, and press the middle mouse button. The highlighted material should be inserted in the .bashrc editing window.
      • Save the revised .bashrc file.
    3. Back in your terminal window, type

      source .bashrc
      

      This command tells the window to read the newly-changed .bashrc file.

      Contact the instructor if an error message is printed when you type source .bashrc.

    The basic idea of this insertion is to define three environmental variables which will allow you to compile cleanly with the robots. With this insertion, when we start using the robots, you'll be ready to compile and run your programs!

    Now we'll explore statements you just pasted into your .bashrc file in more detail.

    PATH

    During the course, you will be creating and running many programs that you have written in C. Of course, in order to run a program, Linux must be able to find it. Since you will be issuing commands in a terminal window, it is natural to tell Linux to look at the directory for your terminal window. In Linux jargon, this directory is called . (dot), and the PATH command (above) tells Linux to check the current directory when trying to run a program.

    LD_LIBRARY_PATH

    This tells the operating system where to look for the libraries when it's running your programs.

    • LD_LIBRARY_PATH="$LS_LIBRARY_PATH:~walker/Myro/lib"

    export

    This statement exports all of your new environment variables into any future instances of your Bash shell.

    • export LD_LIBRARY_PATH

    By setting this environmental variables, the computer will know where to find the MyroC library when you want to run programs using the Scribbler 2 robots.

  2. Edit your .emacs file:

    1. Open a terminal window. Prepare to edit .emacs in your home directory, type:

      emacs .emacs &
      
    2. Add the following lines to the bottom of your .emacs file:

      ; Set keyboard command shortcut for compiling
      (global-set-key [(control c) (c)] 'compile)
          

      To accomplish this, you can copy between Linux windows as before.

    3. Save the revised .emacs file.

Repeat for Each Partner

  1. Repeat Steps 4 and 5 for each partner in your programming group.

Modifying emacs

  1. By default, the emacs editor is configured, so that you encounter a split screen each time you open the editor. If you find this feature annoying, you can change this default in either of two ways:
    • When you open emacs, look at the bottom of the "Welcome to GNU Emacs" window. Click the box that says "Never show it [this startup screen] again."
    • Within emacs,
      • click the "Options" tab at the top of the window, select "Customize Emacs" and then "Specific Option".
      • The phrase "Customize variable:" will appear in the little window at the bottom. Type inhibit-startup-screen
      • A new window will appear, which contains the line
        Inhibit Startup Screen: Toggle off (nil)
        Click the Toggle button, and then the Save for future sessions button above.
    With either choice, emacs should start with just one window (no split screen) in the future.
  2. Refine your emacs environment to help support C programming.

    • In the "Options" menu, set the following options.

      • Click the box for "Paren Match Highlighting (Show Paren mode)"
      • Click the box for "Case-Insensitive Search"
    • Enable "Syntax Highlighting (Global Font Lock mode)" as follows:

      • Press the escape key ("Esc") [this is considered a "meta" key; nothing will appear to happen until you finish the next step]
      • Type x (for execute) [M-x will appear in the small window at the bottom]
      • Type global-font-lock-mode (and return) in the bottom window
    • When done, click the "Save Options" choice in the "Options" menu.

    Reminder: Since emacs is a very powerful editor, sometimes you will hit an erroneous key, emacs will do something unexpected, and then you will wonder what is happening. In such cases, the keystroke combination <ctrl>-g will stop any editing process within emacs!

Homework

In this section, we will explore part of the Linux directory structure, review some Linux directory commands, and suggest a way to set up your account to organize materials for CSC 161.

The Linux Directory

In this section, we will explore part of the Linux directory.

Pathnames

  1. Current Directory
    1. In a terminal window, type pwd (print working directory) to determine the absolute path name of the current directory.

    2. Type ls . to get a listing of the current directory, and ls .. to get a listing of all files in the parent directory. Note that your current directory should be visible as one item within its parent directory.

  2. Home Directory

    The tilde character used alone specifies your home directory, so ls ~ will give a listing of your home directory. When the tilde appears before a name, the combination denotes the home directory for the corresponding username. Thus, ls ~walker lists the home directory for user walker.

  3. Linux File Hierarchy

    The top of the Linux file hierarchy is designated by a slash (/) and is called root. Use the command ls / to obtain a listing of all files and directories within the root directory. How many are there?

    In reviewing the files within the root directory, look at the following specific directories:

    1. /bin: These are the executable programs that comprise the GNU/Linux utilities. For example, there is an executable file here named ls that is run when you issue the command ls.

    2. /home: You won't be surprised to hear that user accounts are stored in this directory.

    3. /lib: This directory is the home of several libraries that can be used by programmers.

    4. /usr: The name of this directory is pronounced "user", and it generally contains application programs, libraries, and other files that are not part of the GNU/Linux system (i.e., optional resources intended for and requested by users). For example, the Linux C library for the gcc compiler is found as file libgcc.a in the subdirectory /usr/lib/gcc/x86_64-linux-gnu/4.7/.

    root directory hierarchy
  4. Use the commands which and whereis to locate where the emacs program is located:

       which emacs
       whereis emacs
    

File Commands

Consult the following commands in completing the next steps of this lab:

Utility Description
ls "list" files and directories
pwd "print working directory"
cd "change (your working) directory"
mkdir "make directory"
rmdir "remove directory"
cp "copy" a file or directory
mv "move" a file or directory (i.e., rename it)
rm "remove" a file (i.e., delete it)

Creating Directories and Preparing for Later Work

In Step 4, you expanded the .bashrc file in your home directory in preparation for processing C programs involving robots. The following steps continue this setup. While not strictly necessary, these steps will simplify your later work substantially!

Repeat the following steps for each partner.

  1. Organizing work in CSC 161:; Work in CSC 161 involves at least three types of activities:

    • in-class labs (done collaboratively)
    • projects (done collaboratively)
    • supplemental problems (done individually, without collaboration)

    To organize this work, this lab strongly suggests that you create a new directory csc161 as the base for all of the work for this course. Within that, this lab recommends that you create subdirectories projects, labs, and sup-prob. As the semester progresses, organization will help you keep your various files separate.

    To accomplish this work, you might try the following sequence of commands, starting in your home directory within a terminal window. Refer to the table above for a description of each command, and be sure you can explain what each command does!

    mkdir csc161
    cd csc161
    pwd
    mkdir projects
    mkdir labs
    mkdir sup-prob
    ls
    
  2. Prepare shortcut for later C processing: Beginning in your home directory, move to the new csc161 directory, and copy a processing file, called Makefile, to this directory. The relevant command sequence is:

    cd
    cd csc161
    cp ~walker/public_html/courses/161.sp15/Makefile .
    

    Note: The dot at the end of the line indicates that the file is to be copied from /home/walker/... to the current directory (i.e., . stands for the current directory).

    Background: After writing C programs, a utility (called gcc) is needed to prepare the programs to run. Part of this process requires the specification of relevant libraries — particularly when the programs control a robot.

    • The bad news: specification of the relevant libraries can be quite tedious (not hard, but tedious).
    • The good news: we can incorporate the library details into a special file, called Makefile.

    No need to worry about the details of Makefile at this point of the course — for now, just be sure to copy this file to your csc161 directory.

  3. Provide easy access to Makefile for each of the subdirectories for projects, labs, and supplemental problems. In Linux, a simple way to provide this access is to create symbolic links from one file to another. Although the topic of symbolic links may be a bit advanced, creating the links is reasonably straight forward. The following sequence of commands is suggested:

    cd
    cd csc161
    cd projects
    ln -s ../Makefile projects
    cd ../labs
    ln -s ../Makefile labs
    cd ../sup-prob
    ln -s ../Makefile sup-prob
    

    To check these commands, use the cd command to move to one of these subdirectories (e.g., to projects). Then use the ls command to check there is an entry for Makefile. To investigate this entry further, use the command ls -l that should show that Makefile indeed refers to the corresponding file in the csc161 directory.

  4. Anticipate an exciting C program (involving computation and speech) next week: As you get started next week, it will be helpful to copy a program, called quarts-espeak.c to your labs subdirectory.

    • Use the cd command to move to your labs subdirectory.
    • Copy the file /home/walker/public_html/courses/161.sp15/modules/getting-started/quarts-espeak.c to your account using the cp command:
      cp /home/walker/public_html/courses/161.sp15/modules/getting-started/quarts-espeak.c .
      

      Again, note, the dot at the end of the line to indicate the current directory (i.e., . stands for the current directory).

Feedback Welcome

Development of laboratory exercises is an iterative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.