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.

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

Cutting and Pasting Between Windows

Linux allows you to cut and paste between windows without using the Control key. Linux does not require Control+c and Control+v— just use the left and middle mouse buttons, as described below.

  1. Consider the following script on my account that gives information related to your being logged into this workstation.
    /home/johnsonba/public_html/CSC161/2019F/modules/getting-started/src/hello-script
    To use this script, you could type the entire line at a terminal prompt, 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: Do not use the control Control key.

      Note also that you don't explicitly need to "focus" the target window first. Doing so would significantly increase the chances that you'd inadvertently highlight something new, overwriting what you'd implicitly "copied" before and defeating the purpose.

    Don't forget that you'll need to press the Enter at the end of the line to run the command.

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 easily in Emacs 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; some Emacs customization is also done with a file called .emacs (Note the period at the start of both file names. This means the files are "hidden" in a normal directory listing.)

  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:
      # Make MyroC libraries known to the execution environment
      LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/walker/MyroC/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. Upon success, it should print no output and simply give you a new Terminal prompt. If you do see any error messages, you will need to fix them (perhaps with the help of the instructor or mentor).

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

    The basic idea of this insertion is to establish an environment variable that will allow you to run programs using the robots. With this insertion, when we start using the robots, you'll be ready to run your programs!

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

    LD_LIBRARY_PATH

    This environment variable tells the operating system where to look for the libraries when it is running your programs.

    • LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/walker/MyroC/lib"

    export

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

    • export LD_LIBRARY_PATH

    By setting this environment variable here, the computer will know where to find the MyroC libraries when you work with 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.

    You might notice the syntax looks a bit Scheme-ish. That's because Emacs uses a variant of Scheme's ancestor language, LISP, called elisp. (We could discuss whether that makes them siblings, cousins, or nephew and uncle.)

  3. 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.
  4. Refine your emacs environment to help support C programming.
    • In the "Options" menu, set the following option(s).
      • Click the box for "Highlight Matching Parentheses"
    • 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 C-g will stop any editing process within emacs!

Repeat for Each Partner

  1. Repeat Steps 4 through 7 for each partner in your programming group. That means you will need to log out, have the other partner log in and repeat the process in their own account. Once the last partner completes this step, you may continue the rest of the lab from their account.

In the next 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 the command
      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 the command

    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, the command

    ls ~johnsonba

    lists the home directory for user johnsonba. Try both of these now.

  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, do the same to look at the following specific directories:

    1. /bin
    2. /home
    3. /lib
    4. /usr
  4. Use the commands which and whereis to locate where the emacs program is located:
    which emacs
    whereis emacs

Creating Directories and Preparing for Later Work

In completing the next steps of this lab, you may consult the table of commands from the reading.

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)
    • homework 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 homework. 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 homework
    ls
  2. Prepare shortcuts 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 /home/johnsonba/public_html/CSC161/2019F/shared/Makefile ./

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

    Background: After writing C programs, a utility called the C compiler 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 and their locations can be quite 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 ./
    cd ../labs
    ln -s ../Makefile ./
    cd ../homework
    ln -s ../Makefile ./

    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.

Optional: Additional .bashrc configurations

The default shell prompt in your terminal is the name of the host machine where the terminal is running, followed by a dollar sign ($), followed by a space. Some prefer the prompt give more useful information, such as the user who is logged in and the shell's current working directory. The following exercises guide you in making this configuration, should you prefer it.

  1. After opening it in emacs for editing, add the following (or a similar variant) to your .bashrc file.

    # Set shell prompt to "host cwd$ "
    PS1="\h \W$ "
    
    export PS1

    Don't forget to save your changes. Your new prompt should take effect the next time you log in to the MathLAN.

    While one may also use the \u option to include the user's login.

Of course, you may use any variant (rearranging or deleting pieces) you prefer. The complete list of options may be found in the GNU Bash Reference Manual.