| CSC 161 | Grinnell College | Spring, 2015 |
| Imperative Problem Solving and Data Structures | ||
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.
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.
Open a terminal window.
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.
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.
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.
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.)
Edit your .bashrc file:
Open a terminal window. Prepare to edit .bashrc; in your home directory, type:
emacs .bashrc &
# 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:
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.
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.
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"
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.
Edit your .emacs file:
Open a terminal window. Prepare to edit .emacs in your home directory, type:
emacs .emacs &
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.
Save the revised .emacs file.
Repeat Steps 4 and 5 for each partner in your programming group.
Refine your emacs environment to help support C programming.
In the "Options" menu, set the following options.
Enable "Syntax Highlighting (Global Font Lock mode)" as follows:
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!
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.
In this section, we will explore part of the Linux directory.
In a terminal window, type pwd (print working directory) to determine the absolute path name of the current directory.
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.
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.
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:
/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.
/home: You won't be surprised to hear that user accounts are stored in this directory.
/lib: This directory is the home of several libraries that can be used by programmers.
/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/.
Use the commands which and whereis to locate where the emacs program is located:
which emacs whereis emacs
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) |
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.
Organizing work in CSC 161:; Work in CSC 161 involves at least three types of activities:
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
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.
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.
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.
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.
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).
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.