CSC 161 Grinnell College Fall, 2014
 
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.sp14/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:.
      
      # set environmental variables for Myro C and Myro C++
         ### MYRO LIBRARIES ###
      
         ## Myro C -- When using libMyroC.so or libMyroCDev.so ##
         # include the location of the original and development MyroC headers #
         C_INCLUDE_PATH="$C_INCLUDE_PATH:/home/walker/Myro/include/MyroC"
         C_INCLUDE_PATH="$C_INCLUDE_PATH:/home/walker/Myro/include/MyroCDev"
      
         # include the location of the MyroC shared library object file #
         LIBRARY_PATH="$LIBRARY_PATH:/home/walker/Myro/lib"
      
         # make the libraries know to the execution environment #
         LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/walker/Myro/lib"
      
         export C_INCLUDE_PATH LIBRARY_PATH 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.

    C_INCLUDE_PATH

    This tells the compiler where to look for the MyroC header files (which is a big list of robot functions). Actually, there are two versions of the MyroC header files, and these two lines allow you to reference both versions.

    • C_INCLUDE_PATH="$C_INCLUDE_PATH:/home/walker/Myro/include/MyroC"
    • C_INCLUDE_PATH="$C_INCLUDE_PATH:/home/walker/Myro/include/MyroCDev"

    LIBRARY_PATH

    This tells the compiler where to look for the MyroC libraries (which tells the computer how to do the robot functions listed in the MyroC header files).

    • LIBRARY_PATH="$LIBRARY_PATH:~walker/Myro/lib"

    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 C_INCLUDE_PATH LIBRARY_PATH LD_LIBRARY_PATH

    By setting these environmental variables here, the computer will know where to find these packages when you work with the Scribbler 2 robots.

Repeat for Each Partner

  1. Repeat Step 4 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 before the home directory corresponding to the name. 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

File Utilities

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)

Navigating Directories

  1. Within your home directory, create a new directory csc161. Then move to this csc161 directory and create subdirectories projects, labs, and sup-prob. As the semester progresses, organization will help you keep your various files separate.

    You might try the following sequence of commands to set up these subdirectories. 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. Use the cd command to move to the labs subdirectory. Then, in anticipation of an upcoming lab, copy the file /home/walker/public_html/courses/161.sp14/modules/getting-started/quarts-espeak.c to your account using the cp command:

    cp /home/walker/public_html/courses/161.sp14/modules/getting-started/quarts-espeak.c .
    

    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).

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.