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
-
Open a terminal window.
- 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.
- 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
Note, you should read this step, but you may feel free to skip doing it for now. It is included here so that you can refer to the process later.
-
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/passphraseprogram 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.
-
Consider the following script on my account that gives information related to your being logged into this workstation.
/home/weinman/public_html/courses/CSC161/2015S/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 Ctrl 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.
-
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.)
-
Edit your
.bashrcfile:-
Open a terminal window. Prepare to edit
.bashrc; in your home directory, type:emacs .bashrc &
-
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/Myro/lib" export LD_LIBRARY_PATHTo 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
.bashrcediting window. -
Save the revised
.bashrcfile.
-
Back in your terminal window, type
source .bashrc
This command tells the window to read the newly-changed
.bashrcfile.Contact the instructor if an error message is printed when you type
source .bashrc.
The basic idea of this insertion is to 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 tells the operating system where to look for the libraries when it's running your programs.
-
LD_LIBRARY_PATH="$LS_LIBRARY_PATH:/home/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 environment variable here, the computer will know where to find the MyroC libraries when you work with the Scribbler 2 robots.
-
-
Edit your
.emacsfile:-
Open a terminal window. Prepare to edit
.emacs; in your home directory, type:emacs .emacs &
-
Add the following lines to the bottom of your
.emacsfile:
To accomplish this, you can copy between Linux windows as before.; Set keyboard command shortcut for compiling (global-set-key [(control c) (c)] 'compile) -
Save the revised
.emacsfile.
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.)
-
-
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 theTogglebutton, and then theSave for future sessionsbutton above.
-
Refine your emacs environment to help support C programming.
-
In the "Options" menu, set the following option(s).
- Click the box for "Paren Match Highlighting (Show Paren mode)"
-
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!
-
Repeat for Each Partner
- 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
-
Current 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, andls ..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.
-
-
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 ~walkerlists the home directory for userwalker. - 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:
/bin/home/lib/usr
-
Use the commands
whichandwhereisto locate where theemacsprogram 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.
-
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
csc161as the base for all of the work for this course. Within that, this lab recommends that you create subdirectoriesprojects, labs,andsup-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 homework ls
-
Prepare shortcut for later C processing: Beginning in your home directory, move to the new
csc161directory, and copy a processing file, calledMakefile, to this directory. The relevant command sequence is:cd cd csc161 cp /home/weinman/public_html/courses/CSC161/2015S/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
gcc, the GNU 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 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
Makefileat this point of the course—for now, just be sure to copy this file to yourcsc161directory. -
Provide easy access to
Makefilefor 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
cdcommand to move to one of these subdirectories (e.g., toprojects). Then use thelscommand to check there is an entry forMakefile. To investigate this entry further, use the commandls -lthat should show thatMakefileindeed refers to the corresponding file in thecsc161directory. -
Anticipate an exciting C program (involving computation and speech) next week: As you get started next week, it will be helpful to save the program,
quarts-espeak.cto yourlabssubdirectory.
